tags:

views:

116

answers:

3

Hi, I use a script (http://www.webmotionuk.co.uk/php-jquery-image-upload-and-crop-v11/) that handles an image and then create a thumbnail.

Actually I don't want to store images on the file system but I want to store them in a mysql db in a BLOB field.

I could first store the created thumbnail on the fs, store the file in the DB and after i can delete the thumb from FS.

Is there a way to store directly the image on the DB?

This is the function:

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){

    list($imagewidth, $imageheight, $imageType) = getimagesize($image);

    $imageType = image_type_to_mime_type($imageType);



    $newImageWidth = ceil($width * $scale);

    $newImageHeight = ceil($height * $scale);

    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

    switch($imageType) {

     case "image/gif":

      $source=imagecreatefromgif($image); 

      break;

        case "image/pjpeg":

     case "image/jpeg":

     case "image/jpg":

      $source=imagecreatefromjpeg($image); 

      break;

        case "image/png":

     case "image/x-png":

      $source=imagecreatefrompng($image); 

      break;

    }



    imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);

    switch($imageType) {

     case "image/gif":

        imagegif($newImage,$thumb_image_name); 

      break;

        case "image/pjpeg":

     case "image/jpeg":

     case "image/jpg":

        imagejpeg($newImage,$thumb_image_name,90); 

      break;

     case "image/png":

     case "image/x-png":

      imagepng($newImage,$thumb_image_name);  

      break;

    }

    chmod($thumb_image_name, 0777);

    return $thumb_image_name;

}
+1  A: 

How to:

Use file_get_contents() on the returned thumbnail, and then write the results to the database blob field.

$image = file_get_contents($thumbnail);
mysql_query("INSERT INTO tblName (fieldName) VALUES ('{$image}')");

But, you may not want to because:

That being said, I would encourage you to really consider what you're doing. Images pulled from the database won't be cached, from my understanding. Additionally, database connections will remain opened for much longer, using more resources. Also, your database size will grow rapidly, making it more laborious to manage migration down the road.

Jonathan Sampson
+1  A: 

You can store binary data such as an image into a BLOB field in a database. It's as simple as putting the file contents into an INSERT statement. However, I highly suggest you take a look at some of these threads. Storing an image in a database is usually not the best way to go:

User Images: Database or filesystem storage?
Storing images in database: Yea or nay?
Should I store my images in the database or folders?
Would you store binary data in database or folders?
Store pictures as files or or the database for a web app?
Storing a small number of images: blob or fs?
store image in filesystem or database?

zombat
Thank you all, but I actually know what is the advantages and the disadvantages of storing data in the DB. For my purposes this is the best solution.
Pennywise83
A: 

As J. Sampson said, it's not a good way to store images, but it is possible. Do as Jonathan said and then get data out of db, something similar to this:

<?php

$query = mysql_query("SELECT * FROM tbl WHERE id = 'blaa';");
$data = mysql_fetch_array($query);

//Then create image with function
imagecreatefromstring($data['imgdata'])

?>
Martti Laine