PHP zoom processing png and JPG images

Source: Internet
Author: User
Tags imagecopy

This example shows the use of PHP's own GD library to zoom in and out of PNG and JPG images

$target _width = 120;   Target image width $target_height = 150;     Target image Height $target_img = ' me.jpg '; Cut picture Deal ($target _width, $target _height, $target _img);/** * Get picture information, prepare parameters */function deal ($target _width, $target _  Height, $target _img) {$img _info=getimagesize ($target _img);       Get the original size $original _width= $img _info[0];       Original picture width $original _height= $img _info[1];    Original image height $original _mime= $img _info[' mime ');       $type =substr ($original _mime,6); The original $original_mime value is ' image/type ', the image type is obtained by capturing from the sixth character $target _scale = $target _height/$target _width; The aspect ratio of the target image $original _scale = $original _height/$original _width;        The original picture aspect ratio if ($original _scale>= $target _scale) {//too high $w = Intval ($src _w);        $h = intval ($target _scale* $w);        $x = 0;    $y = ($original _height-$h)/3;        } else {//over-width $h = intval ($original _height);        $w = Intval ($h/$target _scale);        $x = ($original _width-$w)/2;    $y = 0; } switch ($type) {case 'JPEG ': dealjpg ($target _img, $w, $h, $x, $y, $original _width, $original _height, $target _width, $target _height);        Break Case ' png ': dealpng ($target _img, $w, $h, $x, $y, $original _width, $original _height, $target _width, $target _heigh            T);        Break            Default:echo "Please confirm that you need to handle the correct image type";    Break }}/** * processing jpg */function dealjpg ($target _img, $w, $h, $x, $y, $original _width, $original _height, $target _width, $target _he  ight) {//Trim $source =imagecreatefromjpeg ($target _img);      Create a new Image $croped =imagecreatetruecolor ($w, $h); Create a new True color image imagecopy ($croped, $source, 0, 0, $x, $y, $original _width, $original _height);    Copy part of image//Zoom $scale = $target _width/$w;   $target = Imagecreatetruecolor ($target _width, $target _height);    Create a new True color image $final _w = intval ($w * $scale);    $final _h = intval ($h * $scale);    Imagecopyresampled ($target, $croped, 0, 0, 0, 0, $final _w, $final _h, $w, $h); Resample copy part of image and resize//Save $timestamp = time ();    Imagejpeg ($target, "$timestamp. jpg"); Imagedestroy ($target);} /** * Handles PNG */function dealpng ($target _img, $w, $h, $x, $y, $original _width, $original _height, $target _width, $target _    Height) {//Trim $source =imagecreatefrompng ($target _img);      $croped =imagecreatetruecolor ($w, $h); Create a new True color image imagecopy ($croped, $source, 0, 0, $x, $y, $original _width, $original _height);    Copy part of image//Zoom $scale = $target _width/$w;   $target = Imagecreatetruecolor ($target _width, $target _height);    Create a new True color image $final _w = intval ($w * $scale);    $final _h = intval ($h * $scale);    Imagecopyresampled ($target, $croped, 0, 0, 0, 0, $final _w, $final _h, $w, $h);    Resample copy part of image and resize/save $timestamp = time ();    Imagepng ($target, "$timestamp. png"); Imagedestroy ($target);}

  

PHP zoom processing png and JPG 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.