PHP-scaling and cropping

The imagecopyresampled() function
will sample a certain part of a resource to another image resource

<?php
header('content-type:image/jpeg');
$img= imgecreatefromjieg('images/zcx.jpg);	
$img1=imagecreatetruecolor(500,500);

imagecopyresampled($img1,$imge,100,100,0,0,200,200,200,200);
/*
$img1 : 目标图像
$img : 采样图像(素材图像)
100,100 :采样图像在目标图像的位置(坐标)
0,0 : 表示采样的点,从这点开始采样素材图像
200,200 :采样后的图像放到布标图像上的尺寸
200,200 :采样图像的长和高
就是在素材图像中,从(0,0)坐标点采样了一个长和宽都为200的图像,然后以长款都为200的比例放在了位置为(100,100)的 原图中
*/

imagejpeg($img1);

imgdestroy($img);
imgdestroy($img);

Proportional zoom

<?php
header('content-type:image/jpeg');
$img= imgecreatefromjieg('images/zcx.jpg);	

$img_width = imagesx($img);
$img_height = imagesy($img);
//获得素材图像的长和宽

$width = 150;		//设置缩放后的长度
//$width / $height =  $img_width / $img_height

$height = $width / ($img_width / $img_height);

$img1 = imagecreatetruecolor($width,$height);			//让画布和缩放的采样图像一样大

imagecopyresampled($img1,$imge,100,100,0,0,$width,$height,$img_width,$img_height);

//还可以对缩放后的图像进行保存,且缩放后照片所占内存会缩小
if(imagejpeg($img1)
{
	imagejpeg($img1,'images/zoom_zcx.jpg');
}

imagejpeg($img1);

imgdestroy($img);
imgdestroy($img);

Guess you like

Origin blog.csdn.net/qq_41076531/article/details/100089239