PHP 中的图像裁剪产生空白结果
我只是尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为 sobedai 答案的补充:您在 CropPicture() 中使用的任何函数都可能会失败。您必须测试每一个的返回值。如果出现错误,它们将返回 false 并且您的函数无法(正常)继续。
编辑:您可能还对 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).
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...
有几件事:
保存 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.