TShopping

 找回密碼
 註冊
搜索
查看: 1920|回復: 0
打印 上一主題 下一主題

[教學] PHP實用小例子:批量壓縮圖片

[複製鏈接]
跳轉到指定樓層
1#
發表於 2011-8-7 13:08:20 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
進度顯示頁代碼
  1. <?php
  2. ini_set("memory_limit","120M");
  3. include("functions.php");
  4. $dir="image/";

  5. if ($dh = @opendir($dir)) {

  6. set_time_limit(0);
  7. $width = 800;   //顯示的進度條長度,單位 px
  8. $total = count(scandir($dir)); //總共需要操作的記錄數
  9. $pix = $width / $total; //每條記錄的操作所佔的進度條單位長度
  10. $progress = 0;
  11. //echo $totel; //當前進度條長度
  12. ?>
  13. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  14. <html>
  15. <head>
  16.     <title>降低品質</title>
  17.     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  18.     <style>
  19.     body, div input { font-family: Tahoma; font-size: 14px;  }
  20.         </style>
  21.     <script language="JavaScript">
  22.     <!--
  23.     function updateProgress(sMsg, iWidth)
  24.     {  
  25.         document.getElementById("status").innerHTML = sMsg;
  26.         document.getElementById("progress").style.width = (iWidth-8) + "px";
  27.         document.getElementById("percent").innerHTML = parseInt(iWidth / <?php echo $width; ?> * 100) + "%";
  28.      }
  29.     //-->
  30.     </script>  
  31.    
  32. </head>

  33. <body>
  34. <div style="padding: 8px; border:none;left:expression((body.offsetWidth-this.offsetWidth)/2);position:absolute; width: <?php echo $width+8; ?>px" >
  35.     <div><font color="gray">→</font></div>
  36.     <div style="padding:0; margin:3px; background-color: white; border: 1px solid #d6d6d6; width: <?php echo $width; ?>px">
  37.         <div id="progress" style="padding:1px; margin:3px; background: url(bg.gif) center left repeat-x;border:0;width:0px;text-align:center;height:16px"></div>            
  38.     </div>
  39.     <div id="status">&nbsp;</div>
  40.     <div id="percent" style="position: relative; top: -44px; text-align: center; font-weight: bold; font-size:12px; color:#0099ff">0%</div>
  41. </div>
  42. <?php
  43. while (($file = readdir($dh)) !== false) {
  44.                      if ($file != "." && $file != "..") {
  45.                         
  46.                          if(@filetype($dir."/".$file)=="file"){
  47.                          $ff=$dir."/".$file;
  48.                          quanty($ff);
  49.                          }
  50.                         

  51. flush();    //將輸出發送給客戶端瀏覽器
  52. //foreach ($ff as $user) {  
  53.    //   在此處使用空循環模擬較為耗時的操作,實際應用中需將其替換;
  54.     //   如果你的操作不耗時,我想你就沒必要使用這個腳本了 :)

  55. ?>
  56. <script language="JavaScript">
  57.     updateProgress("… <?php echo $file; ?> ", <?php echo min($width, intval($progress)); ?>);
  58. </script>
  59. <?php
  60.     //flush();    //將輸出發送給客戶端瀏覽器,使其可以立即執行服務器端輸出的 JavaScript 程序。
  61.    $progress += $pix;     
  62.                          }
  63.                
  64.                 }
  65.                 //}//end foreach
  66. //   最後將進度條設置成最大值 $width,同時顯示操作完成
  67. ?>
  68. <script language="JavaScript">
  69.     updateProgress("over! ", <?php echo $width; ?>);
  70.         //window.location.href='index.php';
  71. </script>

  72. </body>
  73. </html>
  74. <?php

  75. }
  76. ?>
複製代碼



壓縮圖片函數

  1. function quanty($filename){
  2.   if(is_file($filename)) {
  3.       $size = getimagesize($filename);
  4.       $fileInfo = pathinfo($filename);
  5.       $ext = strtolower($fileInfo["extension"]);
  6.           $dst_filename =  $fileInfo["dirname"]."/".$fileInfo["basename"];
  7.       $max_width = $size[0];
  8.       $max_height = $size[1];
  9.        if($ext == 'jpg' || $ext == 'jpeg'||$ext == 'png'||$ext == 'gif'){
  10.         if ($ext == 'jpg' || $ext == 'jpeg') {
  11.           $src_img = imagecreatefromjpeg($filename) or die( 'Cannot load input JPEG image' );
  12.         } else if ($ext == 'png') {
  13.           $src_img = imagecreatefrompng($filename) or die( 'Cannot load input PNG image' );
  14.         } else if ($ext == 'gif') {
  15.           $src_img = imagecreatefromgif($filename) or die( 'Cannot load input GIF image' );
  16.         }

  17.         $dst_img = imagecreatetruecolor($max_width, $max_height);
  18.         imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $max_width, $max_height, $size[0], $size[1]);
  19.         imagejpeg($dst_img, $dst_filename, 85);
  20.         imagedestroy($src_img);
  21.         imagedestroy($dst_img);
  22.                
  23.                 }
  24.       }

  25. }
複製代碼


 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-5-2 11:51 , Processed in 0.069368 second(s), 18 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表