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; } } }