imagecopyresampled 生成黑盒但未重新采样图像
我正在努力使用 PHP 调整一些 jpeg 图像的大小和重新采样。它采用任何大于 500 像素 x 500 像素的图像,并使最大边为 500 像素。这应该相对简单,但每次我运行脚本时它都会生成黑色 jpeg。创建的 jpeg 具有正确的尺寸,但不包括调整大小的图像。 GD 库已启用,并且我已确保它能够找到原始图像。我已经看了这个代码块一天半了,但没有运气,我没有看到什么?
<?php
$testimage = 'SandyCayCaribbeanbeach.jpg';
$testfolder = "testimage/testimage.jpg";
list($orgwidth, $orgheight, $type, $attr) = getimagesize($testimage);
echo "org. width " . $orgwidth . "px" . "<br />";
echo "org. height " . $orgheight . "px" . "<br />";
if($orgwidth > 500 || $orgheight > 500){
if($orgwidth > $orgheight){
header('Content-type: image/jpeg');
$ratio = $orgwidth/500;
$newwidth = floor($orgwidth/$ratio);
$newheight = floor($orgheight/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
else{
header('Content-type: image/jpeg');
$ratio = $orgheight/500;
$newheight = floor($orgheight/$ratio);
$newwidth = floor($orgwidth/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
}
?>
I am working to resize and resample some jpeg images using PHP. It take any image greater than 500px by 500px and make the largest side 500px. This should be relatively simple but every time I run the script it makes a black jpeg. The jpeg created has the proper dimensions but does not include the resized image. The GD library is enabled, and I have made sure it is finding the original image. I've been looking at this block of code for a day and half with no luck, what am I not seeing?
<?php
$testimage = 'SandyCayCaribbeanbeach.jpg';
$testfolder = "testimage/testimage.jpg";
list($orgwidth, $orgheight, $type, $attr) = getimagesize($testimage);
echo "org. width " . $orgwidth . "px" . "<br />";
echo "org. height " . $orgheight . "px" . "<br />";
if($orgwidth > 500 || $orgheight > 500){
if($orgwidth > $orgheight){
header('Content-type: image/jpeg');
$ratio = $orgwidth/500;
$newwidth = floor($orgwidth/$ratio);
$newheight = floor($orgheight/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
else{
header('Content-type: image/jpeg');
$ratio = $orgheight/500;
$newheight = floor($orgheight/$ratio);
$newwidth = floor($orgwidth/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先确保您已打开错误报告。还要确保它可以找到源图像“SandyCayBaribbeanbeach.jpg”。
在处理图像大小调整之前进行简单的
if(file_exists())
检查将有助于捕获错误。Firstly make sure you have error reporting turned on. Also make sure it can find the source image "SandyCayBaribbeanbeach.jpg".
A simple
if(file_exists())
check before handling the image resizing will help trap errors.我发现我必须指定图像的完整路径,不是 URL,即
/path/to/image.jpg
而不是
http://www.blah.com/image.jpg
使其正常工作。希望它能帮助某人。
I found that I had to specify a full path to the image, not a URL i.e.
/path/to/image.jpg
instead of
http://www.blah.com/image.jpg
to get this to work properly. Hope it helps someone.
仔细检查以确保您的源图像确实是 JPEG。如果您运行的是 Windows,请在 MS Paint 中将其打开并重新另存为 JPEG。这将有助于排除它是不同格式的可能性。
Double-check to make sure your source image is truly a JPEG. If you are running Windows, open it up in MS Paint and re-save as a JPEG. This will help rule out the possibility of it being a different format.
我最近也在一段代码中与这个问题斗争了一段时间,发现如果未定义尺寸,imagecopyresampled 甚至会返回 1。确保源高度和宽度已设置。
I also fought this for a while in a piece of my code recently, and found that imagecopyresampled will even return 1 if the dimensions are not defined. Make sure that your source height and width are set.