用PHP生成等比图像的方法
生活随笔
收集整理的這篇文章主要介紹了
用PHP生成等比图像的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PHP代碼
<?php
/************************************************************************
* 函數名稱:createSmallImg()
* 函數說明:創建等比例圖片
* 輸入參數:$dir 保存路徑$source_img 原圖片名稱$small_ex 縮率圖文件名后綴$maxwidth 最大寬度$maxheight 最大高度
* 說 明:$maxwidth和$maxheight只能傳遞一個,如果傳最大寬度將自動計算高度,如果傳最大高度將自動計算寬度
* 返 回 值:如果創建成功返回文件保存的地址,否則返回false
* 編 寫 者:李小宇
* 編寫時間:2011/8/18
**************************************************************************/
function createSmallImg($dir,$source_img,$small_ex="_s",$maxwidth='',$maxheight='') {if(!empty($maxwidth) && !empty($maxheight)) {return false;}$img_name=substr($source_img,0,-4);$img_ex = strtolower(substr(strrchr($source_img,"."),1));/*注釋的這段用作直接在瀏覽器上顯示圖片$im=imagecreatefromjpeg($file);header("Content-type: image/jpeg");imagejpeg($im);*/switch($img_ex) {case "jpg":$src_img=imagecreatefromjpeg($dir.$source_img);break;case "gif":$src_img=imagecreatefromgif($dir.$source_img);break;case "png":$src_img=imagecreatefrompng($dir.$source_img);break;}$old_width=imagesx($src_img);$old_height=imagesy($src_img);if(!empty($maxheight) && $old_height>=$maxheight) {$new_height=$maxheight;$new_width=round(($old_width*$new_height)/$old_height);} elseif(!empty($maxwidth) && $old_width>=$maxwidth) {$new_width=$maxwidth;$new_height=round(($old_height*$new_width)/$old_width);}if(!empty($new_width) || !empty($new_height)) {$dst_img=imagecreatetruecolor($new_width,$new_height);imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_width,$old_height);$smallname=$dir.$img_name.$small_ex.".".$img_ex;switch($img_ex) {case "jpg":imagejpeg($dst_img,$smallname,100);break;case "gif":imagegif($dst_img,$smallname);break;case "png":imagepng($dst_img,$smallname);break;}}return $smallname;
}
imagecopyresampled比imagecopyresized生成的圖片更清晰
重采樣拷貝部分圖像并調整大小
imagecopyresampled() 將一幅圖像中的一塊正方形區域拷貝到另一個圖像中,平滑地插入像素值,因此,尤其是,減小了圖像的大小而仍然保持了極大的清晰度。成功時返回 TRUE, 或者在失敗時返回 FALSE.
轉載于:https://www.cnblogs.com/phpfans/archive/2011/08/19/2145708.html
總結
以上是生活随笔為你收集整理的用PHP生成等比图像的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ruby 之 Block, Proc,
- 下一篇: 沉香多少钱一克啊?