CMSimple Drupal Yii 內容管理系統三方案 - 提升台灣中小企業競爭力

目前位置:   首頁 > Program Talk > regenerate.php

regenerate.php

這個程式以遞迴方式進入某一目錄讀取所有目錄與子目錄中的圖檔, 並且能夠加入浮水印檔案或者日期註記, 最後使用者可以選擇將各圖檔縮圖後存檔.

<?php
$photo_width="1024";
ls_recursive2("./source_files/");
function ls_recursive2($dir)
{
global $photo_width;
  if (is_dir($dir))
  {
   $files = scandir($dir);
   foreach ($files as $file)
   {
     $rand_id=makeRandomPassword();
     $currentfile = $dir . "/" . $file;  
     $last_dir = "";
     // Calculate they identation.
     $count = substr_count($currentfile, '/');
     $minus_count = substr_count($_SERVER['DOCUMENT_ROOT'], '/');
     $count -= ($minus_count + 2);
     for($p = 0; $p < $count; $p++)
     {
       $last_dir .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
     }
              
     if (is_dir($currentfile))
     {
       if ($file != '.' && $file != '..')
     {
       $last_dir .= "<img src='images/folder.gif' alt='' align='middle' width='16' height='16' border='0'>&nbsp;<a href=\"javascript:go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>";
         //echo $last_dir;
$filename=substr($currentfile, strrpos($currentfile, '/'));
echo "<br>";
$is_graphics=eregi(".jpg|.JPG",$file);
if($is_graphics)
{
reduce_graphics($dir,"new_files",$file,$rand_id,$photo_width);
echo $currentfile;
echo "<br>";
}
         ls_recursive2($currentfile);
       }
     }
     else
     {
       $last_dir .= "<img src='images/file.gif' alt='' align='middle' width='16' height='16' border='0'>&nbsp;<a href=\"javascript:go('" . $currentfile . "')\">". substr($currentfile, strrpos($currentfile, '/')) . "</a><br>";
       //echo $last_dir;
echo "<br>";
$is_graphics=eregi(".jpg|.JPG",$file);
if($is_graphics)
{
reduce_graphics($dir,"new_files",$file,$rand_id,$photo_width);
echo $currentfile;
echo "<br>";
}
     }
   }
  }
}
function reduce_graphics($src_dir,$dst_dir,$entry,$photo_theme,$new_w)
{
$thumb_path=$dst_dir;
$image_stats = GetImageSize($src_dir."/".$entry);
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
// 若原圖小於 1024 則保留原圖大小
if($imagewidth < $new_w){
    $new_w = $imagewidth;
    $new_h = $imageheight;
}else{
    $ratio = ($imagewidth / $new_w);
    $new_h = round($imageheight / $ratio);
}
if (!file_exists($thumb_path."/".$entry))  
{
	$file_time=date("Y_m_d_His",filemtime($src_dir."/".$entry));
 	if ($img_type=="2") 
	{
	$watermarkimage_stats = GetImageSize($file_path."/watermark.jpg");
	$watermarkwidth = $watermarkimage_stats[0];
	$watermarkheight = $watermarkimage_stats[1];
	$watermark_img=imagecreatefromjpeg($file_path."/watermark.jpg");
 	$src_img = imagecreatefromjpeg($src_dir."/".$entry);
 	$dst_img = imagecreatetruecolor($new_w,$new_h);
	imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
	
	//$newthumbfile=$thumb_path."/".$entry;
	//$newthumbfile name is combined with the filetime and photo theme
	//$newthumbfile=$thumb_path."/".$file_time."_".$photo_theme.".jpg";
    // 改用原始檔案儲存檔案
    $newthumbfile=$thumb_path."/".$entry;
    // 關閉 watermak 使用
	//imagecopymerge($dst_img,$watermark_img,0,0,0,0,$watermarkwidth,$watermarkheight,50);
	//put date text on the photo
	$orange = imagecolorallocate($im, 220, 210, 60);
	$px    = (imagesx($dst_img) - 7.5 * strlen($file_time)) / 2;
	//imagestring(image,font,x,y,string,color);
    // 不要加上日期
	//imagestring($dst_img, 3, $px, 9,$file_time, $orange);
    // 使用 quality 70
	imagejpeg($dst_img,$newthumbfile);
 	}
	 elseif  ($img_type=="3")
	 {
 	$dst_img=imagecreatetruecolor($new_w,$new_h);
 	$src_img=imagecreatefrompng($file_path."/".$entry);
	imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
	$newthumbfile=$thumb_path."/".$entry;
 	imagepng($dst_img,$newthumbfile);
 	}
	 elseif  ($img_type=="1")
	 {
	//can not handle gif files
 	//$cfg_thumb_url=$cfg_fullsizepics_url;
 	}
}
imagedestroy($dst_img);
return TRUE;
 }
function makeRandomPassword() {
  $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  srand((double)microtime()*1000000);
      $i = 0;
      while ($i <= 3) {
            $num = rand() % 33;
            $tmp = substr($salt, $num, 1);
            $pass = $pass . $tmp;
            $i++;
      }
      return $pass;
}
?>

 

Powered by CMSimple_XH| Template: ge-webdesign.de| 登入