Php and other proportional scaling images and cutting Image code sharing ,. Php and other proportional scaling image and cutting Image code sharing, php and other proportional scaling image and cutting Image code sharing *** image scaling function (fixed height can be set, proportional scaling of images, such as fixed width or Max php, and sharing of cut Image code,
Php and other proportional scaling image and cut Image code sharing
/*** Image scaling function (fixed height, fixed width or maximum width and height can be set. gif, jpg, and png formats are supported.) * Author: specs ** @ param string $ source_path source image * @ param int $ target_width target width * @ param int $ target_height target height * @ param string $ fixed_orig Lock width/height (optional parameter width, height or null value) * @ return string */function myImageResize ($ source_path, $ target_width = 200, $ target_height = 200, $ fixed_orig = '') {$ source_info = getimagesize ($ source_path ); $ source_width = $ source_info [0]; $ source_height = $ source_info [1]; $ source_mime = $ source_info ['Mime ']; $ ratio_orig = $ source_width/$ source_height; if ($ fixed_orig = 'width') {// fixed width $ target_height = $ target_width/$ ratio_orig;} elseif ($ fixed_orig = 'height ') {// fixed height $ target_width = $ target_height * $ ratio_orig;} else {// maximum width or maximum height if ($ target_width/$ target_height> $ ratio_orig) {$ target_width = $ target_height * $ ratio_orig;} else {$ target_height = $ target_width/$ ratio_orig;} switch ($ source_mime) {case 'image/GIF ': $ source_image = imagecreatefromgif ($ source_path); break; case 'image/jpeg ': $ source_image = imagecreatefromjpeg ($ source_path); break; case 'image/png ': $ source_image = hour ($ source_path); break; default: return false; break;} $ target_image = hour ($ target_width, $ target_height); imagecopyresampled ($ target_image, $ source_image, 0, 0, 0, 0, $ target_width, $ target_height, $ source_width, $ source_height); // header ('content-type: image/jpeg '); $ imgArr = explode ('. ', $ source_path); $ target_path = $ imgArr [0]. '_ new. '. $ imgArr [1]; imagejpeg ($ target_image, $ target_path, 100 );}
Usage:
- MyImageResize ($ filename, 200,200); // maximum width and height
- MyImageResize ($ filename, 200,200, 'width'); // fixed width
- MyImageResize ($ filename, 200,200, 'height'); // fixed height
Cut the image to a fixed size:
Function imagecropper ($ source_path, $ target_width, $ target_height) {$ source_info = histogram ($ source_path); $ source_width = $ source_info [0]; $ source_height = $ source_info [1]; $ source_mime = $ source_info ['Mime ']; $ source_ratio = $ source_height/$ source_width; $ target_ratio = $ target_height/$ target_width; // if ($ source_ratio> $ target_ratio) {$ cropped_width = $ source_width; $ cropped_height = $ source_width * $ target_ratio; $ source_x = 0; $ source_y = ($ source_height-$ cropped_height)/2;} elseif ($ source_ratio <$ target_ratio) {// the source image is too wide $ cropped_width = $ source_height/$ target_ratio; $ cropped_height = $ source_height; $ source_x = ($ source_width-$ cropped_width)/2; $ source_y = 0;} else {// moderate Source image $ cropped_width = $ source_width; $ cropped_height = $ source_height; $ source_x = 0; $ source_y = 0;} switch ($ source_mime) {case 'image/GIF': $ source_image = imagecreatefromgif ($ source_path ); break; case 'image/jpeg ': $ source_image = imagecreatefromjpeg ($ source_path); break; case 'image/png': $ source_image = imagecreatefrompng ($ source_path); break; default: return false; break;} $ target_image = imagecreatetruecolor ($ target_width, $ target_height); $ cropped_image = crop ($ cropped_width, $ cropped_height); // crop imagecopy ($ cropped_image, $ source_image, 0, 0, $ source_x, $ source_y, $ cropped_width, $ cropped_height); // scale imagecopyresampled ($ target_image, $ cropped_image, 0, 0, 0, 0, 0, $ target_width, $ target_height, $ cropped_width, $ cropped_height); $ dotpos = strrpos ($ source_path ,'. '); $ imgName = substr ($ source_path, 0, $ dotpos); $ suffix = substr ($ source_path, $ dotpos); $ imgNew = $ imgName. '_ small '. $ suffix; imagejpeg ($ target_image, $ imgNew, 100); imagedestroy ($ source_image); imagedestroy ($ target_image); imagedestroy ($ cropped_image );}
Articles you may be interested in:
- Example of image proportional scaling in PHP
- Php scaling image (proportional scaling based on width and height) example
- PHP adds the watermark function for image uploading and proportional scaling.
- Function sharing for generating thumbnails by proportional scaling of PHP images
- Php achieves proportional scaling of the specified size to generate a thumbnail for the uploaded image
- Php code for proportional scaling of images
- How to achieve proportional scaling of php images
Shares, php, and other proportional scaling images and cut Image code sharing/*** image scaling function (fixed height, fixed width, or maximum width can be set...