PHP picture Clipping and zooming example (lossless cropping picture)

Source: Internet
Author: User
Tags imagecopy imagejpeg
This article describes the PHP picture clipping and scaling example, not much nonsense, the specific code is as follows:

/* *exif_imagetype--Determine the type of an image * Function Description: function is to cut an image into an image of any size, and keep the image is not deformed * parameter description: Enter the file name to process the picture, generate a new picture of the saved file name, create a new picture of the width of the new image to create a high * /////Get any size image, no stretching in place, no distortion, no blank function 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 ($SR                C_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 proportion of width is smaller than the height direction, cut or enlarge according to the height proportion standard} else {$ratio = $ratio _w;            }//define an intermediate temporary image, 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 $ratio scale for the target image//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://Storage image imagejpeg ($new _img, $dst _file, 100);                  Break                    Case Imagetype_png:imagepng ($new _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 flat image is enlarged, then cropped//=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 a large middle image, the image is equal to the height or width of the target image, and then enlarge the original $inter _w = (int) ($W * $ratio);            $inter _h = (int) ($H * $ratio);            $inter _img = Imagecreatetruecolor ($inter _w, $inter _h);            Scale the original image to crop imagecopyresampled ($inter _img, $src _img, 0, 0, 0, 0, $inter _w, $inter _h, $w, $h);            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://Storage image imagejpeg ($new _img, $dst _file, 100);                  Break         Case Imagetype_png:imagepng ($new _img, $dst _file, 100);                  Break                    Case Imagetype_gif:imagegif ($new _img, $dst _file, 100);       Break           Default:break; }}//if3}//End Function//output new picture image_resize (' test.jpg ', ' demo.jpg ', ' 1200px ', ' 1200px ');

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.