0

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?

3
  • What is the exact error message? PS: imagejpeg accepts a resource as a first argument, not a string Commented Jul 22, 2012 at 22:02
  • Are you sure that given SQL query returns at least one row? Commented Jul 22, 2012 at 22:02
  • 1
    in addition to @zerkms: OP, have a look at this function Commented Jul 22, 2012 at 22:04

1 Answer 1

1

Try using imagecreatefromstring to create a valid resource.

// ...
if (false !== $im = imagecreatefromstring($image)) {
    imagejpeg($im, SITE_ROOT . '/photos/7/test.jpeg');
    imagedestroy($im);
}
Sign up to request clarification or add additional context in comments.

3 Comments

false !== odd comparison.... true == is MUCH clearer. That's kinda made me laugh. first time i've come across a statement such as that
@Flukey, following the documentation, FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.
Yes I know. but you could still use true == it's much clearer to read than false !== Your way makes the code difficult to read. I know what the function returns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.