图像处理之图像拷贝缩略图原理imagecopymergegray、imagecopyresampled

1、imagecopymergegray 用灰度拷贝并合并图像的一部分:

imagecopymergegray($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)。

$img = imagecreatetruecolor(300, 300);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 0, 255));
$jpeg = imagecreatefromjpeg(‘test.jpg’);
imagecopymergegray($img, $jpeg, (imagesx($img)-imagesx($jpeg))/2, (imagesy($img)-imagesy($jpeg))/2, 0, 0, imagesx($jpeg), imagesy($jpeg), 50);
header(‘Content-type:image/jpeg’);
imagejpeg($img);
2、imagecopyresampled缩放图片:

imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)。

代码如下:

$img = imagecreatetruecolor(300, 300);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
$jpeg = imagecreatefromjpeg(‘test1.jpg’);
$jpeg_width = imagesx($jpeg);
$jpeg_height = imagesy($jpeg);
if($jpeg_width>$jpeg_height){
$target_width = 200;
$target_height = (200/$jpeg_width)*$jpeg_height;
}else if($jpeg_width<$jpeg_height){
$target_height = 200;
$target_width = (200/$jpeg_height)*$jpeg_width;
}else{
$target_width = 200;
$target_height = 200;
}
imagecopyresampled($img, $jpeg, (imagesx($img)-$target_width)/2, (imagesy($img)-$target_height)/2, 0, 0, $target_width, $target_height, imagesx($jpeg), imagesy($jpeg));
header(‘Content-type:image/jpeg’);
imagejpeg($img);