使用GD调整大小可输出黑色图像 [英] Resizing with GD outputs black images

查看:51
本文介绍了使用GD调整大小可输出黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What can cause php gd to produce a black image after resizing? The following code always outputs a black image for every valid jpeg file.

<?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] => 
)

该代码似乎可以在其他环境中使用.可能与操作系统,已安装的软件包,库等有关吗?

The code appeared working in other environments. Probably it is related to OS, installed packages, libraries, etc?

推荐答案

研究

只是想重现你的情况.使用现成的PHP和Apache运行代码将显示以下内容

Research

Just tried to reproduce your situation. Running your code with out-of-the-box PHP and Apache displays the following

图像"http://localhost/"由于包含以下内容而无法显示 错误.

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

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

Although browser tells you that there were some errors, they cannot be seen because of the headers returned in response were of Content-Type: image/jpeg thus forcing browser to interpret it as an image. By removing the header and setting the following will output errors.

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

回答

什么会导致php gd调整大小后产生黑色图像?

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

由于gd_info输出证明已加载 GD扩展 ,请检查文件名(Linux是caseSensitive)和权限是否正确.如果Apache作为www-data(组)运行

Since the gd_info output proves that the GD extension is loaded, check if the filename (linux is caseSensitive) and permissions are correct. If Apache is running as www-data (group)

sudo chown :www-data test.jpg && sudo chmod 660 test.jpg 

代码改进/解决方案

Code improvement / solution

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)上,单行代码将在您的系统上安装GD:

Comments

This should be used as a temporary solution (development environment). IMHO, the errors should be handled by a central error handler, display_errors should be false in production. Also, the errors (in this case there would be Fatal error) are logged by default - check the logs for more (the frequent, the better). Also, on linux (with apt) the one-liner will install GD on your system:

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

这篇关于使用GD调整大小可输出黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