PHP JPEG 函数不工作
任何处理 JPEG 的 PHP 函数似乎都无法在我的服务器上运行。
此代码:
<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>
创建一个空文件。
使用 GIF 或 PNG 函数将创建一个包含预期文本“简单文本字符串”的图像。
这:
$im = imagecreatefromjpeg("test.jpg");
返回
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'test.jpg' is not a valid JPEG file in /path/to/test.php on line 2
A phpinfo() 显示:
gd
GD Support enabled
GD Version 2.0 or higher
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.3.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
并且网络服务器可以读取任何相关文件。
GIF 和 PNG 函数工作正常,符合预期。
有什么想法吗?
编辑:
在我的 Apache 错误日志文件中找到此内容:
gd-jpeg: JPEG library reports unrecoverable error: Wrong JPEG library version: library is 80, caller expects 62
Any PHP functions that deal with JPEGs don't seem to be working on my server.
This code:
<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>
creates an empty file.
Using a GIF or PNG function will create an image containing the text "A Simple Text String" as expected.
This:
$im = imagecreatefromjpeg("test.jpg");
returns
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'test.jpg' is not a valid JPEG file in /path/to/test.php on line 2
A phpinfo() shows:
gd
GD Support enabled
GD Version 2.0 or higher
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.3.9
T1Lib Support enabled
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
And the webserver can read any relevant files.
GIF and PNG functions work fine, and as expected.
Any ideas?
EDIT:
Found this in my Apache error log file:
gd-jpeg: JPEG library reports unrecoverable error: Wrong JPEG library version: library is 80, caller expects 62
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的错误日志清楚地表明您的 PHP 是针对/需要
libjpeg
版本 62 进行编译的,而您服务器上的库是版本 80。安装正确版本的
libjpeg
,或者重新编译gd
/php
。Your error log clearly shows that your PHP is compiled against/requires
libjpeg
version 62, while the library on your server is version 80.Either install the correct version of
libjpeg
, or recompilegd
/php
.似乎文件
test.jpg
不存在,或者不是正确的文件类型标头(例如,如果有人将 test.png 重命名为 test.jpg,它仍然具有 .png 标头)。尝试使用图像编辑程序创建一个新的 test.jpg 文件,看看它是否有效。It appears that the file
test.jpg
doesn't exist, or isn't the correct filetype headers (eg, if someone renames a test.png as test.jpg it will still have the .png headers). Try creating a new test.jpg file using an image editing program and see if it works.对我有用的一个答案是完全围绕图像进行工作,并将其视为一条字符串。
我会尝试以标准方式执行此操作,但如果它不起作用,请尝试将其作为字符串
尝试 旧版本或 GD 和 PHP 的组合显然存在一些问题。
就我而言,我认为我有一张使用较新版本/压缩的 jpeg 7 的图像。
An answer that worked for me was to work around the image altogether and trearit as a string.
Ill try to do it the standard way, but if it doesnt work, try it as a string
There are apparently some issue wilth older versions or combos of GD an PHP.
In my case I had an image using a newer version/compression of jpeg 7 I think.