Skip to content
This repository was archived by the owner on Jun 9, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5d5f405
Turned the short php tag into the standard long php tag, reordered c…
tchalvak Jan 29, 2010
8a7b21f
Fixed start/end PHP tags
gedrox May 14, 2012
8ff6e84
Fixed problems with transparency
gedrox May 14, 2012
43d8797
Resizer is cropping images now
gedrox Jul 12, 2012
f0c8ceb
Fixed GIF Bug
XjSv Sep 17, 2012
317ae6f
Update smart_resize_image.function.php
nunorafaelrocha Oct 19, 2012
665bd91
add quality input for function
Nimrod007 Jun 8, 2013
660fe6f
update readme file
Nimrod007 Jun 8, 2013
63391f3
Update README.rdoc
Nimrod007 Jun 10, 2013
ed072c6
bug fix
YuzuruS Jul 17, 2013
dd3c585
Update README.rdoc
Nimrod007 Jul 27, 2013
1b4cc7e
imagegif function has no 'quality' argument, imagepng's quality argum…
Henridv Sep 17, 2013
9648439
Merge pull request #1 from Henridv/master
Nimrod007 Sep 17, 2013
0f5734f
Merge remote-tracking branch 'tchalvak/master'
Jan 29, 2014
4326cdd
Merge remote-tracking branch 'gedrox/master'
Jan 29, 2014
e47191c
Merge remote-tracking branch 'XjSv/master'
Jan 29, 2014
1d5b011
Merge remote-tracking branch 'nunorafaelrocha/master'
Jan 29, 2014
7d406ae
Merge remote-tracking branch 'YuzuruS/master'
Jan 29, 2014
6065654
correct opening tag
Jan 29, 2014
85d7ec9
Merge pull request #2 from diego-vieira/master
Nimrod007 Jan 30, 2014
790b1c1
Update README.rdoc
Mar 6, 2014
736792d
Merge pull request #3 from mamamou/patch-1
Nimrod007 Mar 6, 2014
0cd2fda
Update smart_resize_image.function.php
BobRobH Mar 21, 2014
7764fbf
Update README.rdoc
BobRobH Mar 21, 2014
2f98d27
Merge pull request #4 from BobRobH/master
Nimrod007 Mar 21, 2014
8115520
Update README.rdoc
Nimrod007 Mar 21, 2014
686328e
Update README.rdoc
Nimrod007 Mar 21, 2014
56ec0e4
Update README.rdoc
Nimrod007 Jul 14, 2014
90f11fd
change readme markup
May 24, 2015
f1c380d
Update README.md
May 24, 2015
f015ff4
Update README.md
May 24, 2015
848e7ae
Update README.md
May 24, 2015
02e9a20
Update README.md
May 26, 2015
b451a8f
add crop position
May 27, 2015
be5750b
Update README.md
May 28, 2015
0c9fdc6
New option: grayscale
zatikbalazs May 5, 2016
c3a6abf
Merge pull request #12 from zatikbalazs/patch-1
Jun 29, 2016
75c3de8
Update README.md
Jun 29, 2016
d0ba3e8
Update README.md
Nimrod007 Apr 17, 2017
f941123
Update README.md
vallicgrr Jun 26, 2017
03cebf2
Merge pull request #21 from vallicgrr/patch-1
Nimrod007 Jul 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# PHP - smart&easy image resize function

* resizing an image with 1 simple function.
* more stuff - visit my blog : www.nimrodstech.com

## How to use : (code snippet)

```php
//indicate which file to resize (can be any type jpg/png/gif/etc...)
$file = 'your_path_to_file/file.png';

//indicate the path and name for the new resized file
$resizedFile = 'your_path_to_file/resizedFile.png';

//call the function (when passing path to pic)
smart_resize_image($file , null, SET_YOUR_WIDTH , SET_YOUR_HEIGHT , false , $resizedFile , false , false ,100 );
//call the function (when passing pic as string)
smart_resize_image(null , file_get_contents($file), SET_YOUR_WIDTH , SET_YOUR_HEIGHT , false , $resizedFile , false , false ,100 );

//done!
```

## Funcion call (with doc)

```php
/**
* easy image resize function
* @param $file - file name to resize
* @param $string - The image data as a string, default is null
* @param $width - new image width
* @param $height - new image height
* @param $proportional - keep image proportional, default is no
* @param $output - name of the new file (include path if needed)
* @param $delete_original - if true the original image will be deleted
* @param $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink
* @param $quality - enter 1-100 (100 is best quality) default is 100
* @return boolean|resource
*/
function smart_resize_image($file,
$string = null,
$width = 0,
$height = 0,
$proportional = false,
$output = 'file',
$delete_original = true,
$use_linux_commands = false,
$quality = 100
) {code code code...

```

