PHP use GD library to generate images after downloading problems
Is this, I use the GD library to generate pictures, do not save the image can be downloaded directly? Try it one morning or not ...
image.php
Create a true Color canvas
$image = Imagecreatetruecolor (400,190);
Background Create color
$green = Imagecolorallocate ($image, 255,255,255);
Fill Canvas Color
Imagefill ($image, 0,0, $green);
Output picture
Header ("Content-type:image/jpeg");
Imagejpeg ($image);
Destroying resources
Imagedestroy ($image);
index.php
imagedown.php
if (isset ($_get[' filename ')) {
???????? Var_dump (getimagesize ($_get[' filename '));
???????? Download ($_get[' filename ');
????}
???? function Download ($fileName) {
???????? Header ("Content-type:image/jpeg");
???????? Header (' content-disposition:attachment; Filename= '. $fileName. ');
???????? Header (' Content-length: '. FileSize ($fileName));
???????? ReadFile ($fileName);
????}
------Solution--------------------
No, you have to go through the picture to land the process. PHP's GD library does not seem to support memory directly read the generated picture content, can only be saved to local re-read.
It is recommended to rewrite the GD library to provide a function for getting picture content.
------Solution--------------------
Write like this
Download (' x.jpg ');
function image () {
Create a true Color canvas
$image = Imagecreatetruecolor (400,190);
Background Create color
$green = Imagecolorallocate ($image, 255,255,0);
Fill Canvas Color
Imagefill ($image, 0,0, $green);
Output picture
Header ("Content-type:image/jpeg");
Imagejpeg ($image);
Destroying resources
Imagedestroy ($image);
}
function Download ($fileName) {
Ob_start ();
Image ();
$s = Ob_get_clean ();
Header ("Content-type:image/jpeg");
Header (' content-disposition:attachment; Filename= '. $fileName. ');
Header (' Content-length: '. strlen ($s));
Echo $s;
}