├── README.md ├── issue#1 ├── README.md └── resize_image_with_crop_options.php └── smart_resize_image.function.php /README.md: -------------------------------------------------------------------------------- 1 | # PHP - smart&easy image resize function 2 | 3 | * resizing an image with 1 simple function. 4 | * more stuff - visit my blog : www.nimrodstech.com 5 | 6 | ## How to use : (code snippet) 7 | 8 | ```php 9 | //indicate which file to resize (can be any type jpg/png/gif/etc...) 10 | $file = 'your_path_to_file/file.png'; 11 | 12 | //indicate the path and name for the new resized file 13 | $resizedFile = 'your_path_to_file/resizedFile.png'; 14 | 15 | //call the function (when passing path to pic) 16 | smart_resize_image($file , null, SET_YOUR_WIDTH , SET_YOUR_HEIGHT , false , $resizedFile , false , false ,100 ); 17 | //call the function (when passing pic as string) 18 | smart_resize_image(null , file_get_contents($file), SET_YOUR_WIDTH , SET_YOUR_HEIGHT , false , $resizedFile , false , false ,100 ); 19 | 20 | //done! 21 | ``` 22 | 23 | ## Funcion call (with doc) 24 | 25 | ```php 26 | /** 27 | * easy image resize function 28 | * @param $file - file name to resize 29 | * @param $string - The image data as a string, default is null 30 | * @param $width - new image width 31 | * @param $height - new image height 32 | * @param $proportional - keep image proportional, default is no 33 | * @param $output - name of the new file (include path if needed) 34 | * @param $delete_original - if true the original image will be deleted 35 | * @param $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink 36 | * @param $quality - enter 1-100 (100 is best quality) default is 100 37 | * @return boolean|resource 38 | */ 39 | function smart_resize_image($file, 40 | $string = null, 41 | $width = 0, 42 | $height = 0, 43 | $proportional = false, 44 | $output = 'file', 45 | $delete_original = true, 46 | $use_linux_commands = false, 47 | $quality = 100 48 | ) {code code code... 49 | 50 | ``` 51 | 52 | ### Thanks for all the people adding more functions! keep it going 53 | 54 | ### Enjoy! 55 | 56 | 57 | 58 | 59 | 60 | 61 | ##### License 62 | 63 | Copyright © 2008 Maxim Chernyak 64 | 65 | 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: 66 | 67 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 68 | 69 | 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. 70 | 71 | -------------------------------------------------------------------------------- /issue#1/README.md: -------------------------------------------------------------------------------- 1 | #PHP - smart&easy image resize function with crop position option 2 | 3 | ### due to [issue#1](https://github.com/Nimrod007/PHP_image_resize/issues/6). 4 | 5 | ##How to use : (code snippet) 6 | 7 | ```php 8 | //indicate which file to resize (can be any type jpg/png/gif/etc...) 9 | $file = 'your_path_to_file/file.png'; 10 | 11 | //indicate the path and name for the new resized file 12 | $resizedFile = 'your_path_to_file/resizedFile.png'; 13 | 14 | //call the function with crop from top 15 | smart_resize_image($file , null, SET_YOUR_WIDTH , SET_YOUR_HIGHT , false , $resizedFile , false , false ,100 , true); 16 | 17 | //call the function with crop from center (defualt) 18 | smart_resize_image(null , file_get_contents($file), SET_YOUR_WIDTH , SET_YOUR_HIGHT , false , $resizedFile , false , false ,100, false); 19 | 20 | //done! 21 | ``` 22 | 23 | ##Funcion call (with doc) 24 | 25 | ```php 26 | /** 27 | * easy image resize function 28 | * @param $file - file name to resize 29 | * @param $string - The image data, as a string 30 | * @param $width - new image width 31 | * @param $height - new image height 32 | * @param $proportional - keep image proportional, default is no 33 | * @param $output - name of the new file (include path if needed) 34 | * @param $delete_original - if true the original image will be deleted 35 | * @param $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink 36 | * @param $quality - enter 1-100 (100 is best quality) default is 100 37 | * @param $cropFromTop - if false crop will be from center, if true crop will be from top 38 | * @return boolean|resource 39 | */ 40 | function smart_resize_image($file, 41 | $string = null, 42 | $width = 0, 43 | $height = 0, 44 | $proportional = false, 45 | $output = 'file', 46 | $delete_original = true, 47 | $use_linux_commands = false, 48 | $quality = 100, 49 | $cropFromTop = false 50 | ) { 51 | ) {code code code... 52 | 53 | ``` 54 | -------------------------------------------------------------------------------- /issue#1/resize_image_with_crop_options.php: -------------------------------------------------------------------------------- 1 | = 0 && $transparency < $palletsize) { 75 | $transparent_color = imagecolorsforindex($image, $transparency); 76 | $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); 77 | imagefill($image_resized, 0, 0, $transparency); 78 | imagecolortransparent($image_resized, $transparency); 79 | } 80 | elseif ($info[2] == IMAGETYPE_PNG) { 81 | imagealphablending($image_resized, false); 82 | $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); 83 | imagefill($image_resized, 0, 0, $color); 84 | imagesavealpha($image_resized, true); 85 | } 86 | } 87 | 88 | if ($cropFromTop){ 89 | $cropHeightFinal = 0; 90 | }else{ 91 | $cropHeightFinal = $cropHeight; 92 | } 93 | imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeightFinal, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight); 94 | 95 | 96 | # Taking care of original, if needed 97 | if ( $delete_original ) { 98 | if ( $use_linux_commands ) exec('rm '.$file); 99 | else @unlink($file); 100 | } 101 | 102 | # Preparing a method of providing result 103 | switch ( strtolower($output) ) { 104 | case 'browser': 105 | $mime = image_type_to_mime_type($info[2]); 106 | header("Content-type: $mime"); 107 | $output = NULL; 108 | break; 109 | case 'file': 110 | $output = $file; 111 | break; 112 | case 'return': 113 | return $image_resized; 114 | break; 115 | default: 116 | break; 117 | } 118 | 119 | # Writing image according to type to the output destination and image quality 120 | switch ( $info[2] ) { 121 | case IMAGETYPE_GIF: imagegif($image_resized, $output); break; 122 | case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break; 123 | case IMAGETYPE_PNG: 124 | $quality = 9 - (int)((0.9*$quality)/10.0); 125 | imagepng($image_resized, $output, $quality); 126 | break; 127 | default: return false; 128 | } 129 | 130 | return true; 131 | } 132 | -------------------------------------------------------------------------------- /smart_resize_image.function.php: -------------------------------------------------------------------------------- 1 | = 0 && $transparency < $palletsize) { 79 | $transparent_color = imagecolorsforindex($image, $transparency); 80 | $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); 81 | imagefill($image_resized, 0, 0, $transparency); 82 | imagecolortransparent($image_resized, $transparency); 83 | } 84 | elseif ($info[2] == IMAGETYPE_PNG) { 85 | imagealphablending($image_resized, false); 86 | $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); 87 | imagefill($image_resized, 0, 0, $color); 88 | imagesavealpha($image_resized, true); 89 | } 90 | } 91 | imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight); 92 | 93 | 94 | # Taking care of original, if needed 95 | if ( $delete_original ) { 96 | if ( $use_linux_commands ) exec('rm '.$file); 97 | else @unlink($file); 98 | } 99 | 100 | # Preparing a method of providing result 101 | switch ( strtolower($output) ) { 102 | case 'browser': 103 | $mime = image_type_to_mime_type($info[2]); 104 | header("Content-type: $mime"); 105 | $output = NULL; 106 | break; 107 | case 'file': 108 | $output = $file; 109 | break; 110 | case 'return': 111 | return $image_resized; 112 | break; 113 | default: 114 | break; 115 | } 116 | 117 | # Writing image according to type to the output destination and image quality 118 | switch ( $info[2] ) { 119 | case IMAGETYPE_GIF: imagegif($image_resized, $output); break; 120 | case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break; 121 | case IMAGETYPE_PNG: 122 | $quality = 9 - (int)((0.9*$quality)/10.0); 123 | imagepng($image_resized, $output, $quality); 124 | break; 125 | default: return false; 126 | } 127 | 128 | return true; 129 | } 130 | --------------------------------------------------------------------------------