### Thanks for all the people adding more functions! keep it going

### Enjoy!






##### License

Copyright © 2008 Maxim Chernyak

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

51 changes: 0 additions & 51 deletions README.rdoc

This file was deleted.

53 changes: 53 additions & 0 deletions issue#1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#PHP - smart&easy image resize function with crop position option

### due to [issue#1](https://github.com/Nimrod007/PHP_image_resize/issues/6).

##How to use : (code snippet)

```php
//indicate which file to resize (can be any type jpg/png/gif/etc...)
$file = 'your_path_to_file/file.png';

//indicate the path and name for the new resized file
$resizedFile = 'your_path_to_file/resizedFile.png';

//call the function with crop from top
smart_resize_image($file , null, SET_YOUR_WIDTH , SET_YOUR_HIGHT , false , $resizedFile , false , false ,100 , true);

//call the function with crop from center (defualt)
smart_resize_image(null , file_get_contents($file), SET_YOUR_WIDTH , SET_YOUR_HIGHT , false , $resizedFile , false , false ,100, false);

//done!
```

##Funcion call (with doc)

```php
/**
* easy image resize function
* @param $file - file name to resize
* @param $string - The image data, as a string
* @param $width - new image width
* @param $height - new image height
* @param $proportional - keep image proportional, default is no
* @param $output - name of the new file (include path if needed)
* @param $delete_original - if true the original image will be deleted
* @param $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink
* @param $quality - enter 1-100 (100 is best quality) default is 100
* @param $cropFromTop - if false crop will be from center, if true crop will be from top
* @return boolean|resource
*/
function smart_resize_image($file,
$string = null,
$width = 0,
$height = 0,
$proportional = false,
$output = 'file',
$delete_original = true,
$use_linux_commands = false,
$quality = 100,
$cropFromTop = false
) {
) {code code code...

```
131 changes: 131 additions & 0 deletions issue#1/resize_image_with_crop_options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* easy image resize function
* @param $file - file name to resize
* @param $string - The image data, as a string
* @param $width - new image width
* @param $height - new image height
* @param $proportional - keep image proportional, default is no
* @param $output - name of the new file (include path if needed)
* @param $delete_original - if true the original image will be deleted
* @param $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink
* @param $quality - enter 1-100 (100 is best quality) default is 100
* @param $cropFromTop - if false crop will be from center, if true crop will be from top
* @return boolean|resource
*/
function smart_resize_image($file,
$string = null,
$width = 0,
$height = 0,
$proportional = false,
$output = 'file',
$delete_original = true,
$use_linux_commands = false,
$quality = 100,
$cropFromTop = false
) {

if ( $height <= 0 && $width <= 0 ) return false;
if ( $file === null && $string === null ) return false;

# Setting defaults and meta
$info = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
$cropHeight = $cropWidth = 0;

# Calculating proportionality
if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min( $width / $width_old, $height / $height_old );

$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
}
else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
$widthX = $width_old / $width;
$heightX = $height_old / $height;

$x = min($widthX, $heightX);
$cropWidth = ($width_old - $width * $x) / 2;
$cropHeight = ($height_old - $height * $x) / 2;
}

# Loading image to memory according to type
switch ( $info[2] ) {
case IMAGETYPE_JPEG: $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string); break;
case IMAGETYPE_GIF: $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string); break;
case IMAGETYPE_PNG: $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string); break;
default: return false;
}


# This is the resizing/resampling/transparency-preserving magic
$image_resized = imagecreatetruecolor( $final_width, $final_height );
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$transparency = imagecolortransparent($image);
$palletsize = imagecolorstotal($image);

if ($transparency >= 0 && $transparency < $palletsize) {
$transparent_color = imagecolorsforindex($image, $transparency);
$transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($image_resized, 0, 0, $transparency);
imagecolortransparent($image_resized, $transparency);
}
elseif ($info[2] == IMAGETYPE_PNG) {
imagealphablending($image_resized, false);
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
imagefill($image_resized, 0, 0, $color);
imagesavealpha($image_resized, true);
}
}

if ($cropFromTop){
$cropHeightFinal = 0;
}else{
$cropHeightFinal = $cropHeight;
}
imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeightFinal, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);


# Taking care of original, if needed
if ( $delete_original ) {
if ( $use_linux_commands ) exec('rm '.$file);
else @unlink($file);
}

# Preparing a method of providing result
switch ( strtolower($output) ) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
$output = $file;
break;
case 'return':
return $image_resized;
break;
default:
break;
}

# Writing image according to type to the output destination and image quality
switch ( $info[2] ) {
case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break;
case IMAGETYPE_PNG:
$quality = 9 - (int)((0.9*$quality)/10.0);
imagepng($image_resized, $output, $quality);
break;
default: return false;
}

return true;
}
Loading