PHP 中的图像裁剪产生空白结果

发布于 2024-08-27 00:48:03 字数 516 浏览 6 评论 0原文

我只是尝试使用 PHP 裁剪 JPEG 图像(无缩放)。这是我的函数以及输入。

function cropPicture($imageLoc, $width, $height, $x1, $y1) {
    $newImage = imagecreatetruecolor($width, $height);

    $source = imagecreatefromjpeg($imageLoc);
    imagecopyresampled($newImage,$source,0,0,$x1,$y1,$width,$height,$width,$height);
    imagejpeg($newImage,$imageLoc,90);
}

当我按如下方式调用它时 --cropPicture('image.jpg', 300, 300, 0, 0) -- 该函数正确完成,但我留下了一个 300x300 的黑色图像px(换句话说,一块空白画布)。我是否传递了错误的论点?

图像存在并且可写。

I'm simply trying to crop a JPEG image (no scaling) using PHP. Here is my function, along with the inputs.

function cropPicture($imageLoc, $width, $height, $x1, $y1) {
    $newImage = imagecreatetruecolor($width, $height);

    $source = imagecreatefromjpeg($imageLoc);
    imagecopyresampled($newImage,$source,0,0,$x1,$y1,$width,$height,$width,$height);
    imagejpeg($newImage,$imageLoc,90);
}

When I call it as follows--cropPicture('image.jpg', 300, 300, 0, 0)--the function completes properly, but I'm left with a black image that is 300x300 px (in other words, a blank canvas). Am I passing in the wrong arguments?

The image exists and is writeable.

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

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

发布评论

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

评论(2

梦归所梦 2024-09-03 00:48:03

作为 sobedai 答案的补充:您在 CropPicture() 中使用的任何函数都可能会失败。您必须测试每一个的返回值。如果出现错误,它们将返回 false 并且您的函数无法(正常)继续。

function cropPicture($imageLoc, $width, $height, $x1, $y1) {
  $newImage = imagecreatetruecolor($width, $height);
  if ( !$newImage ) {
    throw new Exception('imagecreatetruecolor failed');
  }

  $source = imagecreatefromjpeg($imageLoc);
  if ( !$source ) {
    throw new Exception('imagecreatefromjpeg');
  }

  $rc = imagecopyresampled($newImage,$source,0,0,$x1,$y1,$width,$height,$width,$height);
  if ( !$rc ) {
    throw new Exception('imagecopyresampled');
  }

  $rc = imagejpeg($newImage,$imageLoc,90);
  if ( !$rc ) {
    throw new Exception('imagejpeg');
  }
}

编辑:您可能还对 http://docs.php.net/error_get_last 感兴趣。示例脚本中的异常消息没有那么有用......

As an addition to sobedai's answer: Any of those functions you use in cropPicture() can fail. You have to test the return value of each and every one. In case of an error they return false and your function cannot continue (properly).

function cropPicture($imageLoc, $width, $height, $x1, $y1) {
  $newImage = imagecreatetruecolor($width, $height);
  if ( !$newImage ) {
    throw new Exception('imagecreatetruecolor failed');
  }

  $source = imagecreatefromjpeg($imageLoc);
  if ( !$source ) {
    throw new Exception('imagecreatefromjpeg');
  }

  $rc = imagecopyresampled($newImage,$source,0,0,$x1,$y1,$width,$height,$width,$height);
  if ( !$rc ) {
    throw new Exception('imagecopyresampled');
  }

  $rc = imagejpeg($newImage,$imageLoc,90);
  if ( !$rc ) {
    throw new Exception('imagejpeg');
  }
}

edit: You might also be interested in http://docs.php.net/error_get_last. The exception messages in the example script aren't that helpful...

衣神在巴黎 2024-09-03 00:48:03

有几件事:

保存 imagecopyresampled 和 imagejpeg 的返回值,

其中一个无法正常工作,检查哪个是 false,这将缩小范围。

乍一看,我会首先查看读写权限。

A couple of things:

save the return values for imagecopyresampled and imagejpeg

One of those is not working properly, check which is false and that will narrow it down.

At first glance, I would look at reading and writing permissions first.

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