Monday, May 28, 2012

php image thumbnails



Image Thumbnails Generations


Make 2 folders named "image" and "thumbnails" in your root directory. Insert Medium size or large Images in to images folder Then run the following php file and you will find thumbnails of images you inserted in to images folder in your thumbnails folder.

<?php

$imagefolder='./image';            
$thumbsfolder='./thumbnails';
$errorimage = 1;            
$prefix = "tn_";             
$max_width = 150;            
$max_height = 150;           

echo "<table cellspacing=0><tr><td align=center><a style='color: blue; font-size: 11px; text-decoration: none;' </td></tr>";

@set_time_limit(0);

if (!is_dir($thumbsfolder)){
 mkdir($thumbsfolder, 0777);
}

if (!is_dir($thumbsfolder)){
echo "<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #4</b></font>: Could not create direcotry! Please create it manually!</td></tr></table>";
}

$pics=directory($imagefolder,"jpg,jpeg,png,gif");

$pics=ditchtn($pics,$prefix);

if ($pics[0]!="")
{
 $time_start = microtime_float();
 $ix = 0;
 foreach ($pics as $p)
  {
   createthumb($imagefolder."/".$p,$thumbsfolder."/".$prefix.$p,$max_width,$max_height,$errorimage);
   $ix++;
  }

  $time_end = microtime_float();
  $time = $time_end - $time_start;
  echo "<tr><td style='border: 2px solid green;'>&nbsp;<b>DONE</b>, $ix thumbnails where created in ".round($time,4)." seconds!&nbsp;</td></tr></table>";


}
else
{
 echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #1</b></font>: There are no images in this folder!</td></tr></table>");
 exit;
}



function ditchtn($arr,$thumbname)
{
 foreach ($arr as $item)
  {
   $array = strstr($item, $thumbname);
   if (!$array[1]){
    $tmparr[]=$item;
   }
  }
 return $tmparr;
}


   
function createthumb($name,$filename,$new_w,$new_h,$errorimage)
{
 $system=explode(".",$name);

 if (strpos(strtolower($system[(count($system)-1)]), "jpg") !== FALSE ){
  $src_img=@imagecreatefromjpeg($name);
 }

 if (strpos(strtolower($system[(count($system)-1)]), "jpeg") !== FALSE ){
  $src_img=@imagecreatefromjpeg($name);
 }

 if (strpos(strtolower($system[(count($system)-1)]), "png") !== FALSE ){
  $src_img=@imagecreatefrompng($name);
 }

 if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){
  $src_img=@imagecreatefromgif($name);
 }

if (!$src_img){
 if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){
  if($errorimage == 1){      


   $src_img = imagecreate ("150", "20");
   $bgc = imagecolorallocate ($src_img, 255, 255, 255);
   $tc = imagecolorallocate ($src_img, 255, 0, 0);
   imagefilledrectangle ($src_img, 0, 0, 150, 20, $bgc);
   imagestring ($src_img, 1, 5, 5, "Error loading $name", $tc);

   imagejpeg($src_img,$filename);

   imagedestroy($src_img);
   echo "<tr><td style='border: 2px solid blue; border-bottom: 0px solid blue;'>";
   echo "<a style='color: black; text-decoration: underline;' href='".$filename."'>Thumbnail</a> of <a style='color: black; text-decoration: underline;' href='".$name."'>".strtolower($name)."</a> <font color=red>unsuccessfully</font> created<br>";
   echo "</td></tr>";
  }
  else
  {
   echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #2</b></font>: Your GD lib does not support gif images!</td></tr></table>");
   exit;
  }
 }
 else
 {
  echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #2</b></font>: Unknow filetype!</td></tr></table>");
  exit;
 }
}
else
{
 $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;
 }

 if ($thumb_w == "" OR $thumb_h == ""){
  echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #3</b></font>: Unexpected error!</td></tr></table>");
  exit;
 }

 $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 (strpos(strtolower($system[(count($system)-1)]), "jpg") !== FALSE ){
  imagejpeg($dst_img,$filename);
 }

 if (strpos(strtolower($system[(count($system)-1)]), "jpeg") !== FALSE ){
  imagejpeg($dst_img,$filename);
 }

 if (strpos(strtolower($system[(count($system)-1)]), "png") !== FALSE ){
  imagepng($dst_img,$filename);
 }

 if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){
  imagegif($dst_img,$filename);
 }


 imagedestroy($dst_img);
 imagedestroy($src_img);
 echo "<tr><td style='border: 2px solid blue; border-bottom: 0px solid blue;'>";
 echo "<a style='color: black; text-decoration: underline;' href='".$filename."'>Thumbnail</a> of <a style='color: black; text-decoration: underline;' href='".$name."'>".strtolower($name)."</a> <font color=green>successfully</font> created<br>";
 echo "</td></tr>";
 }
}


function directory($dir,$filters)
{
 $handle=opendir($dir);
 $files=array();
 if ($filters == ""){
  while(($file = readdir($handle))!==false){
   $files[] = $file;
  }
 }
else{
 if ($filters != "")  {
  $filters=explode(",",$filters);
  while (($file = readdir($handle))!==false){
   for ($f=0;$f<count($filters);$f++){
    $system=explode(".",$file);
    if (strtolower($system[(count(system))]) == strtolower($filters[$f])){
     $files[] = $file;
    }
   }
  }
 }
 else
 {
  echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #3</b></font>: Unexpected error!</td></tr></table>");
  exit;
 }
}
closedir($handle);
return $files;
}

function microtime_float()
{
 list($usec, $sec) = explode(" ", microtime());
 return ((float)$usec + (float)$sec);
}
?>

No comments:

Post a Comment