php使用GD創(chuàng)建保持寬高比縮略圖的方法
更新時間:2015年04月17日 11:04:58 作者:不吃皮蛋
這篇文章主要介紹了php使用GD創(chuàng)建保持寬高比縮略圖的方法,涉及php使用GD庫操作圖片的技巧,需要的朋友可以參考下
本文實例講述了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');
希望本文所述對大家的php程序設計有所幫助。
您可能感興趣的文章:
- php使用GD2繪制幾何圖形示例
- 如何打開php的gd2庫
- PHP使用GD庫輸出漢字的方法【測試可用】
- php利用gd庫為圖片添加水印
- PHP GD庫相關圖像生成和處理函數(shù)小結(jié)
- PHP基于GD庫的圖像處理方法小結(jié)
- php gd等比例縮放壓縮圖片函數(shù)
- php中使用GD庫做驗證碼
- PHP中使用GD庫繪制折線圖 折線統(tǒng)計圖的繪制方法
- php使用GD庫創(chuàng)建圖片縮略圖的方法
- php使用GD實現(xiàn)顏色漸變實例
- php中使用gd庫實現(xiàn)下載網(wǎng)頁中所有圖片
- php中使用gd庫實現(xiàn)遠程圖片下載實例
- php使用gd2繪制基本圖形示例(直線、圓、正方形)
相關文章
攻克CakePHP系列一 連接MySQL數(shù)據(jù)庫
請先參閱以前寫的文章以便對CakePHP有所了解文章,上一篇2008-10-10
詳解WordPress中用于更新和獲取用戶選項數(shù)據(jù)的PHP函數(shù)
這篇文章主要介紹了WordPress中用于更新和獲取用戶選項數(shù)據(jù)的PHP函數(shù),分別為對update_user_option()函數(shù)和get_user_option()函數(shù)用法的講解,需要的朋友可以參考下2016-03-03
用mysql觸發(fā)器自動更新memcache的實現(xiàn)代碼
不錯的一篇文章,用于項目中可以帶來更多的便利,按照方法已經(jīng)調(diào)試成功,可以大大提高項目的速度。2009-10-10

