imagecopy重采样裁剪
如果我有一张 2048 x 2048 的图像,并且我想要一张 1488x1488 的图像,从顶部向下 450 像素,从左侧 280 像素,
则正确的代码 x.png 是 2048 x 2048 图片:
<?php
$imagesrc_location = 'x.png';
// Get new sizes
list($srcwidth, $srcheight) = getimagesize($imagesrc_location);
$imagedst = imagecreatetruecolor(1488, 1488);
$imagesrc = imagecreatefrompng($imagesrc_location);
if (imagecopyresampled($imagedst,$imagesrc,0,0,280,450,1488,1488,2048,2048)) {
// Output image
header('Content-type: image/png');
imagepng($imagedst);
} else {
echo "Could not resize file";
}
这是一张显示我想要的图片,灰色部分是裁剪后的图片。
If I have an image 2048 x 2048 and I would like an image 1488x1488 450 pixels down from top and 280 pixels from left
is this the right code x.png is the 2048 x 2048 picture:
<?php
$imagesrc_location = 'x.png';
// Get new sizes
list($srcwidth, $srcheight) = getimagesize($imagesrc_location);
$imagedst = imagecreatetruecolor(1488, 1488);
$imagesrc = imagecreatefrompng($imagesrc_location);
if (imagecopyresampled($imagedst,$imagesrc,0,0,280,450,1488,1488,2048,2048)) {
// Output image
header('Content-type: image/png');
imagepng($imagedst);
} else {
echo "Could not resize file";
}
Here is a picture showing what I want, the grey part is the cropped picture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:我认为问题是,您的源大小将使
imagecopyresampled
缩小。这可能适用于作物:但是请看这里: http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
我认为你想要的是:
https://www.php.net/手册/en/function.imagecopy.php
https://www.php.net/manual/en/function.imagecopyresampled.php
EDIT: I think the problem is, your source size will make the
imagecopyresampled
scale it down. This may work for a crop:But look here: http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
I think that what you want is:
https://www.php.net/manual/en/function.imagecopy.php
https://www.php.net/manual/en/function.imagecopyresampled.php