This file help you generate thumbnail for all pictures inside 1 folder, and rename it, can move it to thumbnail folder …
First, we find all files was thumbnail pictures, define by name in that folder:
function hhfindtb($arr,$thumbname) { foreach ($arr as $item) { if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;} } return $tmparr; }
Then, read the content of directory, filter out all images files, and creatthumb functions:
function directory($dir,$filters) { $handle=opendir($dir); $files=array(); if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}} if ($filters != "all") { $filters=explode(",",$filters); while (($file = readdir($handle))!==false) { for ($f=0;$f<sizeof($filters);$f++): $system=explode(".",$file); if ($system[1] == $filters[$f]){$files[] = $file;} endfor; } } closedir($handle); return $files; } function createthumb($name,$filename,$new_w,$new_h) { $system=explode(".",$name); if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);} if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);} $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if (preg_match("/png/",$system[1])) { imagepng($dst_img,$filename); } else { imagejpeg($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); }
That’s all, now you can generate thumbnail for all files by:
$imagefolder='.'; $thumbsfolder='.'; $pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG"); $pics=hhfindtb($pics,"thumb_"); if ($pics[0]!="") { foreach ($pics as $p) { createthumb($p,"thumb_".$p,150,150); } }
You can use another thumbnail function:
function make_thumb($src,$dest,$desired_width) { /* read the source image */ $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); /* find the "desired height" of this thumbnail, relative to the desired width */ $desired_height = floor($height*($desired_width/$width)); /* create a new, "virtual" image */ $virtual_image = imagecreatetruecolor($desired_width,$desired_height); /* copy source image at a resized size */ imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image,$dest); }