Php Image Resize圖片大小調(diào)整的函數(shù)代碼
更新時(shí)間:2011年01月17日 22:31:56 作者:
php下可以將圖片大小調(diào)整的函數(shù)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
if($dst_width <1 || $dst_height <1) {
echo "params width or height error !";
exit();
}
if(!file_exists($src_file)) {
echo $src_file . " is not exists !";
exit();
}
$type=exif_imagetype($src_file);
$support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
if(!in_array($type, $support_type,true)) {
echo "this type of image does not support! only support jpg , gif or png";
exit();
}
switch($type) {
case IMAGETYPE_JPEG :
$src_img=imagecreatefromjpeg($src_file);
break;
case IMAGETYPE_PNG :
$src_img=imagecreatefrompng($src_file);
break;
case IMAGETYPE_GIF :
$src_img=imagecreatefromgif($src_file);
break;
default:
echo "Load image error!";
exit();
}
$src_w=imagesx($src_img);
$src_h=imagesy($src_img);
$ratio_w=1.0 * $dst_width/$src_w;
$ratio_h=1.0 * $dst_height/$src_h;
if ($src_w<=$dst_width && $src_h<=$dst_height) {
$x = ($dst_width-$src_w)/2;
$y = ($dst_height-$src_h)/2;
$new_img=imagecreatetruecolor($dst_width,$dst_height);
imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
} else {
$dstwh = $dst_width/$dst_height;
$srcwh = $src_w/$src_h;
if ($ratio_w <= $ratio_h) {
$zoom_w = $dst_width;
$zoom_h = $zoom_w*($src_h/$src_w);
} else {
$zoom_h = $dst_height;
$zoom_w = $zoom_h*($src_w/$src_h);
}
$zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
$new_img=imagecreatetruecolor($dst_width,$dst_height);
$x = ($dst_width-$zoom_w)/2;
$y = ($dst_height-$zoom_h)/2+1;
imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
}
}
相關(guān)文章
WordPress導(dǎo)航菜單的滾動(dòng)和淡入淡出效果的實(shí)現(xiàn)要點(diǎn)
這篇文章主要介紹了WordPress導(dǎo)航菜單的滾動(dòng)和淡入淡出效果的實(shí)現(xiàn)要點(diǎn),講解了根據(jù)WordPress默認(rèn)主題的PHP編寫技巧,需要的朋友可以參考下2015-12-12php 防止單引號(hào),雙引號(hào)在接受頁(yè)面轉(zhuǎn)義
在使用php的過程中,我們需要使用單引號(hào)和雙引號(hào),所以需要轉(zhuǎn)義一下2008-07-07PHP zlib擴(kuò)展實(shí)現(xiàn)頁(yè)面GZIP壓縮輸出
GZIP(GNU-ZIP)是一種壓縮技術(shù)。經(jīng)過GZIP壓縮后頁(yè)面大小可以變?yōu)樵瓉?lái)的30%甚至更小。這樣用戶瀏覽的時(shí)候就會(huì)感覺很爽很愉快!2010-06-06PHP中l(wèi)ist()函數(shù)用法實(shí)例簡(jiǎn)析
這篇文章主要介紹了PHP中l(wèi)ist()函數(shù)用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了list()函數(shù)的功能,參數(shù)定義,使用技巧及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01如何讓thinkphp在模型中自動(dòng)完成session賦值小教程
在項(xiàng)目中遇到一個(gè)問題,需要讓thinkphp在模型中自動(dòng)完成session賦值,經(jīng)過一番研究,終于實(shí)現(xiàn),下面記錄一下,另附上thinkPHP的session的相關(guān)知識(shí)2014-09-09Windows下XDebug 手工配置與使用說(shuō)明
XDebug 是一個(gè) php 代碼的調(diào)試工具, 對(duì)程序員調(diào)試程序有很大的幫助, 下面簡(jiǎn)單說(shuō)下 XDebug 在 Windows 下的配置.2010-07-07