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

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

function my_image_resize($src_file, $dst_file, $dst_width=32, $dst_height=32) {
if($dst_width <1 || $dst_height <1) {
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<=$dst_width && $src_h<=$dst_height) {
$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 <= $ratio_h) {
$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;
}
}
}

相关文章

php5与php7的区别点总结

php5与php7的区别是什么?下面本篇文章就来给大家对比一下php5与php7,介绍php5与php7之间的区别。有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 php...

thinkPHP的Html模板标签使用方法

注意:在使用如<html:select />等标签之前,必须要引入TP的标签库:<tagLib name="html" /> 如果我们现在需要一个select下拉...

PHP使用反射机制实现查找类和方法的所在位置

本文实例讲述了PHP使用反射机制实现查找类和方法的所在位置。分享给大家供大家参考,具体如下: //参数1是类名,参数2是方法名 $func = new ReflectionMetho...

PHP命名空间namespace定义及导入use用法详解

本文实例讲述了PHP命名空间namespace定义及导入use用法。分享给大家供大家参考,具体如下: 在PHP中,出现同名函数或是同名类是不被允许的。为防止编程人员在项目中定义的类名或函...

对Session和Cookie的区分与解释

对Session和Cookie的区分与理解  先说session 对SESSION的争论好象一直没有停止过,不过幺麽能理解SESSION的人应该占90以上。但还是讲讲,别嫌老~...