Inserting modified image into a database

See all posts See thread Reply

Re: Inserting modified image into a database new!
by Rocket Dog, 16 years, 3 months ago
Well yes, but you can't both insert the file into the database and to the server as a file if you also need to modify the file. Reason being, each time you call process() everything gets reset.

So in my case, I want to convert the image format of the original, then save it on disk and also to the database. Then I want to create a thumbnail version, which I also want to save on disk as well as to the database. So in essence I have 4 processes to go through for each uploaded file:

1) Reformat the file and save it on disk.
2) Reformat the file as in #1, then save it to the database.
3) Resize the file as a thumbnail and save it on disk.
4) Resize the file as in #3 and save it to the database.

I can somewhat reduce the amount of code required by creating function "A" that will handle the image reformatting, and function "B" to handle the image resizing. I can then call on function "A" for steps 1 and 2, and function "B" for steps 3 and 4.

But if I've still got this all wrong, feel free to jump in! And thanks for your quick feedback!Reply
Re: Inserting modified image into a database new!
by colin, 16 years, 3 months ago
What about something like this?

include('class.upload.php');
$foo = new upload($_FILES['form_field']);
if ($foo->uploaded) {
  // Your first pass at the image
  $foo->image_convert         = 'png';
  $foo->image_resize          = true;
  $foo->image_x               = 500;
  $foo->image_ratio_y         = true;
  // Call process() without an argument to get the image in return
  $img = $foo->process();
  // File WAS processed successfully.
  if ($foo->processed) {
    // Prepare the query (and then run it). The content of the image is in $img
    $query = " INSERT INTO my_table(image_file) VALUES ('" . addslashes($img) . "')";
    // Store the picture on file, using PHP functions
    imagepng($img, '/home/a/b/c/image.png');
    // Do not delete the temp file as we will do another pass
  }
  // Your second pass at the image (thumbnail)
  $foo->image_convert         = 'png';
  $foo->image_resize          = true;
  $foo->image_x               = 100;
  $foo->image_ratio_y         = true;
  // Call process() without an argument to get the image in return
  $img = $foo->process();
  // File WAS processed successfully.
  if ($foo->processed) {
    // Prepare the query (and then run it). The content of the image is in $img
    $query = " INSERT INTO my_table(thumbnail_file) VALUES ('" . addslashes($img) . "')";
    // Store the picture on file, using PHP functions
    imagepng($img, '/home/a/b/c/thumbnail.png');
    // Delete the temporary file(s).
    $foo->clean();
  }
}
Reply
Re: Inserting modified image into a database new!
by sergio, 11 years, 2 months ago
Where are you actually connection to the database here?Reply