php 在php中创建图像,显示有效,保存到文件不会
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1413753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating an image in php, display works, saving to file does not
提问by Fons
I am creating an image via a php script using imagepng. This works fine and displays well on the website. even saving-as gives me a valid .png file
我正在使用 imagepng 通过 php 脚本创建图像。这工作正常并且在网站上显示良好。甚至另存为给了我一个有效的 .png 文件
header( "Content-type: image/png" );
imagepng($my_img);
$save = "../sigs/". strtolower($name) .".png";
//imagepng($my_img, $save, 0, NULL);
imagepng($my_img, $save);
This is the end part of the code I use to generate the file and return it as picture on the website. but both options (tried the one marked out as well) dont save the file to the webserver for later use. The folder where the file is written is even set to chmod 777 at the moment to rule out any issues on that front. the $name is for sure a valid string without spaces.
这是我用来生成文件并将其作为图片返回到网站上的代码的结尾部分。但是这两个选项(也尝试了标出的选项)都不会将文件保存到网络服务器以备后用。写入文件的文件夹现在甚至设置为 chmod 777 以排除这方面的任何问题。$name 肯定是一个没有空格的有效字符串。
回答by mauris
Make sure that PHP has permissions to write files to that folder. chmod probably only affects FTP users or particular users.
确保 PHP 有权将文件写入该文件夹。chmod 可能只影响 FTP 用户或特定用户。
And try one at a time. i.e.:
并一次尝试一个。IE:
header( "Content-type: image/png" );
imagepng($my_img);
then
然后
$save = "../sigs/". strtolower($name) .".png";
imagepng($my_img, $save);
So that you can isolate errors.
以便您可以隔离错误。
Attempt saving in the same folder as the script first, see if there's any problem.
尝试先保存在与脚本相同的文件夹中,看看是否有任何问题。
$save = strtolower($name) .".png";
imagepng($my_img, $save);
回答by Fons
Thanks a lot for you help on clearing my mind and having me look from a different angle. All had to do with rights of the file.
非常感谢你帮助我理清思路,让我从不同的角度看。所有这些都与文件的权限有关。
As the script generated the file, the rights are not set correct and overwriting is not possible.
由于脚本生成文件,权限设置不正确,无法覆盖。
after taking out:
取出后:
header( "Content-type: image/png" );
imagepng($my_img);
I received an error message about not being able to write. when is set the file manual to chmod 755, the script worked like a charm.
我收到一条关于无法写入的错误消息。当将文件手册设置为 chmod 755 时,脚本就像一个魅力。
so the new code now looks like this:
所以新代码现在看起来像这样:
header( "Content-type: image/png" );
imagepng($my_img);
$save = "../sigs/". strtolower($name) .".png";
chmod($save,0755);
imagepng($my_img, $save, 0, NULL);
imagedestroy($my_img);
Setting the file to be writeable fixed the issue and all is working as intended.
将文件设置为可写解决了这个问题,一切都按预期工作。
Best regards Fons
最好的问候 Fons
回答by jeroen
Are you sure the relative path is correct? That can be a bit confusing if that script is called from another script.
你确定相对路径是正确的吗?如果该脚本是从另一个脚本调用的,那可能会有点混乱。
You could try to change the path to:
您可以尝试将路径更改为:
$save = $_SERVER['DOCUMENT_ROOT'] . "/sigs/" . strtolower($name) . ".png";
Edit:And of course check the return value of imagepng() and your error log
编辑:当然检查 imagepng() 的返回值和您的错误日志
回答by walter1957
Your code, Fons, provoked the usual problem I was having of displaying the Image created using the GD Library but disabling any Html code after the Php. By removing the line below
您的代码 Fons 引发了我经常遇到的问题,即显示使用 GD 库创建的图像但在 Php 之后禁用任何 Html 代码。通过删除下面的行
header( "Content-type: image/png" ); imagepng($image);
and only using the next 2 lines in Php I was able to save the file and then access the Image in the Html section (bottom line of code) without destroying the Html coding.
并且仅在 Php 中使用接下来的 2 行,我才能保存文件,然后在不破坏 Html 编码的情况下访问 Html 部分(代码的底行)中的图像。
$save='./img/Graph.png'; chmod($save,0755);
imagepng($image,$save,0,NULL); imagedestroy($image);
<img width="500" height="350" align="top" alt="" src="./img/Graph.png" />
Thanks.
谢谢。

