Code case for customizing Image Center clipping function in PHP

Source: Internet
Author: User
Tags imagecopy imagejpeg
This paper describes the custom Image Center clipping function implemented by PHP. Share to everyone for your reference, as follows:

The general idea of Image center cut:

1. First zoom the image so that the scaled image can cover the cut area exactly. (imagecopyresampled-resample Copy part of image and resize)

2. Place the zoomed image in the middle of the clipping area. (Part of the imagecopy-copy image)

3. Cut the image and save it. (Imagejpeg | Imagepng | imagegif-output image to browser or file)

Specific code:


================== Zoom clipping Function ====================/** * Center crop picture * @param string $source [Original path] * @param int $width [set width] * @pa RAM int $height [Set height] * @param string $target [target path] * @return bool [clipping result] */function Image_center_crop ($source, $width,  $height, $target) {if (!file_exists ($source)) return false;      /* Load image by Type */switch (Exif_imagetype ($source)) {case Imagetype_jpeg: $image = Imagecreatefromjpeg ($source);    Break      Case Imagetype_png: $image = Imagecreatefrompng ($source);    Break      Case Imagetype_gif: $image = Imagecreatefromgif ($source);  Break  } if (!isset ($image)) return false;  /* Get image size information */$target _w = $width;  $target _h = $height;  $source _w = Imagesx ($image);  $source _h = Imagesy ($image);  /* Calculate clipping width and height */$judge = (($source _w/$source _h) > ($target _w/$target _h)); $resize _w = $judge?  ($source _w * $target _h)/$source _h: $target _w; $resize _h =! $judge?  ($source _h * $target _w)/$source _w: $target _h; $start _x = $judge? ($resize_w-$target _w)/2:0; $start _y =! $judge?  ($resize _h-$target _h)/2:0;  /* Draw Center Zoom image */$resize _img = Imagecreatetruecolor ($resize _w, $resize _h);  Imagecopyresampled ($resize _img, $image, 0, 0, 0, 0, $resize _w, $resize _h, $source _w, $source _h);  $target _img = Imagecreatetruecolor ($target _w, $target _h);  Imagecopy ($target _img, $resize _img, 0, 0, $start _x, $start _y, $resize _w, $resize _h);  /* Save the picture to file */if (!file_exists (DirName ($target))) mkdir (DirName ($target), 0777, true);      Switch (Exif_imagetype ($source)) {case Imagetype_jpeg:imagejpeg ($target _img, $target);    Break      Case Imagetype_png:imagepng ($target _img, $target);    Break      Case Imagetype_gif:imagegif ($target _img, $target);  Break }//return Boolval (file_exists ($target))//php5.5 the Boolval () function is used to get the returned Boolean value return File_exists ($target)? true:false;// Compatible with low-version PHP notation}


The ================== function uses the way ====================//the original picture $source = ' ... /source/img/middle.jpg '; $width = 480; Cropped width $height = 480;//cropped height//cropped picture storage directory $target = ' ... /source/temp/resize.jpg ';//Save to target folder if (Image_center_crop ($source, $width, $height, $target)) {  echo ' original 1440* 900:  ';  echo "

Operating effect:

The original 1440*900 is:

After the modified picture 480*480:

Similarly, 480*320, 800*600, and other dimensions of the image only need to modify the corresponding parameters.

Attached: Problems encountered in code testing

Error: Call an undefined function exif_imagetype ()

Workaround:

Open extensionextension=php_exif.dll

and extension=php_mbstring.dll put it to the extension=php_exif.dll front

another: boolval() function is a function that can be used above the PHP5.5 version , this article tests the code for a compatible low version, using the following statement instead:


return file_exists ($target)? True:false;
Related Article

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.