Reply to Re: Inserting modified image into a database

Re: Inserting modified image into a database new!
by colin, 15 years, 9 months ago
This functionality is already built in the class. You can call Process() with no arguments to get in return the raw content of the picture. You can then save it directly in the database, or output it right away.

Here is your code, using the built-in feature:
include('class.upload.php');
$foo = new upload($_FILES['form_field']);
if ($foo->uploaded) {
  // Modify the image however you like.  For instance...
  $foo->image_convert         = 'jpg';
  $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. The content of the image is in $img
    $query = " INSERT INTO my_table(image_file) VALUES ('" . addslashes($img) . "')";
    // Delete the temporary file(s).
    $foo->clean();
  }
}

Note that doing as above is more efficient as you don't have to create an image, read it back, and then delete it. But generally speaking, it is a bad idea to store images in a database, unless they are very small.

To output the image directly into the browser:
$img = $foo->process();
header("Content-type: image/jpeg");
echo $img;
Reply

Your reply

Name *
Email 
Title *
Text *
CAPTCHA image
Enter the code displayed on the image:
Click on the image to generate another one if it is hard to read it. The case is important