1.創(chuàng)建iamge處理類?
include_once 'ImageResize.class.php';?
date_default_timezone_set('PRC');?
??? ??? {???
??
??? protected $nameinfo;???
??? ??? $InputImageFileExtension;????
??
??? ?? ?? ?? getInstance() {???
??????? ?? $instance;???
??????? if (!isset ($instance)) {???
??????????? $class = __CLASS__;???
??????????? $instance = ??? $class ();???
??????? }???
??
??????? $instance ??;???
??? }???
??
??? function uploadresize($fileparam, $imageparam) {???
? $newW = $imageparam['imageW'];?
? $newH = $imageparam['imageH'];?
??????? $this->nameinfo = ??('.',$fileparam['name']);???
??
??????? //取得圖文格式???
??????? $this->InputImageFileExtension = $this->nameinfo[1];???
??
??????? //判斷是不是給了 ??? ?? ?????
??????? if(empty($imageparam['imagename'])){???
?????????? $outputFileName = $this->nameinfo[0];???
??????? }else{???
??????????? $outputFileName = $imageparam['imagename'];???
?????? }???
??
??????? //判斷路徑是否??確???
??????? if (!file_exists($imageparam['imagepath'])) {???
??????????? if(!mkdir ($imageparam['??? ??']) ){???
??????????????? ??? ?? ??('??? ??? ???????.');???
??????????????? ??;???
?????????? }???
??????? }???
??
?????? $file_src = $imageparam['imagepath']."/". $?????? . "_." . $this->InputImageFileExtension;???
?????? //? ??? ??? ??? ?????
?????? if(file_exists($file_src)){???
??????? unlink($file_src);???
?????? }???
??
?????? move_uploaded_file($fileparam['tmp_name'], $file_src);???
?????? // ??? ?? ???
?? $imageResize = new ImageResize();?
?? $imageResize->my_image_resize($file_src,$file_src,$newW,$newH,$this->InputImageFileExtension);??
??? }???
}???
?>??
2.設(shè)置圖文大小處理類:?
class ImageResize {?
/*?
* ??? ,圖 Image 不變shape?
* 參數(shù)說ming:輸入 需要處理圖 Pictures ural 文件name ,生成新圖 Images 保存文件name ,生成 new 圖 Images 寬 , 生成 新 圖 clips 的 high?
* ???: smallchicken?
* ?? 2008-12-18?
*/?
// 獲得任意大小圖???,不足地方拉伸,不產(chǎn)生變?, 不留下空白?
function my_image_resize($src_file, $dst_file, $new_width, $new_height , $type) {?
if ($new_width < 1 || $new_height < 1) {?
echo "params ?? ?? ?? ?? !";?
??();?
}?
if (!file_exists ( $src_file )) {?
echo $src_file . " ???? ???? !";
exit ();
}
// ??? ??
$support_type = array ("jpg", "png", "gif" );
if (! in_array ( $type, $support_type, true )) {
echo "? ??? ???? ???? ????! jpg, gif ?? png? ?????."
exit ();
}
//??? ??
switch ($type) {
case "jpg" :
$src_img = imagecreatefromjpeg ( $src_file )
break
case " png" :
$src_img = imagecreatefrompng( $src_file );
break;
case "gif" :
$src_img = imagecreatefrompng( $src_file );
break;
default :
echo "??? ?? ??!";
exit ();
$w = Imagesx ( $src_img )
$h = Imagesy ( $src_img ); ///
if($w < $new_width && $h < $new_height){
// ?? ?? ??? ?? ?? ??? ???? ?? ?? ??? ??
$inter_w = $w;
$inter_h = $h;
$inter_img = imagecreatetruecolor( $inter_w, $inter_h )
imagecopy( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h );
// ?? ?? ??? ?? ???? ???? ?? ??? ?? $ratio ratio
// ? ??? ??
$new_img = imagecreatetruecolor ( $inter_w, $inter_h )
imagecopyresampled ( $new_img, $inter_img, 0, 0, 0, 0, $inter_w, $inter_w, $inter_h, $inter_h )
???($type) {
case "jpg" :
imagejpeg( $ new_img, $dst_file, 100 ); // ??? ??
break;
case "png" :
imagepng ( $new_img, $dst_file, 100 ); " :
imagegif( $new_img, $dst_file, 100 );
break;
default :
break;
}
die();
}
////
$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 );
imagecopy( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h )// ?? ? ??? ??? ?? ?????. ?? ?? ???? ?? ??????. $ratio ratio
// ? ??? ??
$new_img = imagecreatetruecolor ( $new_width, $new_height )
imagecopyresampled ( $new_img, $inter_img, 0 , 0, 0 , 0, $new_width, $new_height, $inter_w, $inter_h )
???($type) {
case "jpg" :
imagejpeg( $new_img, $dst_file, 100 ) ; // ?? ???
break;
case "png" :
imagepng ( $new_img, $dst_file, 100 )
case "gif" :
imagegif ( $new_img, $ dst_file, 100 );
break;
default :
break
}
} else {
// 1?? ??
// 2 ?? ???? ?? ????? ?? ??? ?? ????? ????. ?? ?? ???? ??? ?? ?????.
// =if( ($ratio_w < 1 && $ratio_h > 1 ) || ($ratio_w >1 && $ratio_h < ;1) )
$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 ); $new_img, $inter_img, 0, 0, 0, 0, $new_width, $ new_height )
???($type) {
case "jpg" :
imagejpeg( $new_img, $dst_file, 100 ); // ??? ??
break;
case "png" :
imagepng ( $new_img, $dst_file, 100 )
case "gif" :
imagegif ( $new_img, $dst_file, 100 );
break;
default :
break
}
} // if3
} // ?? ??
??>?>
3. ??? ??? ?? ? ?? ??
include "../../tools/Image/Image.class.php" ;
$array = $_FILES['photo'];
//?? ?? ????
$resizeParam = array (
'imagepath' => 'd:/xampp/ www /', //??? ?? ??
'imageW' => 200, //??? ?? ??
'imageH' => 200 //??? ?? ??
)
if ( !empty ( $array)) {
Image :: getInstance()->uploadresize($array, $resizeParam)
}
?>4. ??? ??
< ;html>