Php and Other proportional scaling images and Cutting Image Code sharing,

Source: Internet
Author: User
Tags imagejpeg

Php and Other proportional scaling images and Cutting Image Code sharing,

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:

  1. MyImageResize ($ filename, 200,200); // maximum width and height
  2. MyImageResize ($ filename, 200,200, 'width'); // fixed width
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.