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 相关文章推荐
phplock(php进程锁) v1.0 beta1
Nov 24 PHP
PHP5 字符串处理函数大全
Mar 23 PHP
php中将网址转换为超链接的函数
Sep 02 PHP
php判断类是否存在函数class_exists用法分析
Nov 14 PHP
Sublime里直接运行PHP配置方法
Nov 28 PHP
php实现高效获取图片尺寸的方法
Dec 12 PHP
php实现的单一入口应用程序实例分析
Sep 23 PHP
PHP的Yii框架中行为的定义与绑定方法讲解
Mar 18 PHP
thinkPHP内置字符串截取函数用法详解
Nov 15 PHP
ThinkPHP中create()方法自动验证表单信息
Apr 28 PHP
PHP在同一域名下两个不同的项目做独立登录机制详解
Sep 22 PHP
Laravel框架实现修改登录和注册接口数据返回格式的方法
Aug 17 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
PHP的fsockopen、pfsockopen函数被主机商禁用的解决办法
2014/07/08 PHP
通过js为元素添加多项样式,浏览器全兼容写法
2014/08/30 Javascript
js中数组排序sort方法的原理分析
2014/11/20 Javascript
jQuery延迟加载图片插件Lazy Load使用指南
2015/03/25 Javascript
js+html5绘制图片到canvas的方法
2015/06/05 Javascript
jQuery实现响应鼠标背景变化的动态菜单效果代码
2015/08/27 Javascript
快速解决js开发下拉框中blur与click冲突
2016/10/10 Javascript
Node.js中常规的文件操作总结
2016/10/13 Javascript
Angular使用ng-messages与PHP进行表单数据验证
2016/12/28 Javascript
微信小程序学习(4)-系统配置app.json详解
2017/01/12 Javascript
js图片轮播插件的封装
2017/07/21 Javascript
详解webpack模块化管理和打包工具
2018/04/21 Javascript
jquery操作select常见方法大全【7种情况】
2019/05/28 jQuery
JavaScript 变量,数据类型基础实例详解【变量、字符串、数组、对象等】
2020/01/04 Javascript
Python编程中运用闭包时所需要注意的一些地方
2015/05/02 Python
Python编程中time模块的一些关键用法解析
2016/01/19 Python
10招!看骨灰级Pythoner玩转Python的方法
2019/04/15 Python
深入浅析Python 中 is 语法带来的误解
2019/05/07 Python
使用pyinstaller打包PyQt4程序遇到的问题及解决方法
2019/06/24 Python
Python pandas用法最全整理
2019/08/04 Python
Python实现的微信红包提醒功能示例
2019/08/22 Python
wxpython多线程防假死与线程间传递消息实例详解
2019/12/13 Python
深入了解python列表(LIST)
2020/06/08 Python
python接口自动化框架实战
2020/12/23 Python
Html5新标签datalist实现输入框与后台数据库数据的动态匹配
2017/05/18 HTML / CSS
iHerb俄罗斯:维生素、补品和天然产品
2020/07/09 全球购物
会计自我鉴定范文
2013/10/06 职场文书
自荐信格式技巧有哪些呢
2013/11/19 职场文书
经理助理岗位职责
2014/03/05 职场文书
建筑工程造价专业自荐信
2014/07/08 职场文书
新颖的化妆品活动方案
2014/08/21 职场文书
暑期政治学习心得体会
2014/09/02 职场文书
生日宴会祝酒词
2015/08/10 职场文书
2019入党申请书格式
2019/06/25 职场文书
使用python+pygame开发消消乐游戏附完整源码
2021/06/10 Python
Java实现斗地主之洗牌发牌
2021/06/14 Java/Android