使用 GD 调整大小输出黑色图像

Resizing with GD outputs black images

什么会导致 php gd 在调整大小后生成黑色图像?以下代码始终为每个有效的 jpeg 文件输出黑色图像。

<?php

$filename = 'test.jpg';
$percent = 0.5;

header('Content-Type: image/jpeg');

list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);
imagedestroy($thumb);
?>

gd_info()的输出:

  Array
(
    [GD Version] => bundled (2.1.0 compatible)
    [FreeType Support] => 1
    [FreeType Linkage] => with freetype
    [T1Lib Support] => 
    [GIF Read Support] => 1
    [GIF Create Support] => 1
    [JPEG Support] => 1
    [PNG Support] => 1
    [WBMP Support] => 1
    [XPM Support] => 
    [XBM Support] => 1
    [JIS-mapped Japanese Font Support] => 
)

代码出现在其他环境中。可能与 OS、已安装的软件包、库等有关?

确保 gd 已安装并启用。

要检查,请使用以下命令创建一个 PHP 文件:

<?php phpinfo(); 

通过浏览器访问文件并向下滚动到 gd 部分。如果 gd 不存在或被禁用,请使用 yum、apt-get 或 Windows 等价物添加它。

您还需要可用的 GD 库 (http://www.libgd.org/)。

考虑切换到 IMagick (http://php.net/manual/en/book.imagick.php) 以获得更好的图像质量。

尝试使用此代码查看正在发生的错误:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$filename = 'test.jpg';
$percent = 0.5;

list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//header('Content-Type: image/jpeg');
//imagejpeg($thumb);
//imagedestroy($thumb);
?>

因此,如果 PHP 遇到错误,它会显示在屏幕上。如果没有错误,屏幕将显示空白。

研究

刚刚尝试重现您的情况。 运行 您的代码 out-of-the-box PHP 并且 Apache 显示以下内容

The image “http://localhost/” cannot be displayed because it contains errors.

虽然浏览器告诉您有一些错误,但由于响应返回的 headers 属于 Content-Type: image/jpeg,因此无法看到它们,因此迫使浏览器将其解释为图像。通过删除 header 并设置以下内容将输出错误。

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
...
//header('Content-Type: image/jpeg');
...
回答

What can cause php gd to produce a black image after resizing?

由于 gd_info 输出证明 GD extension 已加载,请检查文件名(linux 区分大小写)和权限是否正确。如果 Apache 是 运行 as www-data (group)

sudo chown :www-data test.jpg && sudo chmod 660 test.jpg 
代码改进/解决方案
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

if (extension_loaded('gd') && function_exists('gd_info'))
{
    $filename = 'test.jpg';

    if (file_exists($filename) && is_readable($filename))
    {
        $percent = 0.5;

        header('Content-Type: image/jpeg');

        list($width, $height) = getimagesize($filename);
        $newwidth = $width * $percent;
        $newheight = $height * $percent;

        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($filename);

        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        imagejpeg($thumb);
        imagedestroy($thumb);
    }
    else
    {
        trigger_error('File or permission problems');
    }
}
else
{
    trigger_error('GD extension not loaded');
}
注释

这应该用作临时解决方案(开发环境)。恕我直言,错误应该由中央错误处理程序处理,display_errors 在生产中应该是 false。此外,默认情况下会记录错误(在本例中为 Fatal error)- 检查日志以获取更多信息(越频繁越好)。此外,在 linux(使用 apt)上,one-liner 将在您的系统上安装 GD:

sudo apt-get update && sudo apt-get install php5-gd && sudo /etc/init.d/apache2 restart

下面的函数创建了一个缩略图,只需稍作改动,您就可以 O/P 将其显示在屏幕上。我觉得这样比较好用

/*
 * PHP function to resize an image maintaining aspect ratio
 * http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
 *
 * Creates a resized (e.g. thumbnail, small, medium, large)
 * version of an image file and saves it as another file
 */

define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);

function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
{
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
    switch ($source_image_type) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($source_image_path);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($source_image_path);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($source_image_path);
            break;
    }
    if ($source_gd_image === false) {
        return false;
    }
    $source_aspect_ratio = $source_image_width / $source_image_height;
    $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
    if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
        $thumbnail_image_width = $source_image_width;
        $thumbnail_image_height = $source_image_height;
    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
        $thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
        $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
    } else {
        $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
        $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
    }
    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
    imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
    imagedestroy($source_gd_image);
    imagedestroy($thumbnail_gd_image);
    return true;
}

参考this more信息。

如果 php-gd 不存在,请使用以下命令。

sudo apt-get install php5-gd