I have a folder with some big images. I would like to create the thumbnails of these images. I have the function that makes that.

I thought there ar etwo ways to make this.
One creating another folder on the web server and the files of the thumbnails .

The second to put into a database into a BLob field the entire binary of each thumb created.

I choosed the second way. I thougt it was more clean and suitable.
But i found some difficulties to insert into database the thubnail such created.
I quote down here the function that creates the tumbnail and the query:

................
if ($handle = opendir($pathToSlides)) {
while (false !== ($file = readdir($handle))) {
if (eregi ("(.)+\.(jp(e){0,1}g$)",$file))
{
$MyThumb = $pathToSlides."/".$file;
$MyThumbFile = thumb($MyThumb, 2, 30);
$insert="insert into $table_cacheThumbs (id_file, id_company,id_event,last_update,file_thumb) Values ('$id_event.$count', '$id_company', '$id_event', Now(), '$MyThumbFile')";/// I think the problem is here

   if (!($result_insert=mysql_query($insert,$link)))
	{
		die(sprintf("error inexecuting $insert internal error %d:%s",mysql_errno(),mysql_error()));
	}
   $count++;
   }

}
closedir($handle);
}
................

function thumb($source, $scale, $quality = 80)
{
/ Check for the image's exisitance /
if (!file_exists($source)) {
echo 'File does not exist!';
}
else {
$size = getimagesize($source); // Get the image dimensions and mime type
$w = $size[0] / $scale; // Width divided
$h = $size[1] / $scale; // Height divided
$resize = imagecreatetruecolor($w, $h); // Create a blank image
/ Check quality option. If quality is greater than 100, return error /
if ($quality > 100) {
echo 'The maximum quality is 100. Quality changes only affect JPEG images.';
}
else {

//use the buffer to take in memory tha image

ob_start();
switch ($size['mime']) {
case 'image/jpeg':
$im = imagecreatefromjpeg($source);
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG
imagejpeg($resize, '', $quality); // Output the new JPEG
break;

                case 'image/png': 
                $im = imagecreatefrompng($source); 
                imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original PNG 
                imagepng($resize, '', $quality); // Output the new PNG 
                break; 
            } 
            imagedestroy($im); 
			$thumbnail = ob_get_contents(); 
			ob_end_clean(); 
			return  $thumbnail;
        } 
    } 
}   
    • [deleted]

    Why keep them in the db? I know this is an age old debate here, but I have had much greater success with keeping images in the filesystem and pointers to them in the db. Just create an images folder, with sub-folders of thumbs and originals, give the files the same name in each, and just put the filename in the db. That's what I do at http://www.bullockfamily.com/photos/

      well... kante whant to point out is that by submitting one image it can create a thumbnail immediately. It is an old style submitting two images one is the big size and the other is the thumbnail

        thanks .....
        that's what i wanted to ear!

        we agree

          2 years later

          u wana look up [man]imagecreatefromgif[/man] , [man]imagecreatefrompng[/man] ,
          [man]imagecreatefromjpg[/man] , [man]imagecopyresized[/man] , [man]imagejpeg[/man] , [man]imagepng[/man] , [man]imagegif[/man]

            Write a Reply...