开发者

PHP further process result of ImagePNG() without saving to disk?

开发者 https://www.devze.com 2023-01-16 00:24 出处:网络
Is there a way to process created image (using ImagePNG() from GD library) in mem开发者_开发百科ory so to speak without need to save temporary file to disk? What I am trying to achieve is basically to

Is there a way to process created image (using ImagePNG() from GD library) in mem开发者_开发百科ory so to speak without need to save temporary file to disk? What I am trying to achieve is basically to POST created image and would like to avoid any need to create temporary files, if possible. PHP would just output response of that POST from server.

Thanks


Well then you need curl.

From the docs: PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

To capture the image in memory and send it with curl after you could try output buffering:

    <?php
ob_start();
header('Content-type: image/jpeg');
ImageJpg(...);
$myImage = ob_get_contents();
ob_clean ();

?>

Something like that?


I tried the output buffering but gave up after two hours. This is what worked for me:

First create a temporary file and then pass that to ImagePNG(). After that just use file reading routine to read the file content and store the result in a variable and delete the temp file created (unlink($tmp); )

$tmpfile = tempnam("/tmpdir", "img");
//your name will not clash with another request

imagepng($my_img,$tmpfile);
$handle = fopen($tmpfile, "rb");
$img_file_contents = fread($handle, filesize($tmpfile));
fclose($handle);
unlink($tmpfile); //no accumulation of image files
//now send the $img_file_contents via cURL
0

精彩评论

暂无评论...
验证码 换一张
取 消