Two function comparisons for PHP image scaling
PHP scales images in two ways: the imagecopyresized () function works in all GD versions, but its algorithm for scaling images is coarser. Imagecopyresamples (), the pixel interpolation algorithm obtains the image edge is smooth, the quality is good, but the function speed is slower than imagecopyresized (). 01if($src _h){02$thumb _w=$size;03$thumb _h=intval($src _h/$src _w*$size);04}Else{05$thumb _h=$size;06$thumb _w=intval($src _w/$src _h*$size);07}08 09$thumb= Imagecreatetruecolor ($thumb _w,$thumb _h);10//old method, but produced a very poor quality effect11//imagecopyresized ($thumb, $src, 0, 0, 0, 0, $thumb _w, $thumb _h, $src _w, $src _h);12imagecopyresampled ($thumb,$src, 0, 0, 0, 0,$thumb _w,$thumb _h,$src _w,$src _h);13 14$file=Array_pop(Explode("/",$filename));15 16imagejpeg ($thumb, "/tmp/thumb/i-$size-".$file); The parameters of the two function are the same. as follows: imagecopyresampled (dest, SRC,DX,DY,SX,SY,DW,DH,SW,sh); imagecopyresized (dest, SRC,DX,DY,SX,SY,DW,DH,SW,sh); All two of them are crawled from the original image (source) to a specific location (SX, SY) Copies the image Qu region to a specific location (Dx,dy) of the target T image (destination). In addition DW,DH Specifies the size of the copied image area on the target image, Sw,sh specifies the size of the image area copied from the original image. If you have experience with PS, it is equivalent to select an area in the original image, cut to move to the destination image, while there are stretching or shrinking operations. The main difference between writing imagecopyresampled and imagecopyresized in the PHP manual is to make him look better, closer to the primary color: imagecopyresampled () copies a rectangular portion of one image to another image, smoothly interpolating pixel values?? So, in particular,reducing the difference between the size of an image still retains a great deal of clarityimagecopyresampled () and imagecopyresized () is obvious, with this You don't need to think about using imagecopyresized (), unless you're using GIF, only imagecopyresampled can see it. This example displays the image in the original One-fourth size. 01<?PHP02//specify file path and scale03$filename= ' Test.jpg ';04$percent= 0.5;05//specify the header file content Typezhi value06Header(' Content-type:image/jpeg ');07//Get the width height of the picture08List($width,$height) =getimagesize($filename);09$newwidth=$width*$percent;10$newheight=$height*$percent;11//Create a picture. Receive parameters are width-height, which returns the generated resource handle12$thumb= Imagecreatetruecolor ($newwidth,$newheight);13//Gets the source file resource handle. Receive parameter is picture path, return handle14$source= Imagecreatefromjpeg ($filename);15//cut the source file all fields and zoom out onto the target picture. Top two resource handles16imagecopyresampled ($thumb,$source, 0, 0, 0, 0,$newwidth,$newheight,$width,$height);17//Output to Browser18imagejpeg ($thumb);19?>This example will take an image at the widest or highest200pixels to display. View Sourceprint? 01<?PHP02//file path03$filename= ' Test.jpg ';04//maximum width and height05$width= 200;06$height= 200;07//Set HTTP header content Type value08Header(' Content-type:image/jpeg ');09//Get picture Width height10List($width _orig,$height _orig) =getimagesize($filename);11if($width&& ($width _orig<$height _orig))12{ //High-200,kuan, height-to-width-proportional reduction13$width= ($height/$height _orig) *$width _orig;14}Else {15$height= ($width/$width _orig) *$height _orig;16}17//change the size. As in the previous example. 18$image _p= Imagecreatetruecolor ($width,$height);19$image= Imagecreatefromjpeg ($filename);20imagecopyresampled ($image _p,$image, 0, 0, 0, 0,$width,$height,$width _orig,$height _orig);21st//Output22imagejpeg ($image _p,NULL, 100)23?>
Imagecopyresamples ()