imagecopy重采样裁剪

发布于 2025-01-05 10:01:04 字数 748 浏览 2 评论 0原文

如果我有一张 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.

                  enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2025-01-12 10:01:04

编辑:我认为问题是,您的源大小将使imagecopyresampled缩小。这可能适用于作物:

imagecopyresampled($imagedst,$imagesrc,0,0,280,450,1488,1488,1488,1488)

但是请看这里: http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
我认为你想要的是:

imagecopy($imagedst,$imagesrc,0,0,280,450,1488,1488)

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:

imagecopyresampled($imagedst,$imagesrc,0,0,280,450,1488,1488,1488,1488)

But look here: http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
I think that what you want is:

imagecopy($imagedst,$imagesrc,0,0,280,450,1488,1488)

https://www.php.net/manual/en/function.imagecopy.php

https://www.php.net/manual/en/function.imagecopyresampled.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文