PHP 创建图像文件,但仅适用于本地开发,不适用于远程服务器
我有一个代码可以动态调整图像大小并将其发送到浏览器。
但它无法正常工作......只有当我要求它将图像输出到文件而不是浏览器时。
我不认为问题出在我的代码上,因为这个问题只发生在真实服务器上;在我的电脑上它工作得很好。
代码:
$img = $_GET['img'];
ini_set('allow_url_fopen', 'on');
$info = getimagesize($img);
header('Content-type: '.image_type_to_mime_type($info[2]));
Fotos::redimensiona($img, null, Fotos::MINIGAL_WIDTH, Fotos::MINIGAL_HEIGHT, false);
Fotos::redimensiona():
//[...] a whole bunch of code calculating dimensions, they just works
// $funcImage is like 'imagejpeg'/'imagepng'/'imagegif', depends on file
if ($arquivo) {
$funcImage($thumb, "$final.$ext");
return "$final.$ext";
}
else {
$funcImage($thumb);
}
请记住:它适用于本地开发,但不适用于远程 Web 服务器。
[编辑]
如果我注释 header
行,则会打印二进制代码;当我更改原始图像时,此代码会发生变化,如预期的那样。
但是通过 header
Firefox 会显示页面的 URL(例如 http://www.sabianoar.com.br/novosabia/inc/phpImg.php?img=awful_escaped_long_path.jpeg< /code>),如果我执行
CTRL+I
,它会告诉我这是一个大小为 0x0 的 JPEG,大约 10kb。
Opera 向我显示一个空图像,就像我放置了一个带有错误 src
的 一样,即
[EDIT2] 当我尝试保存然后打开输出时(它通常保存到 .jpeg 文件),EyeOfGnome 说“不是 JPEG 文件:以 0xef 0xbb 开头”。
I have a code that dinamically resizes an image and sends it to the browser.
But it doesn't work properly... only if I ask it to output the image to a file instead of the browser.
I don't think the problem is my code, because this problem only occurs on the real server; in my computer its working perfectly.
Code:
$img = $_GET['img'];
ini_set('allow_url_fopen', 'on');
$info = getimagesize($img);
header('Content-type: '.image_type_to_mime_type($info[2]));
Fotos::redimensiona($img, null, Fotos::MINIGAL_WIDTH, Fotos::MINIGAL_HEIGHT, false);
Fotos::redimensiona():
//[...] a whole bunch of code calculating dimensions, they just works
// $funcImage is like 'imagejpeg'/'imagepng'/'imagegif', depends on file
if ($arquivo) {
$funcImage($thumb, "$final.$ext");
return "$final.$ext";
}
else {
$funcImage($thumb);
}
Remember: it works on local development, but doesn't on remote web server.
[EDIT]
if I comment the header
line, the binary code is printed; and this code changes when i change the original image, as expected.
But with the header
Firefox shows me the URL of the page (like http://www.sabianoar.com.br/novosabia/inc/phpImg.php?img=awful_escaped_long_path.jpeg
), and if I do CTRL+I
it tells me it is an JPEG of 0x0 size, and like 10kb.
Opera shows me an empty image, as it would do if I had placed an <img>
with the wrong src
, i.e.
[EDIT2]
EyeOfGnome says "Not a JPEG file: starts with 0xef 0xbb" when I try to save and then open the output (it saves to a .jpeg file normally).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码在开头输出 BOM 。从远程 Web 服务器打开您的代码并检查(使用十六进制编辑器,例如 xxd)在
之前是否包含 BOM。如果 BOM 不在您正在查看的文件中,则它可能位于包含文件中。
有些编辑器添加 BOM,然后 apache 输出它。这可能就是破坏你形象的原因。
Your code outputs BOM at the beginning. Open your code from remote web server and check (using hex editor, for example xxd) if it contains BOM before
<?php
. If BOM isn't in a file you're looking at, it might be in includes.Some editors add BOM and apache outputs it. That's probably what breaks your images.
检查镜像/镜像所在目录的权限。
Check the permissions of the image/directory where the image is located.
这可能不是原因,但是您为什么要回显
Fotos::reDimension()
的结果?像imagejpeg()
等函数在输出图像后都会返回一个bool(不是图像数据),所以你最终会在图像的末尾打印一个“1”。我刚刚测试了这个,它似乎并没有破坏 jpeg 图像。This probably isn't the cause, but why are you echoing the result of
Fotos::redimensional()
? The functions likeimagejpeg()
etc. all return a bool (not the image data) after outputting the image, so you will end up printing a '1' to the end of the image. I just tested this though and it doesn't seem to break jpeg images.您是否尝试过将本地计算机与服务器进行比较?比较 phpinfo() 的输出;它们是不同的平台吗?
Have you tried comparing your local machine to the server? Compare the output of phpinfo(); Are they different platforms?