php使用GD創(chuàng)建保持寬高比縮略圖的方法
本文實(shí)例講述了php使用GD創(chuàng)建保持寬高比縮略圖的方法。分享給大家供大家參考。具體如下:
/** * Create a thumbnail image from $inputFileName no taller or wider than * $maxSize. Returns the new image resource or false on error. * Author: mthorn.net */ function thumbnail($inputFileName, $maxSize = 100) { $info = getimagesize($inputFileName); $type = isset($info['type']) ? $info['type'] : $info[2]; // Check support of file type if ( !(imagetypes() & $type) ) { // Server does not support file type return false; } $width = isset($info['width']) ? $info['width'] : $info[0]; $height = isset($info['height']) ? $info['height'] : $info[1]; // Calculate aspect ratio $wRatio = $maxSize / $width; $hRatio = $maxSize / $height; // Using imagecreatefromstring will automatically detect the file type $sourceImage = imagecreatefromstring(file_get_contents($inputFileName)); // Calculate a proportional width and height no larger than the max size. if ( ($width <= $maxSize) && ($height <= $maxSize) ) { // Input is smaller than thumbnail, do nothing return $sourceImage; } elseif ( ($wRatio * $height) < $maxSize ) { // Image is horizontal $tHeight = ceil($wRatio * $height); $tWidth = $maxSize; } else { // Image is vertical $tWidth = ceil($hRatio * $width); $tHeight = $maxSize; } $thumb = imagecreatetruecolor($tWidth, $tHeight); if ( $sourceImage === false ) { // Could not load image return false; } // Copy resampled makes a smooth thumbnail imagecopyresampled($thumb,$sourceImage,0,0,0,0,$tWidth,$tHeight,$width,$height); imagedestroy($sourceImage); return $thumb; } /** * Save the image to a file. Type is determined from the extension. * $quality is only used for jpegs. * Author: mthorn.net */ function imageToFile($im, $fileName, $quality = 80) { if ( !$im || file_exists($fileName) ) { return false; } $ext = strtolower(substr($fileName, strrpos($fileName, '.'))); switch ( $ext ) { case '.gif': imagegif($im, $fileName); break; case '.jpg': case '.jpeg': imagejpeg($im, $fileName, $quality); break; case '.png': imagepng($im, $fileName); break; case '.bmp': imagewbmp($im, $fileName); break; default: return false; } return true; } $im = thumbnail('temp.jpg', 100); imageToFile($im, 'temp-thumbnail.jpg');
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- php使用GD2繪制幾何圖形示例
- 如何打開php的gd2庫(kù)
- PHP使用GD庫(kù)輸出漢字的方法【測(cè)試可用】
- php利用gd庫(kù)為圖片添加水印
- PHP GD庫(kù)相關(guān)圖像生成和處理函數(shù)小結(jié)
- PHP基于GD庫(kù)的圖像處理方法小結(jié)
- php gd等比例縮放壓縮圖片函數(shù)
- php中使用GD庫(kù)做驗(yàn)證碼
- PHP中使用GD庫(kù)繪制折線圖 折線統(tǒng)計(jì)圖的繪制方法
- php使用GD庫(kù)創(chuàng)建圖片縮略圖的方法
- php使用GD實(shí)現(xiàn)顏色漸變實(shí)例
- php中使用gd庫(kù)實(shí)現(xiàn)下載網(wǎng)頁(yè)中所有圖片
- php中使用gd庫(kù)實(shí)現(xiàn)遠(yuǎn)程圖片下載實(shí)例
- php使用gd2繪制基本圖形示例(直線、圓、正方形)
相關(guān)文章
PHP實(shí)現(xiàn)根據(jù)時(shí)間戳獲取周幾的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)根據(jù)時(shí)間戳獲取周幾的方法,涉及PHP針對(duì)時(shí)間與日期操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02攻克CakePHP系列一 連接MySQL數(shù)據(jù)庫(kù)
請(qǐng)先參閱以前寫的文章以便對(duì)CakePHP有所了解文章,上一篇2008-10-10詳解WordPress中用于更新和獲取用戶選項(xiàng)數(shù)據(jù)的PHP函數(shù)
這篇文章主要介紹了WordPress中用于更新和獲取用戶選項(xiàng)數(shù)據(jù)的PHP函數(shù),分別為對(duì)update_user_option()函數(shù)和get_user_option()函數(shù)用法的講解,需要的朋友可以參考下2016-03-03PHP實(shí)現(xiàn)的文件瀏覽器功能簡(jiǎn)單示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的文件瀏覽器功能,結(jié)合完整實(shí)例形式分析了php針對(duì)目錄與文件的遍歷、判斷、屬性讀取等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09php+iframe 實(shí)現(xiàn)上傳文件功能示例
這篇文章主要介紹了php+iframe 實(shí)現(xiàn)上傳文件功能,結(jié)合實(shí)例形式分析了PHP通過(guò)動(dòng)態(tài)的創(chuàng)建iframe實(shí)現(xiàn)上傳文件的具體步驟、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03用mysql觸發(fā)器自動(dòng)更新memcache的實(shí)現(xiàn)代碼
不錯(cuò)的一篇文章,用于項(xiàng)目中可以帶來(lái)更多的便利,按照方法已經(jīng)調(diào)試成功,可以大大提高項(xiàng)目的速度。2009-10-10PHP實(shí)現(xiàn)的簡(jiǎn)單分頁(yè)類及用法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的簡(jiǎn)單分頁(yè)類及用法,結(jié)合實(shí)例形式分析的PHP分頁(yè)類的功能、定義與具體使用技巧,需要的朋友可以參考下2016-05-05