www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Set GD image stream to variable?

  1. #1
    Join Date
    Mar 2005
    Posts
    250

    resolved [RESOLVED] Set GD image stream to variable?

    With the GD library functions, it seems one can only output an image resource's binary data directly, or to a file:

    PHP Code:
    imagejpeg($imgresource);

    imagejpeg($imgresource$filename); 
    But the data stream can't simply be stored as a variable:

    PHP Code:
    $datastream imagejpeg($imgresource); 
    The above does not work, because imagejpeg() doesn't return a stream, it outputs it.

    Why do I want to store the stream itself? Because rather than output the data directly or write a new image file, I want to store it in a database! I've used the GD functions to manipulate an image, and now I want to store the binary data of the resulting image in a database. That is, the same kind of data I would get by running fread() on an existing image.

    Isn't there any way to do this?

  2. #2
    Join Date
    Apr 2007
    Posts
    1,664
    imagejpeg($imgresource, $filename);

    Then read with file_get_contents() the file and unlink() it.

  3. #3
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    PHP Code:
    ob_start();
    imagejpeg($imgresource); 
    $data ob_get_clean(); 

  4. #4
    Join Date
    Mar 2005
    Posts
    250
    Interesting. I've never worked with output buffering; I looked up the documentation after seeing your reply, and it seems pretty straightforward. I'll give it a try!

  5. #5
    Join Date
    Mar 2005
    Posts
    250
    That worked great! Thanks!

  6. #6
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Glad I could help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles