- 相關(guān)推薦
php生成高清縮略圖實(shí)例
php生成高清縮略圖的方法,較為詳細(xì)的分析了php生成縮略圖時(shí)出現(xiàn)失真的解決方法,并給出了完整實(shí)例進(jìn)行總結(jié)分析,需要的朋友可以參考下。
在使用php的函數(shù)生成縮略圖的使用,縮略圖很多情況下都會(huì)失真,這個(gè)時(shí)候需要有一些對(duì)應(yīng)的解決方法。
1.用imagecreatetruecolor和imageCopyreSampled函數(shù)分別取代imagecreate和imagecopyresized
2.給imagejpeg的第三個(gè)參數(shù)帶上100(例:imagejpeg($ni,$toFile,100))
下面是具體的函數(shù)
function CreateSmallImage( $OldImagePath, $NewImagePath, $NewWidth=154, $NewHeight=134)
{
// 取出原圖,獲得圖形信息getimagesize參數(shù)說明:0(寬),1(高),2(1gif/2jpg/3png),3(width="638" height="340")
$OldImageInfo = getimagesize($OldImagePath);
if ( $OldImageInfo[2] == 1 ) $OldImg = @imagecreatefromgif($OldImagePath);
elseif ( $OldImageInfo[2] == 2 ) $OldImg = @imagecreatefromjpeg($OldImagePath);
else $OldImg = @imagecreatefrompng($OldImagePath);
// 創(chuàng)建圖形,imagecreate參數(shù)說明:寬,高
$NewImg = imagecreatetruecolor( $NewWidth, $NewHeight );
//創(chuàng)建色彩,參數(shù):圖形,red(0-255),green(0-255),blue(0-255)
$black = ImageColorAllocate( $NewImg, 0, 0, 0 ); //黑色
$white = ImageColorAllocate( $NewImg, 255, 255, 255 ); //白色
$red = ImageColorAllocate( $NewImg, 255, 0, 0 ); //紅色
$blue = ImageColorAllocate( $NewImg, 0, 0, 255 ); //藍(lán)色
$other = ImageColorAllocate( $NewImg, 0, 255, 0 );
//新圖形高寬處理
$WriteNewWidth = $NewHeight*($OldImageInfo[0] / $OldImageInfo[1]); //要寫入的高度
$WriteNewHeight = $NewWidth*($OldImageInfo[1] / $OldImageInfo[0]); //要寫入的寬度
//這樣處理圖片比例會(huì)失調(diào),但可以填滿背景
if ($OldImageInfo[0] / $NewWidth > $org_info[1] / $NewHeight) {
$WriteNewWidth = $NewWidth;
$WriteNewHeight = $NewWidth / ($OldImageInfo[0] / $OldImageInfo[1]);
} else {
$WriteNewWidth = $NewHeight * ($OldImageInfo[0] / $OldImageInfo[1]);
$WriteNewHeight = $NewHeight;
}
//以$NewHeight為基礎(chǔ),如果新寬小于或等于$NewWidth,則成立
if ( $WriteNewWidth <= $NewWidth ) {
$WriteNewWidth = $WriteNewWidth; //用判斷后的大小
$WriteNewHeight = $NewHeight; //用規(guī)定的大小
$WriteX = floor( ($NewWidth-$WriteNewWidth) / 2 ); //在新圖片上寫入的X位置計(jì)算
$WriteY = 0;
} else {
$WriteNewWidth = $NewWidth; // 用規(guī)定的大小
$WriteNewHeight = $WriteNewHeight; //用判斷后的大小
$WriteX = 0;
$WriteY = floor( ($NewHeight-$WriteNewHeight) / 2 ); //在新圖片上寫入的X位置計(jì)算
}
//舊圖形縮小后,寫入到新圖形上(復(fù)制),imagecopyresized參數(shù)說明:新舊, 新xy舊xy, 新寬高舊寬高
@imageCopyreSampled( $NewImg, $OldImg, $WriteX, $WriteY, 0, 0, $WriteNewWidth, $WriteNewHeight, $OldImageInfo[0], $OldImageInfo[1] );
//保存文件
// @imagegif( $NewImg, $NewImagePath );
@imagejpeg($NewImg, $NewImagePath, 100);
//結(jié)束圖形
@imagedestroy($NewImg);
}
CreateSmallImage("./images/jiexie.jpg","./images/jiexie.small.jpg",200,300);
CreateSmallImage("./images/jiexie.jpg","./images/jiexie.middle.jpg",400,500);
希望本文所述對(duì)大家php程序設(shè)計(jì)有所幫助。
【php生成高清縮略圖實(shí)例】相關(guān)文章:
php生成圖片縮略圖的方法03-31
php生成縮略圖的兩種方法12-05
php生成縮略圖的兩種方法(代碼)12-07
php查詢mysql的實(shí)例03-29
php簡(jiǎn)單偽靜態(tài)實(shí)例12-02
PHP處理密碼的幾種方式實(shí)例03-29