首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
回上頁
PHP + ImageMagick Codes: PNG2JPG, JPG Quality, Shrink JPG
Type(Type) 2014/6/29 16:15

PHP + ImageMagick Codes: PNG2JPG, JPG Quality, Shrink JPG Quality




PHP Read All files in this directory and the proceed the following Image processing

1. convert_PNG_to_JPG($source): Convert PNG image files to JPG image files within this directory
2. convert_jpg_to_quality_90($source): Convert JPG quality to 90 within same folder
3. shrink_to_goolge_800($source): Shrink all images files in this folder to max 800 width or 800 height


代碼:

<?PHP

function convert_PNG_to_JPG($source)
{          
   // $source = "1.jpg";
   $convert = "D:\ImageMagick-6.6.3-Q16\convert.exe";  // define in constant.php   
   
   /* 一律 90 品質 */
   if (filesize($source) > 524288)
   {
      $dest = str_replace('.png', '.jpg', $source);
      echo "Q90 $dest \n";
      exec($convert.' '.$source.' -strip -quality 90 '.$dest);
   }
   else
   {
      echo ".";
   }
}

function convert_jpg_to_quality_90($source)
{          
   // $source = "1.jpg";
   $convert = "D:\ImageMagick-6.6.3-Q16\convert.exe";  // define in constant.php   
   
   /* 一律 90 品質 */
   if (filesize($source) > 524288)
   {
      echo "Q90 $source \n";
      exec($convert.' '.$source.' -strip -quality 90 '.$source);
   }
   else
   {
      echo ".";
   }
}

function shrink_to_goolge_800($source)
{          
   // $source = "1.jpg";
   $mid = 800;
   $convert = "D:\ImageMagick-6.6.3-Q16\convert.exe";  // define in constant.php   
   $size = getimagesize($source);

   if ($size[0] > 800 || $size[1] > 800)
   {
      echo $source."\n";
       usleep(10);

      if ($size[0] > $size[1])
      {
         $toWWW = ( $size[0] >= $mid ? $mid : $size[0] );
         //echo $convert.' '.$source.' -strip -resize '.$toWWW.'x '.$source;   
         exec($convert.' '.$source.' -resize '.$toWWW.'x '.$source);   
      }
      else
      {
         $toHHH = ( $size[1] >= $mid ? $mid : $size[1] );
         // echo $convert.' '.$source.' -strip -resize x'.$toHHH.' '.$source;         
         exec($convert.' '.$source.' -resize x'.$toHHH.' '.$source);         
      }
   }
   else
   {
      echo "-";
   }
}

function prepare_readidr($pid)
{
   /* 準備清單 */
   $file_list = array();
   if ($handle = opendir("."))
   {
       while (false !== ($filedir = readdir($handle)))
       {
          /* 確定目錄 */
          if (is_dir($filedir))
          {
             /* 再從目錄讀出檔案 */
             if ($handle2 = opendir($filedir))
             while (false !== ($files = readdir($handle2)))
             {
                /* 不能是目錄 */
                if (!is_dir($files))
                {
                   $z = $filedir.'\\'.$files;
                   //if (filemtime($z) > (time() - 100))
                   //   echo '.';
                   //else
                   shrink_to_goolge_800($z);
                   
                   // convert_jpg_to_quality_90($z)
                   
                   // convert_PNG_to_JPG($z)
                }
             }
          }
      }
   }
}


/* main */
prepare_readidr();



?>   




(3,850 views)
[更多討論] 討論區 Windows, Linux, Perl, PHP, C/C++, Driver, Web 理論、應用、硬體、軟體
回上頁

"PHP + ImageMagick Codes: PNG2JPG, JPG Quality, Shrink JPG" 傳統頁面(電腦版)

首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
傳統桌面版 [ 登入/註冊 ]
© Vovo2000.com Mobile Version 小哈手機版 2024