-
-
function CreateImage ( $ SrcImageUrl , $ DirImageUrl , $ Width , $ SrcImageUrl , $ DirImageUrl , $ Width , $ Height ) srcw >// 公鑰 $img = imagecreatefromjpeg($SrcImageUrl);
- if($type == "gif")
- $img = imagecreatefromgif($SrcImageUrl);
- if($type == "png")
- $img = imagecreatefrompng($SrcImageUrl); >$new_width = $Width;
- $new_height=$srch*($Width/$srcw); 🎜>if ($srch > $Height)
- {
- $new_height = $range
- ;
- ; $new_width = $srcw*($Height/$srch);
-
$new_image = imagecreatetruecolor($new_width, $new_height); $new_width , $new_height , $srcw , $srch );
3、改進後程式碼:
-
-
/*
*$o_photo 原圖路徑 p>
*$d_photo 處理後圖片路徑
*$width 定義寬
*$height 定義高
*呼叫方法cutphoto("test.jpg","temp.jpg",256,146);
*/
function cutphoto( $o_photo,$d_photo,$width,$height){
$temp_img = imagecreatefromjpeg($o_photo);
$o_width = imagesx($temp_img) ; //取得原圖寬
$o_height = imagesy($temp_img); //取得原圖高
//判斷處理方法
if($width>$o_width || $height>$o_height){//原圖寬或高比規定的尺寸小,進行壓縮
$newwidth =$o_width;
$newheight=$o_height;
if($o_width>$width){
$ newwidth=$width;
$newheight=$o_height*$width/$o_width;
}
if( $newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}
//縮寫圖
$new_img = imagecreatetruecolor($newwidth, $newheight);
-
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo) ;
imagedestroy($new_img);
}else{//原圖寬與高都比規定尺寸大,進行壓縮後裁切
if($o_height*$width/$o_width>$height){//先確定width與規定相同,如果height比規定大,則ok
$ newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
- $y=($newheight-$height)/2;
}else{ //否則決定height與規定相同,width自適應
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2 ;
$y=0;
}
//縮圖
- $new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $new o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
$temp_img = imagecreatefromjpeg($d_photo);
$o_width = imagesx($temp_img); //取得縮圖寬
$o_height = imagesy($temp_img) ; //取得縮圖高
//裁切圖片
$new_imgx = imagecreatetruecolor($width,$height);
-
imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
imagejpeg($new_imgx , $ d_photo);
imagedestroy($new_imgx);
- }
- }
- ?>
-
複製程式碼
|