什么可能导致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] =>
)
代码似乎在其他环境中工作。可能它与操作系统,已安装的软件包,库等有关吗?
答案 0 :(得分:8)
试图重现你的情况。使用开箱即用的PHP和Apache运行代码将显示以下内容
图片“
http://localhost/
”无法显示,因为它包含 错误。
虽然浏览器告诉您存在一些错误,但由于响应中返回的标头为Content-Type: image/jpeg
而无法看到,因此强制浏览器将其解释为图像。删除header
并设置以下内容将输出错误。
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
...
//header('Content-Type: image/jpeg');
...
回答
调整大小后会导致php gd产生黑色图像的原因是什么?
由于gd_info
输出证明加载了GD extension,请检查文件名(linux是caseSensitive)和权限是否正确。如果Apache
正在www-data
(组)
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
),单行将在您的系统上安装GD:
sudo apt-get update && sudo apt-get install php5-gd && sudo /etc/init.d/apache2 restart
答案 1 :(得分:0)
确保已安装并启用gd。
要检查,请使用以下命令创建PHP文件:
<?php phpinfo();
通过浏览器访问该文件,然后向下滚动到gd部分。如果gd不存在,或者它被禁用,请使用yum,apt-get或Windows等效项添加它。
您还需要GD库(http://www.libgd.org/)。
考虑切换到IMagick(http://php.net/manual/en/book.imagick.php)以获得更好的图像质量。
答案 2 :(得分:0)
尝试使用此代码查看正在发生的错误:
<?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遇到错误,它将在屏幕上显示。如果没有错误,屏幕将显示空白。
答案 3 :(得分:0)
下面的功能会创建一个缩略图,只需很少的更改,您也可以将其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