星期一, 八月 20, 2007

PHP 等比例縮圖程式

兩個 function, 一個算比例要多少(getResizePercent), 另一個 function 依照比例將圖片縮小, 縮到剛好符合預計縮小的其中一邊的尺寸(ImageResize).

使用上只要將這兩個 function  一起放進程式裡面, 直接呼叫 ImageResize(), 把參數傳進去即可.

ImageResize function

  • Usage: ImageResize('ram/xxx.jpg', 'ram/ooo.jpg');
  • 抓取要縮圖的比例, 下述只處理 jpeg
  • $from_filename : 來源路徑, 檔名, ex: /tmp/xxx.jpg
  • $save_filename : 縮圖完要存的路徑, 檔名, ex: /tmp/ooo.jpg
  • $in_width : 縮圖預定寬度
  • $in_height: 縮圖預定高度
  • $quality  : 縮圖品質(1~100)

getResizePercent function

  • 抓取要縮圖的比例
  • $source_w : 來源圖片寬度
  • $source_h : 來源圖片高度
  • $inside_w : 縮圖預定寬度
  • $inside_h : 縮圖預定高度
  • Test:
  • $v = (getResizePercent(1024, 768, 400, 300));
  • echo 1024 * $v . "\n";
  • echo  768 * $v . "\n";

程式原始碼如下:

相關標籤

this is comment icon 好用的程序 [回覆]

谢谢,共享.收藏了.
很实用的程序.

Comment by life10000 (08/21/2007 10:55)

this is comment icon 回 life10000 [回覆]

很高興您喜歡 :)
PS: 您的網站在 Firefox 會都疊在一起耶. XD

Comment by Tsung (08/21/2007 11:23)

this is comment icon 太晚看到了...orz [回覆]

最近也自己寫了一個

Comment by jiing (09/01/2007 15:30)

this is comment icon 回 jiing [回覆]

辛苦了. Orz...

Comment by Tsung (09/03/2007 20:21)

this is comment icon 請問一下: [回覆]

這是把尺寸縮小而已還是連檔案也縮小了?
因我想別人上傳的圖檔其檔案縮小一份作為預覽用,以加速網頁傳輸

Comment by QQ (02/25/2008 16:30)

this is comment icon 回 QQ [回覆]

這個並不會影響到你的檔案, $save_filename 決定縮圖後要存檔的檔名, 大概就只有這樣子而已 :)

Comment by Tsung (02/25/2008 16:46)

this is comment icon [回覆]

請問有長寬的限制嗎?
我測試時,如果圖檔ㄉ長寬超過一定尺寸就沒反應了.....

Comment by kink (12/25/2008 16:33)

this is comment icon 回 kink [回覆]

沒有, 可能是長度和寬度的值寫反了?

Comment by Tsung (12/25/2008 22:06)

this is comment icon [回覆]

這個超好用的 ^^
請問這可以縮GIF或PNG檔嗎?
我縮GIF或PNG檔都會怪怪的, 整張圖都會變黑色的

Comment by tenlans (01/06/2009 09:14)

this is comment icon 回 tenlans [回覆]

可以, 但是程式的 $image = imagecreatefromjpeg($from_filename); 要改成

$function_name = 'imagecreatefrom'.$sub_name;
$image = $function_name($filename);

這樣子~ :)

Comment by Tsung (01/06/2009 09:20)

this is comment icon 縮jpg以外 ? [回覆]

您好,把 $image = imagecreatefromjpeg($from_filename);改成

$function_name = 'imagecreatefrom'.$sub_name;
$image = $function_name($filename);

會產生錯誤耶? 就下面這行出錯..
imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

Comment by azure (11/03/2009 19:59)

this is comment icon 回 azure [回覆]

你要縮的圖片是哪種格式? png / gif 應該都是可以吃才對.

Comment by Tsung (11/03/2009 20:01)

this is comment icon 打擾了 [回覆]

不好意思,是我沒看仔細
$image = $function_name($filename);
改成
$image = $function_name($from_filename);
就可以~感謝

Comment by azure (11/04/2009 13:42)

this is comment icon 回 azure [回覆]

嗯嗯~ :)

Comment by Tsung (11/04/2009 13:56)

this is comment icon [回覆]

請問設定了縮圖的長400與寬400後,若一邊的長度不及設定的尺寸時,能否用指定的顏色將該邊長補齊至400?

Comment by Denise (01/07/2010 13:46)

this is comment icon 回 Denise [回覆]

這就不是這種作法囉~ 這變成你要弄張底色, 然後把這圖片縮完的疊上去, 再合成一張圖.
如果是 Web, 建議用 CSS 來做這件事. :)

Comment by Tsung (01/07/2010 13:55)

this is comment icon 謝謝 [回覆]

這個縮圖函數非常實用
希望能繼續發表 新的文章
期待你新的文章

Comment by @@ (02/01/2010 14:15)
Add this page to del.icio.us

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~