Home > Article > Backend Development > Simple PHP image scaling code_PHP tutorial
I used getimagesize to get the size of the original image and then x0.5 to divide the image/5.
array getimagesize ( string $filename [, array &$imageinfo ] )
The getimagesize() function will determine the size of any gif, jpg, png, swf, swc, ps tutoriald, tiff, bmp, iff, jp2, jpx, jb2, jpc, xbm or wbmp image file and return the image size and file type and a height/width text string that can be used in an tag in a normal html file.
If the image specified by filename cannot be accessed or is not a valid image, getimagesize() will return false and generate an e_warning level error
*/
//Define a file
$filename='1.jpg';
$percent=0.5;
//Send header file
header('content-type: image/jpeg');
//Get the size of the image
list($width,$height)=getimagesize($filename);
//Define new size
$new_width=$width * $percent;
$new_height=$height * $percent;
$image_p=imagecreatetruecolor($new_width, $new_height);
/*
: int imagecreate(int x_size, int y_size);
Return value: integer
Content Description
This function is used to create a completely empty graph. The parameters x_size and y_size are the size of the graphic, in pixels.
*/
$image=imagecreatefromjpeg($filename);
/*
resource imagecreatefromjpeg ( string filename ) Imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename. Imagecreatefromjpeg() returns an empty string on failure and prints an error message, which unfortunately displays as a broken link in the browser. To ease debugging the following example will produce an error jpeg:
*/
imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
/*
):imagecopyresamples(), the image edges obtained by its pixel interpolation algorithm are smoother and of better quality (but the speed of this function is slower than imagecopyresized()). The parameters of the two functions are the same. As follows: imagecopyresampled(dest,src, dx,dy,sx,sy,dw,dh,sw,sh);
*/
//Output image
imagejpeg($image_p, null, 100);
/*
Executing this code will scale the original image by 50% and output it as a new image
*/