2019独角兽企业重金招聘Python工程师标准>>>
$srcWith){
$maxWidth=$maxWidth;
if($srcHeight>$maxHeight){
$maxHeight=($srcHeight/$maxHeight)*$maxWidth;
}else{
$maxHeight=$srcHeight;
}
return array('width'=>$maxWidth,'height'=>$maxHeight);
}
if($maxHeight>$srcHeight){
$maxHeight=$maxHeight;
if($srcWith>$maxWidth){
$maxWidth=($srcWith/$maxWidth)*$maxHeight;
}else{
$maxWidth=$srcWith;
}
return array('width'=>$maxWidth,'height'=>$maxHeight);
}
return array('width'=>$maxWidth,'height'=>$maxHeight);
}
/**
* [makeThumb 压缩图片函数]
* @param [type] $srcFile [原始图片路径]
* @param [type] $dstFile [目标图片路径]
* @param [type] $maxWidth [压缩后的宽度]
* @param [type] $maxHeight [压缩后的高度]
* @return [type] [description]
*/
public function makeThumb($srcFile,$dstFile,$maxWidth,$maxHeight){
//原始图片尺寸和类型
if($size=getimagesize($srcFile)){
$srcWith=$size[0];
$srcHeight=$size[1];
$mime=$size['mime'];
switch ($mime) {
case 'image/jpeg':
$isJpeg=true;
break;
case 'image/gif':
$isGif=true;
break;
case 'image/png':
$isPng=true;
break;
default:
return false;
break;
}
$arr=$this->getNewSize($maxWidth,$maxHeight,$srcWith,$srcHeight);
$thumbWidth=$arr['width'];
$thumbHeight=$arr['height'];
if(isset($isJpeg)&&$isJpeg){
$dstThumPic=imagecreatetruecolor($thumbWidth,$thumbHeight);
$srcPic=imagecreatefromjpeg($srcFile);
imagecopyresampled($dstThumPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcHeight, $srcHeight);
imagejpeg($dstThumPic,$dstFile,100);
imagedestroy($dstThumPic);
imagedestroy($srcPic);
return true;
}elseif(isset($isGif)&&$isGif){
$dstThumPic=imagecreatetruecolor($thumbWidth,$thumbHeight);
$srcPic=imagecreatefromgif($srcFile);
//创建透明画布
imagealphablending($dstThumPic, true);
imagesavealpha($dstThumPic, true);
$trans_color=imagecolorallocatealpha($dstThumPic, 0, 0, 0, 127);
imagefill($dstThumPic, 0, 0, $trans_color);
imagecopyresampled($dstThumPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcHeight, $srcHeight);
imagegif($dstThumPic,$dstFile);
imagedestroy($dstThumPic);
imagedestroy($srcPic);
return true;
}elseif(isset($isPng)&&$isPng){
$dstThumPic=imagecreatetruecolor($thumbWidth,$thumbHeight);
$srcPic=imagecreatefrompng($srcFile);
//创建透明画布
imagealphablending($dstThumPic, true);
imagesavealpha($dstThumPic, true);
$trans_color=imagecolorallocatealpha($dstThumPic, 0, 0, 0, 127);
imagefill($dstThumPic, 0, 0, $trans_color);
imagecopyresampled($dstThumPic, $srcPic, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcHeight, $srcHeight);
imagepng($dstThumPic,$dstFile);
imagedestroy($dstThumPic);
imagedestroy($srcPic);
return true;
}else{
return false;
}
}else{
return false;
}
}
}
$obj=new RarImg();
$obj->makeThumb('2.png','4.png',100,100);
?>