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