PHP cropping without affecting the shape of the image

Source: Internet
Author: User
Tags imagecopy imagejpeg
This time for everyone to bring PHP without affecting the shape of the picture on the premise of clipping, PHP does not affect the shape of the picture under the premise of the attention of the cutting, the following is the actual case, together to see.

Functional Requirements:
After the image is uploaded, it is automatically cropped, and then the cropped picture is displayed in the foreground.

<? *exif_imagetype--Determine the type of an image * Description: function function is to cut an image into an image of any size, the image does not deform * parameter description: Enter the file name to process the picture, generate a new picture of the filename, create a new image width, create a new picture of the high *//get any Size image, insufficient place to stretch, no distortion, no left blank function my_image_resize ($src _file, $dst _file, $new _width, $new _height) {$new _width= intval ($ new_width); $new _height=intval ($new _width);  if ($new _width <1 | | $new _height <1) {echo "params width or height error!"; exit ();} if (!file_exists ($src _file)) { Echo $src _file. "is NOT exists!"; Exit (); }//Image type $type =exif_imagetype ($src _file); $support _type=array (Imagetype_jpeg, Imagetype_png, imagetype_gif); if (!in_array ($type, $support _type,true)) {echo "This type of the image does not support! Only support JPG, GIF or PNG "; Exit (); }//load Image switch ($type) {case imagetype_jpeg: $src _img=imagecreatefromjpeg ($src _file), break, Case imagetype_png: $src _img=imagecreatefrompng ($src _file); Break Case Imagetype_gif: $src _img=imagecreatefromgif ($src _file); Break Default:echo "Load image error!"; Exit (); } $w =imagesx ($src _img); $H=imagesy ($src _img); $ratio _w=1.0 * $new _width/$w; $ratio _h=1.0 * $new _height/$h; $ratio = 1.0; The resulting image of the aspect ratio of the original is small, or large, the principle is to take a large proportion of magnification, the large proportion of the reduction (smaller proportion) if (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1)) {if ($ratio _w < $ratio _h) {$ratio = $ratio _h;//Case one, the ratio of width is smaller than the height direction, crop or enlarge according to the height scale criterion}else {$ratio = $ratio _w;}//Definition An intermediate temporary image in which the aspect ratio of the image meets the target requirements $inter _w= (int) ($new _width/$ratio); $inter _h= (int) ($new _height/$ratio); $inter _img=imagecreatetruecolor ($inter _w, $inter _h); Var_dump ($inter _img); Imagecopy ($inter _img, $src _img, 0,0,0,0, $inter _w, $inter _h); Generate a temporary image with a maximum edge length of the target image $ratio scale//define a new image $new _img=imagecreatetruecolor ($new _width, $new _height); Var_dump ($new _img); exit (); Imagecopyresampled ($new _img, $inter _img,0,0,0,0, $new _width, $new _height, $inter _w, $inter _h); Switch ($type) {case Imagetype_jpeg:imagejpeg ($new _img, $dst _file,100);//store image break; case Imagetype_png:imagepng ($ne W_img, $dst _file,100); Break Case Imagetype_Gif:imagegif ($new _img, $dst _file,100); Break Default:break; }}//End If 1//2 one edge of the target image is larger than the original, one edge is smaller than the original, then the plain image is enlarged, then the crop//=if (($ratio _w < 1 && $ratio _h > 1) | | ($ratio _w >1 && $ratio _h <1)) else{$ratio = $ratio _h> $ratio _w? $ratio _h: $ratio _w;//Take a large proportion of that value//define an intermediate large image that is equal in height or width of the target image and then enlarge $inter _w= (int) for the original image ($W * $ratio); $inter _h= (int) ($H * $ratio); $inter _img=imagecreatetruecolor ($inter _w, $inter _h); Crop imagecopyresampled ($inter _img, $src _img,0,0,0,0, $inter _w, $inter _h, $w, $h) after scaling the original image; Define a new Image $new _img=imagecreatetruecolor ($new _width, $new _height); Imagecopy ($new _img, $inter _img, 0,0,0,0, $new _width, $new _height); Switch ($type) {case Imagetype_jpeg:imagejpeg ($new _img, $dst _file,100);//store image break; case Imagetype_png:imagepng ($ne W_img, $dst _file,100); Break Case Imagetype_gif:imagegif ($new _img, $dst _file,100); Break Default:break; }}//if3}//end functionmy_image_resize (' test.gif ', ' 11111.gif ', ' 100px ', ' 100px ');? >

Summary of the small series:
The above implementation of the PHP image clipping function, flexible application of the PHP GD library related functions, including Imagecreatefromjpeg, Imagecreatefrompng, Imagecreatefromgif, The use of functions such as Imagecreatetruecolor.
Therefore, the flexible grasp of the PHP GD Library of the use of functions, for cutting pictures, watermarks, thumbnails and other functions of the implementation of the important.

Read more: PHP GD Library generated thumbnails of the basic knowledge of the PHP GD library The use of the GD function in the PHP GD library to generate high quality thumbnails (sample) PHP GD library functions Daquan (collection) PHP using the GD library to implement the example code of the screenshot PHP add g D Library Support steps in detail PHP5 installation of the GD library steps to share the whole process of installing the PHP GD library in Linux under windows open PHP GD Library method php open GD Library (Windows environment) PHP GD library dynamic generation of line chart instance code PHP GD Library Drawing example code for 24-hour histogram PHP verification code (GD Library Generation Verification Code) examples of PHP GD library upload images and create thumbnails of code PHP GD library generated verification Code instance

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Delete line breaks when reading a file by line detailed instructions

PHP GD library for service-side image clipping and thumbnail preview function

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.