Tuesday, October 11, 2022

[FIXED] How do I pass binary (blob) data from mysql to imagejpeg for a file save?

Issue

I'm in the process of converting all my images stored in a DB to a file system structure but can't seem to save them to a file. Here is what I'm trying.

....
$SQL = "SELECT thumbnail FROM profile_image WHERE user_id=7";
$r = mysql_query($SQL) or die ("Error");

$image=mysql_result($r,0,"thumbnail");

$destination = SITE_ROOT .'/photos/7/test.jpeg';

imagejpeg($image, $destination);

//header("Content-type: image/jpeg");
//echo ($image);

It's complaining about an invalid resource...what am I missing?


Solution

Try using imagecreatefromstring to create a valid resource.

// ...
if (false !== $im = imagecreatefromstring($image)) {
    imagejpeg($im, SITE_ROOT . '/photos/7/test.jpeg');
    imagedestroy($im);
}


Answered By - Federkun
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.