Operation file
<?php /*打开图片*/ $src = "https://img.php.cn/upload/course/000/000/004/581454f755fb1195.jpg"; $info = getimagesize($src); $type = image_type_to_extension($info[2],false); $fun = "imagecreatefrom{$type}"; $image = $fun($src); /*操作图片*/ //在内存中建立一个宽300高200的真色彩图片 $image_thumb = imagecreatetruecolor(300,200); //将原图复制到新建的真色彩图片上,并且按照一定比例压缩(参数1:真色彩图片,参数2:原图,参数3,4,5,6:原图和真色彩图的起始点,参数7,8:原图和真色彩图的结束点,参数9:原图宽,参数10:原图高) imagecopyresampled($image_thumb,$image,0,0,0,0,300,200,$info[0],$info[1]); //销毁原始图片 imagedestroy($image); ?>
Code explanation:
First create a color canvas of specified size in the memory.
imagecreatetruecolor() Returns an image identifier, representing an image of size Black image of x_size and y_size.
Using the function imagecopyresampled()
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresampled() copies a square area in one image to another image, inserting pixel values smoothly, Thus, inter alia, the size of the image is reduced while still maintaining great sharpness.
dst_image
Target image connection resource.
src_image
Source image connection resource.
dst_x
Target X coordinate point.
dst_y
Target Y coordinate point.
src_x
The X coordinate point of the source.
src_y
The Y coordinate point of the source.
dst_w
Target width.
dst_h
Target height.
src_w
The width of the source image.
src_h
The height of the source image.
Then destroy the picture copied in the memory.
Next Section