【问题标题】:How to output a gif image with gd in php, Bitnami WAMP, Windows 10?如何在 php、Bitnami WAMP、Windows 10 中使用 gd 输出 gif 图像?
【发布时间】:2017-10-12 12:01:59
【问题描述】:

如何在 php、Bitnami WAMP、Windows 10 中输出带有 gd 的 gif 图像? 我可以将其创建为文件,但无法输出。

似乎出现错误,因为没有文件http://localhost/type705b/public/index.php/test/img11
主机后面的参数(public/index.php/test/img11),用于访问控制器动作。 真正的文件是/type705b/src/Bundle/Resources/views/test/img/ImageOutputingError.php

也许我必须更正.htaccess 并启用图像支持?

Options -MultiViews
Options -Indexes

Options +FollowSymlinks
RewriteEngine on
RewriteBase /public/
IndexIgnore * 

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{ENV:REQUEST_FILENAME} !-d
RewriteCond %{ENV:REQUEST_FILENAME} !-f
RewriteCond %{ENV:REQUEST_FILENAME} !-l

RewriteRule ^index.php?(.+)$ index.php?url=$1 [QSA,L] 

在我的情况下,图像在 Chrome 和 Opera 中不显示,在 Firefox 中产生错误“图像http://localhost/type705b/public/index.php/test/img11,无法显示,因为它包含错误”,并在资源管理器中显示带有十字的小方块。

Inspector 显示此标记(我犯了一个错误“|| body, || img”)以将其显示为代码:

    || body style="margin: 0px; background: #0e0e0e;">
|| img style="-webkit-user-select: none;background-position: 0px 0px, 10px 10px;background-size: 20px 20px;background-image:linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);" src="http://localhost/type705b/public/index.php/test/img11">
</body>

背景图像没有我在脚本中使用的颜色:#0000FF; .

ImageOutputingError.php

$width = 150;
$height = 150;
$img = imagecreate($width, $height);

//Create image background
//$white = ImageColorAllocate($img, 255, 255, 255);
//$black = ImageColorAllocate($img, 0, 0, 0);
//$red   = ImageColorAllocate($img, 255, 0, 0);
$blue  = imagecolorallocate($img, 0, 0, 255); 
ImageFill($img,$width,$height,$blue); 

header('Content-type: image/gif');
imagegif($img);
imagedestroy($img);
imagecolordeallocate($img, $blue); 

ImageCreatingWorking.php

//This one works, but i want to output, versus file creation

$width = 150;
$height = 150;
$img = imagecreate($width, $height);
$blue  = imagecolorallocate($img, 0, 0, 255); 
ImageFill($img,$width,$height,$blue); 
imagegif($img, __DIR__ .'/trial.gif' );
imagedestroy($img);
imagecolordeallocate($img, $blue); 

/* * phpinfo()

PHP 版本 5.6.30 <...>

已启用 GD 支持

GD版本捆绑(2.1.0兼容)

FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.7.0
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version 9 compatible
PNG Support enabled
libPNG Version  1.5.26
WBMP Support    enabled
XPM Support enabled
libXpm Version  30411
XBM Support enabled
WebP Support    enabled

*/

【问题讨论】:

    标签: php gd gif


    【解决方案1】:

    尝试删除imagecolordeallocate($img, $blue);(最后一行)。它没有用并产生警告,因为您已经销毁了$img。虽然代码对我有用。

    如果这没有帮助,请尝试在您的 php 文件中注释掉 header('Content-type: image/gif'); 行。它将在您的浏览器中将图像内容显示为文本,然后查看是否没有显示任何错误。通常它应该以 GIF87a 开头,然后是一些不可读的乱码。如果“GIF87a”之前有什么东西,它会阻止浏览器显示图像。例如一些 php 警告或通知。

    代码本身看起来不错,对我有用。

    它不应该与 .htaccess 文件有任何关系。浏览器了解它正在查看图像,这意味着它已到达正确的 url 并接收到内容类型标头。但由于某种原因,该内容不是有效的 GIF。

    【讨论】:

    • 谢谢。我将ImageOutputingError.php 放到 localhost 目录,并使用直接 url localhst/ImageOutputingError.php - 这行得通。仅当将其包含在自己的框架操作中时,它才起作用。很可能标头发送错误,或者我在某处回显,这会阻止图像显示。
    猜你喜欢
    • 2012-06-17
    • 1970-01-01
    • 2010-11-30
    • 2010-12-11
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2011-03-06
    • 2018-09-12
    相关资源
    最近更新 更多