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

yipeiwu_com5年前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;
}
}
}

相关文章

php 删除目录下N分钟前创建的所有文件的实现代码

复制代码 代码如下:<?php//delfile("upload",10);function delfile($dir,$n) //删除当DIR路径下N分钟前创建的所有文件;{if...

php编程实现追加内容到txt文件中的方法

本文实例讲述了php编程实现追加内容到txt文件中的方法。分享给大家供大家参考,具体如下: 覆盖txt文件内容: $filename="aa/a.txt"; $handle=fope...

php和html的区别点详细总结

今天我来和大家讨论下关于PHP技术的另一个简单小问题,就是PHP代码和HTML代码的区别在哪里。 其实,如果简单的说,大家都知道,HTML是典型的静态网络编程用语,而PHP则是一种可实现...

PHP 7.1中AES加解密方法mcrypt_module_open()的替换方案

前言 mcrypt 扩展已经过时了大约10年,并且用起来很复杂。因此它被废弃并且被 OpenSSL 所取代。 从PHP 7.2起它将被从核心代码中移除并且移到PECL中。 PHP手册在7...

PHP根据图片色界在不同位置加水印的方法

本文实例讲述了PHP根据图片色界在不同位置加水印的方法。分享给大家供大家参考。具体实现方法如下: function add_wm($nmw_water, $src_file, $ou...