基于GD庫的縮略圖生成PHP類
<?php /**
- 圖片縮略類
- @author foyon /
class ImgThumb{
/**
* 生成的大小完全符合要求,但是會截圖
* @param unknown_type $src_file
* @param unknown_type $dst_file
* @param unknown_type $new_width
* @param unknown_type $new_height
*/
function ImageResize($src_file, $dst_file , $new_width , $new_height) {
$new_width= intval($new_width);
$new_height=intval($new_height);
if($new_width <1 || $new_height <1) {
echo "params width or height error !";
exit();
}
$src_img=imagecreatefromstring($src_file);
$w=imagesx($src_img);
$h=imagesy($src_img);
$ratio_w=1.0 * $new_width / $w;
$ratio_h=1.0 * $new_height / $h;
$ratio=1.0;
// 生成的圖像的高寬比原來的都小,或都大 ,原則是 取大比例放大,取大比例縮小(縮小的比例就比較小了)
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
if($ratio_w < $ratio_h) {
$ratio = $ratio_h ; // 情況一,寬度的比例比高度方向的小,按照高度的比例標準來裁剪或放大
}else {
$ratio = $ratio_w ;
}
// 定義一個中間的臨時圖像,該圖像的寬高比 正好滿足目標要求
$inter_w=(int)($new_width / $ratio);
$inter_h=(int)($new_height / $ratio);
$inter_img=imagecreatetruecolor($inter_w , $inter_h);
//var_dump($inter_img);
imagecopy($inter_img, $src_img, 0,0,0,0,$inter_w,$inter_h);
// 生成一個以最大邊長度為大小的是目標圖像$ratio比例的臨時圖像
// 定義一個新的圖像
$new_img=imagecreatetruecolor($new_width,$new_height);
//var_dump($new_img);exit();
imagecopyresampled($new_img,$inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h);
imagejpeg($new_img, $dst_file,100);
ob_clean();
header("Content-type: image/jpeg");
Imagejpeg($new_img);
} // end if 1
// 2 目標圖像 的一個邊大于原圖,一個邊小于原圖 ,先放大平普圖像,然后裁剪
// =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
else{
$ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //取比例大的那個值
// 定義一個中間的大圖像,該圖像的高或寬和目標圖像相等,然后對原圖放大
$inter_w=(int)($w * $ratio);
$inter_h=(int)($h * $ratio);
$inter_img=imagecreatetruecolor($inter_w , $inter_h);
//將原圖縮放比例后裁剪
imagecopyresampled($inter_img,$src_img,0,0,0,0,$inter_w,$inter_h,$w,$h);
// 定義一個新的圖像
$new_img=imagecreatetruecolor($new_width,$new_height);
imagecopy($new_img, $inter_img, 0,0,0,0,$new_width,$new_height);
imagejpeg($new_img, $dst_file,100);
ob_clean();
header("Content-type: image/jpeg");
Imagejpeg($new_img);
}
}
/**
* 圖片處理函數
* @param unknown_type $img 傳入的原圖像流(MONGO二進制)
* @param unknown_type $dst_file 轉換后的圖像存儲路徑
* @param unknown_type $toW 生成的寬度
* @param unknown_type $toH 生成的高度
*/
public function ImageChange($img, $dst_file, $toW='', $toH=''){
$im = imagecreatefromstring($img);
$srcW=ImageSX($im);
$srcH=ImageSY($im);
if(!$toW){
$toW = $srcW;
}
if(!$toH){
$toH = $srcH;
}
$toWH=$toW/$toH; //生成寬高比
$srcWH=$srcW/$srcH; //原圖寬高比
if($toWH <= $srcWH){
$ftoW=$toW;
$ftoH=$ftoW*($srcH/$srcW);
}else{
$ftoH=$toH;
$ftoW=$ftoH*($srcW/$srcH);
}
if(function_exists("imagecreatetruecolor")){
@$ni = ImageCreateTrueColor($ftoW,$ftoH);
imagealphablending($ni,false);
imagesavealpha($ni,true);
if($ni){
ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}else{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
}else{
$ni=ImageCreate($ftoW,$ftoH);
ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
}
imagejpeg($ni, $dst_file,100);
ob_clean();
header("Content-type: image/jpeg");
imagejpeg($ni);
}
}
?></pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!