>  기사  >  백엔드 개발  >  acronis true image Php Image Resize图片大小调整的函数代码

acronis true image Php Image Resize图片大小调整的函数代码

WBOY
WBOY원래의
2016-07-29 08:44:131216검색

复制代码 代码如下:


function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
if($dst_width echo "params width or height error !";
exit();
}
if(!file_exists($src_file)) {
echo $src_file . " is not exists !";
exit();
}
$type=exif_imagetype($src_file);
$support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
if(!in_array($type, $support_type,true)) {
echo "this type of image does not support! only support jpg , gif or png";
exit();
}
switch($type) {
case IMAGETYPE_JPEG :
$src_img=imagecreatefromjpeg($src_file);
break;
case IMAGETYPE_PNG :
$src_img=imagecreatefrompng($src_file);
break;
case IMAGETYPE_GIF :
$src_img=imagecreatefromgif($src_file);
break;
default:
echo "Load image error!";
exit();
}
$src_w=imagesx($src_img);
$src_h=imagesy($src_img);
$ratio_w=1.0 * $dst_width/$src_w;
$ratio_h=1.0 * $dst_height/$src_h;
if ($src_w$x = ($dst_width-$src_w)/2;
$y = ($dst_height-$src_h)/2;
$new_img=imagecreatetruecolor($dst_width,$dst_height);
imagecopy($new_img,$src_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
} else {
$dstwh = $dst_width/$dst_height;
$srcwh = $src_w/$src_h;
if ($ratio_w $zoom_w = $dst_width;
$zoom_h = $zoom_w*($src_h/$src_w);
} else {
$zoom_h = $dst_height;
$zoom_w = $zoom_h*($src_w/$src_h);
}
$zoom_img=imagecreatetruecolor($zoom_w, $zoom_h);
imagecopyresampled($zoom_img,$src_img,0,0,0,0,$zoom_w,$zoom_h,$src_w,$src_h);
$new_img=imagecreatetruecolor($dst_width,$dst_height);
$x = ($dst_width-$zoom_w)/2;
$y = ($dst_height-$zoom_h)/2+1;
imagecopy($new_img,$zoom_img,$x,$y,0,0,$dst_width,$dst_height);
switch($type) {
case IMAGETYPE_JPEG :
imagejpeg($new_img,$dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($new_img,$dst_file);
break;
case IMAGETYPE_GIF :
imagegif($new_img,$dst_file);
break;
default:
break;
}
}
}

以上就介绍了acronis true image Php Image Resize图片大小调整的函数代码,包括了acronis true image方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.