PHP上传图片进行等比缩放可增加水印功能


Posted in PHP onJanuary 13, 2014

啥也不说,直接上代码,大家可以自行添加增加水印功能:

<?php 
/** 
* 
* @author zhao jinhan 
* @date 2014年1月13日11:54:30 
* @email xb_zjh@126.com 
* 
*/ 
header('Content-type:text/html; charset=utf-8'); 
//定义缩略图的宽高 
define('THUMB_WIDTH',300); 
define('THUMB_HEIGHT',300); /** 
* 重新生成上传的文件名 
* @return string 
* @author zhao jinhan 
* 
*/ 
function _file_type($filetype = null){ 
switch($filetype) 
{ 
case "image/jpeg": 
$fileextname = "jpg"; 
break; 
case "image/gif": 
$fileextname = "gif"; 
break; 
case "image/png": 
$fileextname = "png"; 
break; 
default: 
$fileextname = false; 
break; 
} 
return $fileextname?date('YmdHis',time()).'.'.$fileextname:false; 
} 
/** 
* 
* @param string $filename 
* @param string $width 
* @param string $height 
* @param string $quality 
* @param string $savepath 
* @return boolean 
*/ 
function _make_thumb($filename='', $width=THUMB_WIDTH, $height=THUMB_HEIGHT, $savepath='./upload'){ 
if(file_exists($filename)){ 
//上传图片的尺寸 
$imagesize=getimagesize($filename); 
$imagewidth=$imagesize[0]; 
$imageheight=$imagesize[1]; 
$mime = $imagesize['mime']; 
//宽高比例 
$ratio = $imagewidth/$imageheight; 
//新建一个背景图片 
$bgimg = imagecreatetruecolor($width, $height); 
$white = imagecolorallocate($bgimg, 255, 255, 255); 
//填充背景色为白色 
imagefill($bgimg,0,0,$white); 
if($mime == 'image/gif'){ 
$im = @imagecreatefromgif($filename); /* Attempt to open */ 
$outfun = 'imagegif'; 
}elseif($mime == 'image/png'){ 
$im = @imagecreatefrompng($filename); /* Attempt to open */ 
$outfun = 'imagepng'; 
}else{ 
$im = @imagecreatefromjpeg($filename); /* Attempt to open */ 
$outfun = 'imagejpeg'; 
} 
if($ratio > 1){ 
//宽度较大 
if($imagewidth > $width){ 
//缩放图片到背景图片上 
$new_width = $width; 
$new_height = ($width*$imageheight)/$imagewidth; 
$bg_y = ceil(abs(($height-$new_height)/2)); 
imagecopyresampled($bgimg, $im, 0, $bg_y, 0, 0, $new_width, $new_height, $imagewidth, $imageheight); 
}else{ 
//复制图片到背景图片上 
$copy = true; 
} 
}else{ 
//高度较大 
if($imageheight > $height){ 
//缩放图片 
$new_height = $height; 
$new_width = ($height*$imagewidth)/$imageheight; 
$bg_x = ceil(($width-$new_width)/2); 
imagecopyresampled($bgimg, $im, $bg_x, 0, 0, 0, $new_width, $new_height, $imagewidth, $imageheight); 
}else{ 
//复制图片到背景图片上 
$copy = true; 
} 
} 
if($copy){ 
//复制图片到背景图片上 
$bg_x = ceil(($width-$imagewidth)/2); 
$bg_y = ceil(($height-$imageheight)/2); 
imagecopy($bgimg, $im, $bg_x, $bg_y, 0, 0, $imagewidth, $imageheight); 
} 
$ext = _file_type($mime); 
$outfun($bgimg, $savepath.'/'.$ext); 
imagedestroy($bgimg); 
return $savepath.'/'.$ext; 
}else{ 
return false; 
} 
} 
if($_POST){ 
$size = $_POST['size']?strtoupper(trim($_POST['size'])):'2M'; 
$imgsize = $_FILES['img']['size']?$_FILES['img']['size']/(1024*1024):0; 
$imgwidth = $imgheight = $_POST['width-height']?intval($_POST['width-height']):300; 
//自定定义文件上传大小 
ini_set('upload_max_filesize',$size); 
$mathsize = str_replace('M','',$size); 
if($imgsize>$mathsize){ 
echo "图片大小不得超过{$size}!"; 
return; 
} 
if($file_name = _file_type($_FILES['img']['type'])){ 
if($_FILES['img']['error'] == UPLOAD_ERR_OK){ 
$savepath = 'upload/'; 
if(!is_dir($savepath)){ 
mkdir($savepath,0644); 
} 
//生成缩略图 
$thumb_file = _make_thumb($_FILES['img']['tmp_name'], $imgwidth, $imgheight, $savepath); 
//move_uploaded_file($_FILES['img']['tmp_name'],$savepath.$file_name); 
echo "生成后的图片为:<img src='".$thumb_file."' />"; 
}else{ 
echo $_FILES['img']['error']; 
return; 
} 
}else{ 
echo "图片格式不正确,请上传jpg,gif,png的格式!"; 
return; 
} 

}else{ 
echo <<<EOT 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>缩放图片保存成正方形</title> 
</head> 
<body> 
<form action="" method="POST" enctype="multipart/form-data"> 
<div> 
<label>上传一张图片:</label> 
<input type="file" name="img" /> 
</div> 
<div> 
<label>生成缩略图的宽高(单位px):</label> 
<input type="text" name="width-height" value="300" /> 
</div> 
<div> 
<label>文件大小上限:</label> 
<input type="text" name="size" value="2M" /> 
</div> 
<div><input type="submit" name="submit" value="提交" /></div> 
</form> 
</body> 
</html> 
EOT; 
}
PHP 相关文章推荐
PHP 和 MySQL 基础教程(三)
Oct 09 PHP
php中0,null,empty,空,false,字符串关系的详细介绍
Jun 20 PHP
微信扫描二维码登录网站代码示例
Dec 30 PHP
php实现递归的三种基本方式
Jul 04 PHP
全新Mac配置PHP开发环境教程
Feb 03 PHP
基于laravel制作APP接口(API)
Mar 15 PHP
Yii的Srbac插件用法详解
Jul 14 PHP
thinkPHP5 tablib标签库自定义方法详解
May 10 PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
Apr 21 PHP
PHP实现二维数组中的查找算法小结
Jun 09 PHP
php常用日期时间函数实例小结
Jul 04 PHP
php7 新增功能实例总结
May 25 PHP
php网站地图生成类示例
Jan 13 #PHP
php防止sql注入示例分析和几种常见攻击正则表达式
Jan 12 #PHP
php中文验证码实现示例分享
Jan 12 #PHP
PHP 下载文件时自动添加bom头的方法实例
Jan 10 #PHP
php环境下利用session防止页面重复刷新的具体实现
Jan 09 #PHP
浅析php数据类型转换
Jan 09 #PHP
js和php邮箱地址验证的实现方法
Jan 09 #PHP
You might like
老机欣赏|中国60年代精品收音机
2021/03/02 无线电
PHP中的超全局变量
2006/10/09 PHP
几个高效,简洁的字符处理函数
2007/04/12 Javascript
javascript天然的迭代器
2010/10/29 Javascript
5分钟理解JavaScript中this用法分享
2013/11/09 Javascript
JavaScript中具名函数的多种调用方式总结
2014/11/08 Javascript
属于你的jQuery提示框(Tip)插件
2016/01/20 Javascript
限制复选框最多选择项的实现代码
2016/05/30 Javascript
使用jquery获取url及url参数的简单实例
2016/06/14 Javascript
学习Angularjs分页指令
2016/07/01 Javascript
JS中Select下拉列表类(支持输入模糊查询)功能
2017/01/17 Javascript
JS查找英文文章中出现频率最高的单词
2017/03/20 Javascript
详解react使用react-bootstrap当轮子造车
2017/08/15 Javascript
jquery实现限制textarea输入字数的方法
2017/09/06 jQuery
JS实现的简单tab切换功能完整示例
2019/06/20 Javascript
python 统计列表中不同元素的数量方法
2018/06/29 Python
浅谈django orm 优化
2018/08/18 Python
详解Python 函数如何重载?
2019/04/23 Python
pytorch 固定部分参数训练的方法
2019/08/17 Python
python三引号如何输入
2020/07/06 Python
selenium框架中driver.close()和driver.quit()关闭浏览器
2020/12/08 Python
PyChon中关于Jekins的详细安装(推荐)
2020/12/28 Python
Numpy ndarray 多维数组对象的使用
2021/02/10 Python
介绍CSS3使用技巧5个
2009/04/02 HTML / CSS
日本乐天德国站:Rakuten.de
2019/05/16 全球购物
费用会计岗位职责
2014/01/01 职场文书
五年级英语教学反思
2014/01/31 职场文书
综合实践活动方案
2014/02/14 职场文书
在校大学生的职业生涯规划书
2014/03/14 职场文书
课程改革实施方案
2014/03/16 职场文书
保险公司早会主持词
2014/03/22 职场文书
民警群众路线教育实践活动对照检查材料
2014/10/04 职场文书
被告答辩状范文
2015/05/22 职场文书
汽车车尾标语大全
2015/08/11 职场文书
使用python+pygame开发消消乐游戏附完整源码
2021/06/10 Python
防止web项目中的SQL注入
2021/12/06 MySQL