Kashif Waheed
Kashif Waheed

Reputation: 597

create thumbnail of image and then save it into database

i am creating a thumbnail of an image and then save it into the database but i don't know how to do so. here is my code

 function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
 {
        $pathToImages = "../uploads/images/";
        // open the directory
        $dir = opendir( $pathToImages );

        // loop through it, looking for any/all JPG files:
        while (false !== ($fname = readdir( $dir ))) {
          // parse path for the extension

          $info = pathinfo($pathToImages . $fname);
          // continue only if this is a JPEG image
          if ( strtolower($info['extension']) == 'jpg' ) 
          {

            $pathToThumbs = "../uploads/images/thumbs/";
        //echo "Creating thumbnail for {$fname} <br />";

        // load image and get image size
        $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
        $width = imagesx( $img );
        $height = imagesy( $img );

        // calculate thumbnail size
        $new_width = $thumbWidth;
        $new_height = floor( $height * ( $thumbWidth / $width ) );

        // create a new temporary image
        $tmp_img = imagecreatetruecolor( $new_width, $new_height );

        // copy and resize old image into new image 
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );

        // save thumbnail into a file
        imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
         $thumbimage =  $fname;
          }
        }
        // close the directory
closedir( $dir );

 }
 createThumbs("/uploads/documents","/uploads/images/thumbs/",100);

creation of thumbnail is fine but now i dont know how to save it in db(lack of skills)

Upvotes: 0

Views: 1698

Answers (2)

Marco God&#237;nez
Marco God&#237;nez

Reputation: 3470

You can use the Dropbox API that automatically generates thumbnails. I think that save binary files in a database is the worst mistake we can make. Especially for the performance and files management becomes difficult.

Upvotes: 0

mohammad falahat
mohammad falahat

Reputation: 748

why you don't save the results in file?

i think you must select the text type in database field, then save the content of image there,

and read the data in a diffrent php file with "Content-type: image/jpg" header.

for example:

$query = "SELECT image FROM youtablename WHERE id = 10";
$result = mysql_query($query) or die('Error, query failed');
$content=mysql_result($result,0,"image");
header("Content-type: image/jpg");
echo $content;

Upvotes: 1

Related Questions