php 如何保存从 imagecreatefromstring() 函数创建的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15760559/
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
How to save an image created from imagecreatefromstring() function?
提问by Baraskar Sandeep
Here is my code:
这是我的代码:
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
I'd like to save the image generated this way to a directory. How do I do this?
我想将这种方式生成的图像保存到一个目录中。我该怎么做呢?
回答by Pablo Martinez
This is the correct syntax for imagepng
:
这是正确的语法imagepng
:
imagepng($im, "/path/where/you/want/save/the/png.png");
According to the PHP manual:
根据PHP 手册:
bool imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )
filename - The path to save the file to.
If not set or NULL, the raw image stream will be outputted directly.
bool imagepng ( 资源 $image [, string $filename [, int $quality [, int $filters ]]] )
文件名 - 保存文件的路径。
如果未设置或为NULL,则直接输出原始图像流。
回答by Aditya P Bhatt
Below code will be helpful:
下面的代码会有所帮助:
$data = 'code in bytes'; // replace with an image string in bytes
$data = base64_decode($data); // decode an image
$im = imagecreatefromstring($data); // php function to create image from string
// condition check if valid conversion
if ($im !== false)
{
// saves an image to specific location
$resp = imagepng($im, $_SERVER['DOCUMENT_ROOT'].'folder_location/'.date('ymdhis').'.png');
// frees image from memory
imagedestroy($im);
}
else
{
// show if any error in bytes data for image
echo 'An error occurred.';
}
Please suggest if some other better way of doing !
请建议是否有其他更好的方法!