【问题标题】:How can I crop an image properly with php?如何使用 php 正确裁剪图像?
【发布时间】:2015-11-18 12:09:14
【问题描述】:

我正在尝试在PHP 上裁剪图像。我看到PHPimagecrop 上有一个功能可以做到这一点。这是我尝试裁剪图像的代码。

$image = imagecreatefromjpeg(//Path of the image);
$croppedImage = imagecrop($image, array("x"=>0,"y"=>0,"width"=>100,"height"=>100));
imagejpeg($croppedImage, //Path in which the image will be stored); 

在这里,我希望图像的裁剪从图像的左角开始,并获得我放在上面的宽度和高度值。

但它只是调整我的图像大小,而不是裁剪它。我做错了什么?

编辑:

我也尝试了函数imagecopyresampled。这是我尝试过的:

dst_image(Destination image link resource) = newImage; //Here the new image that I want to create.

src_image(Source image link resource) = image; //Here the original image.

//Here 0,0 because I want that the new image crop starts in the left corner of the original image.

dst_x(x-coordinate of destination point) = 0;

dst_y(y-coordinate of destination point) = 0;

//Here 0,0 because I want that the crop starts on the 0,0 of the original image

src_x(x-coordinate of source point) = 0;

src_y(y-coordinate of source point) = 0;

dst_w(Destination width) = 150; //The new width of the crop image.

dst_h(Destination height)  = 150; //The new height of the crop image.

src_w(Source width) = 500; //The original width of the image.

src_h(Source height) = 500; //The original height of the image.

所以最后函数会是这样的:

$b = imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w , $dst_h , $src_w , $src_h ); 

我只是在这个函数中遇到问题,这就是为什么要避免其余部分(保存和 imagetruecolor 以及其余部分......)

这个函数给了我预期的结果,但新图像是黑色的,为什么?

提前致谢!

【问题讨论】:

  • 您在此处向我们展示的代码应该给出您正在寻找的结果,而不是您描述的结果(前提是图像大于 100x100)。我建议您检查您的方法。
  • @symcbean 我不明白你的意思。在这里,我以 100x100 为例(图像比 100x100 更大),但在我的真实代码中,我使用变量动态地使用它。这里我只是简化一下。

标签: php image


【解决方案1】:

试试这个:

$dst_x = 0;   // X-coordinate of destination point
$dst_y = 0;   // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // $src_x + $dst_w Crop end X position in original image
$src_h = 220; // $src_y + $dst_h Crop end Y position in original image

// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/cropped_whatever.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');

【讨论】:

  • 我用你放在这里的代码来调整图像的大小,但没有裁剪它。
  • 你试过我给出的代码了吗?首先在测试文件中尝试此代码
  • 你能解释一下你为什么要$src_x + $dst_w吗?因为我无法得到预期的结果:S
  • 你的意思是$src_w变量值? $src_x 是原始图像的起始 x 位置,$dst_w 是我们要裁剪的拇指宽度。我们还需要 x 位置的结尾,因此我们使用$src_x + $dst_w 来获取我们想要停止裁剪图像的结束位置。
【解决方案2】:

您也可以使用imagick 的图像裁剪功能。

【讨论】:

  • 是的,但我不能使用库:(
【解决方案3】:

最后,我得到了我的问题的解决方案。我必须输入相同的 dst_wsrc_w 值。与dst_hsrc_h 相同。 ¡¡而且有效!!

最终解决方案如下:

$b = imagecopyresampled ($dst_image, $src_image, 0, 0, 0, 0, 150, 150, 150, 150);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2017-04-05
    • 2011-06-25
    • 1970-01-01
    相关资源
    最近更新 更多