gpt4 book ai didi

php - PHP 中的 imagecopyresampled,有人可以解释一下吗?

转载 作者:可可西里 更新时间:2023-10-31 23:54:00 24 4
gpt4 key购买 nike

好吧,我以为我理解这个功能,但我对这个有一个完整的心理障碍。

我想从 800x536 的照片创建尺寸为 75x75 的裁剪缩略图。

imagecopyresampled 函数有 10 个可能的参数。我首先尝试了这个:

// Starting point of crop
$tlx = floor(($width / 2) - ($new_width / 2)); //finds halfway point of big image and subtracts half of thumb.
$tly = floor(($height / 2) - ($new_height / 2)); //gets centre of image to be cropped.

imagecopyresampled($tmp_img,$img,0,0,$tlx,$tly,$new_width,$new_height,$orig_width,$orig_height);

这会找到大图像中途标记的任一侧并将其裁剪掉。或者我想。但它实际上裁剪了一些图片并使右侧和底部保持黑色(大概来自之前的 imagecreatetruecolor。

所以我找到了一种方法来做我想做的事,但我希望你解释一下它是如何工作的。

我现在有:

//Create thumbnails.
$new_width = 75; //pixels.
$new_height = 75;

if($width > $height) $biggest_side = $width;
else $biggest_side = $height;

//The crop size will be half that of the largest side
$crop_percent = .5;
$crop_width = $biggest_side*$crop_percent;
$crop_height = $biggest_side*$crop_percent;

$c1 = array("x"=>($width-$crop_width)/2, "y"=>($height-$crop_height)/2);

//Create new image with new dimensions to hold thumb
$tmp_img = imagecreatetruecolor($new_width,$new_height);

//Copy and resample original image into new image.
imagecopyresampled($tmp_img,$img,0,0,$c1['x'],$c1['y'],$new_width,$new_height,$crop_width,$crop_height);

它做得很完美,缩小图像然后从中间裁剪掉,但我的数学不是很敏锐,而且我认为这肯定是我不完全理解 imagecopyresampled 函数。

谁能帮我解决一下?逐个参数。特别是最后两个。最初我输入了原始图像的宽度和高度,但这输入了 400 和 400(最长边的一半)。对不起的咆哮。希望我的头脑能尽快理解这一点:)

亚历克斯

最佳答案

它相当简单,已记录 here

参数:

1) $dst_image,一个有效的 GD 句柄,表示要复制到的图像
2) $src_image,一个有效的 GD 句柄,表示您要从中复制的图像

3) $dst_x - 要将重采样图像放入目标图像中的 X 偏移
4) $dst_y - Y 偏移量,同上

5) $src_x - 您要从中开始复制的源图像中的 X 偏移量
6) $src_y - Y 偏移量,同上

7) $dst_x - $dst_image 中新重采样图像的 X 宽度
8) $dst_y - Y 宽度,同上

9) $src_x - 要从 $src_image 中复制出来的区域的 X 宽度
10) $src_y - Y 宽度,同上

所以...

你有一个 800x536 的 $src_image 和一个 75x75 的 $dst_image

       $width = 800                                $new_width = 75
+-----------------------+ +----+
| | | |
| | | | $new_height = 75
| | $height = 536 +----+
| |
| |
+-----------------------+

听起来您想获取源图像的​​中间 block 并从中制作缩略图,对吗?这个中间 block 应该代表原始图像高度和宽度的一半,所以你想要:

$start_X = floor($width / 4); //  200
$width_Y = floor($height / 4); // 134

200 400 200
+-----------------------+
| | | | 134
|-----+----------+------|
| | This part| | 268
|-----+----------+------|
| | | | 134
+-----------------------+

$end_x = $start_X + (2 * $start_x) // 3 * $start_x = 600
$end_y = $start_Y + (2 * $start_y) // 3 * $start_y = 402

imagecopyresampled($src, $dst, 0, 0, $startX, $start_y, 75, 75, $end_x, $end_y);
a b c d e f g h

a,b - 开始将新图像粘贴到目标图像的左上角
c,d - 在 200,134 处开始从原始图像中吸取像素
e,f - 将调整后的图像设为 75x75(填满缩略图)
g,h - 停止复制原始图像中 600x402 处的像素

现在,假设您希望缩略图完全填满。如果您希望源图像按比例缩小(因此它具有与原始图像相同的高度/宽度比例,那么您必须进行一些数学运算来调整 a,be,f 参数。

关于php - PHP 中的 imagecopyresampled,有人可以解释一下吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3604940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com