本文實(shí)例講述了PHP實(shí)現(xiàn)的自定義圖像居中裁剪函數(shù)。分享給大家供大家參考,具體如下:
圖像居中裁減的大致思路:
1.首先將圖像進(jìn)行縮放,使得縮放后的圖像能夠恰好覆蓋裁減區(qū)域。(imagecopyresampled — 重采樣拷貝部分圖像并調(diào)整大小)
2.將縮放后的圖像放置在裁減區(qū)域中間。(imagecopy — 拷貝圖像的一部分)
3.裁減圖像并保存。(imagejpeg | imagepng | imagegif — 輸出圖象到瀏覽器或文件)
具體代碼:
//==================縮放裁剪函數(shù)==================== /** * 居中裁剪圖片 * @param string $source [原圖路徑] * @param int $width [設(shè)置寬度] * @param int $height [設(shè)置高度] * @param string $target [目標(biāo)路徑] * @return bool [裁剪結(jié)果] */ function image_center_crop($source, $width, $height, $target) { if (!file_exists($source)) return false; /* 根據(jù)類型載入圖像 */ switch (exif_imagetype($source)) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($source); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($source); break; case IMAGETYPE_GIF: $image = imagecreatefromgif($source); break; } if (!isset($image)) return false; /* 獲取圖像尺寸信息 */ $target_w = $width; $target_h = $height; $source_w = imagesx($image); $source_h = imagesy($image); /* 計(jì)算裁剪寬度和高度 */ $judge = (($source_w / $source_h) > ($target_w / $target_h)); $resize_w = $judge ? ($source_w * $target_h) / $source_h : $target_w; $resize_h = !$judge ? ($source_h * $target_w) / $source_w : $target_h; $start_x = $judge ? ($resize_w - $target_w) / 2 : 0; $start_y = !$judge ? ($resize_h - $target_h) / 2 : 0; /* 繪制居中縮放圖像 */ $resize_img = imagecreatetruecolor($resize_w, $resize_h); imagecopyresampled($resize_img, $image, 0, 0, 0, 0, $resize_w, $resize_h, $source_w, $source_h); $target_img = imagecreatetruecolor($target_w, $target_h); imagecopy($target_img, $resize_img, 0, 0, $start_x, $start_y, $resize_w, $resize_h); /* 將圖片保存至文件 */ if (!file_exists(dirname($target))) mkdir(dirname($target), 0777, true); switch (exif_imagetype($source)) { case IMAGETYPE_JPEG: imagejpeg($target_img, $target); break; case IMAGETYPE_PNG: imagepng($target_img, $target); break; case IMAGETYPE_GIF: imagegif($target_img, $target); break; } // return boolval(file_exists($target));//PHP5.5以上可用boolval()函數(shù)獲取返回的布爾值 return file_exists($target)?true:false;//兼容低版本PHP寫法 }
//==================函數(shù)使用方式==================== // 原始圖片的路徑 $source = '../source/img/middle.jpg'; $width = 480; // 裁剪后的寬度 $height = 480;// 裁剪后的高度 // 裁剪后的圖片存放目錄 $target = '../source/temp/resize.jpg'; // 裁剪后保存到目標(biāo)文件夾 if (image_center_crop($source, $width, $height, $target)) { echo "原圖1440*900為:<img src='$source'>"; echo "<hr>"; echo "修改后圖片480*480為:<img src='$target'>"; }
運(yùn)行效果:
原圖1440*900為:
修改后圖片480*480為:

同理,480*320,、800*600等尺寸的圖片只需修改相應(yīng)參數(shù)即可。
附:代碼測(cè)試中遇到的問題
報(bào)錯(cuò):call an undefined function exif_imagetype()
解決方法:
打開擴(kuò)展 extension=php_exif.dll
并將extension=php_mbstring.dll
,放到extension=php_exif.dll
前邊
另:boolval()
函數(shù)為PHP5.5版本以上才能使用的函數(shù),本文測(cè)試代碼中為兼容低版本,使用如下語句代替:
return file_exists($target)?true:false;
PS:這里再為大家推薦幾款相關(guān)的圖片在線工具供大家參考使用:
在線圖片格式轉(zhuǎn)換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
在線PS圖像處理工具:
http://tools.jb51.net/aideddesign/webps
ICO圖標(biāo)在線生成工具:
http://tools.jb51.net/aideddesign/ico_img
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》及《PHP基本語法入門教程》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。