0

I'm trying to upload an image, change its size and then write the result to a MySQL database. Basic code:

    // load original image
$image = imagecreatefromjpeg($file); // load original image

       // create new image
$newImage = imagecreatetruecolor($newWidth, $newHeight);

    // copy original to new, changing size
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight,$origWidth, $origHeight);

    // save new image in database (in a BLOB field)
mysql_query("UPDATE myTable SET Photo='" . mysql_escape_string($newImage) . "' WHERE keyField=2");

But nothing is stored. $newImage appears to be a valid image of the correct size. What am I failing to do?

Jaime Soriano
  • 7,309
  • 2
  • 33
  • 45
user1342299
  • 21
  • 1
  • 2
  • What do you mean with "nothing is stored"? An empty string? Did you try to use `base64_encode` instead of `mysql_escape_string`? (btw, according to PHP documentation `mysql_escape_string` is obsolete). – Jaime Soriano Apr 09 '15 at 19:34
  • I mean that the BLOB field is 0 bytes after the update operation. I've tried base64_encode with the same result. I'm aware that mysql_escape_string is obsolete and am using mysqli, but thought that using the old function would be clearer for the basic code; the outcome is the same. – user1342299 Apr 10 '15 at 09:51

3 Answers3

1

As @Wayne said. I suggest to save image in file system and save file path in database
here is the code to copy image to filesystem

 $imagepath="C:/your/file/complete/path";
 if(isset($_FILES["file"])){
    move_uploaded_file($_FILES["file"]["tmp_name"], $imagepath);
    mysql_query("UPDATE myTable SET photo_path='" .$imagepath. "' WHERE keyField=2");
 }

EDIT: If you really need to save in DB, check this detailed explanation

Community
  • 1
  • 1
Sai Phani
  • 259
  • 1
  • 6
  • Thanks but for various reasons putting the image in the database is the most practical option for what I'm doing. The stored images are only small which is one reason for changing the size if necessary. – user1342299 Apr 10 '15 at 09:52
0

If you really want to store the image in your database, try this instead of your last line:

mysql_query("UPDATE myTable SET Photo='" . mysql_escape_string(imagejpeg($newImage)) . "' WHERE keyField=2");

imagedestroy($newImage);

I haven't tried this by my own, so it just a suggestion.


Short edit concerning your comment:

Try to store the output in the buffer and use it as variable like this:

ob_start();
imagejpeg($newImage)
$bufferImage = ob_get_clean();

mysql_query("UPDATE myTable SET Photo='" . mysql_escape_string($bufferImage) . "' WHERE keyField=2");

imagedestroy($newImage);

Reference: capture PHP output into a variable

Community
  • 1
  • 1
stefan
  • 4,958
  • 4
  • 20
  • 37
  • I've tried this but imagejpeg writes directly to the browser and not into the query. I would have thought that what I'm trying to achieve was fundamental to the use of BLOB fields whether they be images or not. I'm suspicious that $newImage doesn't point directly to the image content but that maybe a helper function is needed to get it into the right state. – user1342299 Apr 10 '15 at 09:55
0

Okay, I've found an answer. The basic problem is that there isn't a PHP image function that outputs to a string. However, this can be circumvented by manipulating buffering thus:

ob_start();      // start buffering              
imagejpeg($newImage);   // write image to buffer
$imageString = ob_get_contents();   // retrieve buffer as string
ob_end_clean();     // clean up and close buffer
mysql_query("UPDATE myTable SET Photo='" . mysql_escape_string($imageString) . "' WHERE keyField=2");  // this now works

Thanks for everyone's help.

user1342299
  • 21
  • 1
  • 2