imagecopyresampled 生成黑盒但未重新采样图像

发布于 2024-10-15 03:41:35 字数 1480 浏览 8 评论 0 原文

我正在努力使用 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 技术交流群。

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

发布评论

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

评论(4

苏璃陌 2024-10-22 03:41:35

首先确保您已打开错误报告。还要确保它可以找到源图像“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.

—━☆沉默づ 2024-10-22 03:41:35

我发现我必须指定图像的完整路径不是 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.

榆西 2024-10-22 03:41:35

仔细检查以确保您的源图像确实是 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.

梦晓ヶ微光ヅ倾城 2024-10-22 03:41:35

我最近也在一段代码中与这个问题斗争了一段时间,发现如果未定义尺寸,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.

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