需要使用gd库的方法 php需要引入gd扩展支持
public function Img($Image,$exif=0,$Dw=450,$Dh=450,$Type=1){
if(!file_exists($Image)){
return false;
}
IF($Type!=1){
copy($Image,str_replace(".","_x.",$Image));
$Image=str_replace(".","_x.",$Image);
}
$ImgInfo=getimagesize($Image);
switch($ImgInfo[2]){
case 1:
$Img = @imagecreatefromgif($Image);
Break;
case 2:
$Img = @imagecreatefromjpeg($Image);
Break;
case 3:
$Img = @imagecreatefrompng($Image);
Break;
}
if(empty($Img)){
if($Type!=1){
unlink($Image);
}
return false;
}
$w=imagesx($Img);
$h=imagesy($Img);
$width = $w;
$height = $h;
if($Type==1){
if($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
if($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
}elseif($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}else{
$width=$width;
$height=$height;
}
if($ImgInfo[2] == 3)
{
imagesavealpha($Img,true);
}
$nImg = imagecreatetruecolor($width,$height);
if($ImgInfo[2] == 3)
{
imagealphablending($nImg,false);
imagesavealpha($nImg,true);
}
imagecopyresampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);
}else{
$nImg = imagecreatetruecolor($Dw,$Dh);
if($h/$w>$Dh/$Dw){
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
imagecopyresampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
}else{
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
imagecopyresampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
}
}
if(!empty($exif))
{
switch ($exif) {
case 8:
$image = imagerotate($nImg, 90, 0);
break;
case 3:
$image = imagerotate($nImg, 180, 0);
break;
case 6:
$image = imagerotate($nImg, -90, 0);
break;
}
}
switch($ImgInfo[2]){
case 1:
imagegif($nImg,$Image);
Break;
case 2:
imagejpeg($nImg,$Image);
Break;
case 3:
imagepng($nImg,$Image);
Break;
}
$fileDirArr = explode('/',$Image);
return end($fileDirArr);
}