Php image scaling implementation
This article mainly introduces the php image scaling implementation method. For more information, see
Php basic exercises-Image Scaling:
The Code is as follows:
<? Php
/**
* Image zoom.
*/
Function imageZoom ($ filename, $ w, $ h ){
/* Arguments meaning */
/* $ Filename: the source of the name */
/* $ W: you want get the image's width */
/* $ H: you want get the imgage's height */
$ Arr = getimagesize ($ filename );
$ Src_w = $ arr [0];
$ Src_h = $ arr [1];
$ Src_t = $ arr [2];
/* 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (intel byte order ),
= TIFF (motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC,
= IFF, 15 = WBMP, 16 = XBM */
$ Src_m = $ arr ['mime '];
$ Src_img = imagecreatefromjpeg ($ filename );
If ($ w/$ src_w)> ($ h/$ src_h )){
$ Bili = $ h/$ src_h;
} Else {
$ Bili = $ w/$ src_h;
}
$ Dst_w = $ src_w * $ bili;
$ Dst_h = $ src_h * $ bili;
$ Dst_img = imagecreatetruecolor ($ dst_w, $ dst_h );
Imagecopyresampled ($ dst_img, $ src_img, 0, 0, 0, $ dst_w, $ dst_h, $ src_w, $ src_h );
Header ("content-type: {$ src_m }");
Switch ($ src_t ){
Case 1:
$ Imgout = "imagegif ";
Break;
Case 2:
$ Imgout = "imagejpeg ";
Break;
Case 3:
$ Imgout = "imagepng ";
Break;
Default:
Echo "The type was wrong! ";
Break;
}
$ Dst_filename = "s _". $ filename;
$ Imgout ($ dst_img, $ dst_filename );
Imagedestroy ($ dst_img );
}
$ Filename = 'gg.jpg ';
ImageZoom ($ filename, 100,200 );
Core: <1> pay attention to how the zoom ratio is obtained. Although the image may be slightly different from the Expected One, the zoom ratio is guaranteed at least.
<2> type control.