PHP use GD library to generate pictures after uploading related issues

Source: Internet
Author: User
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;
}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.