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效率,提高php性能的一些方法
Mar 24 PHP
PHP得到mssql的存储过程的输出参数功能实现
Nov 23 PHP
PHP 数组和字符串互相转换实现方法
Mar 26 PHP
可以保证单词完整性的PHP英文字符串截取代码分享
Jul 15 PHP
PHP中echo,print_r与var_dump区别分析
Sep 29 PHP
PHP实用函数分享之去除多余的0
Feb 06 PHP
PHP curl模拟登录带验证码的网站
Nov 30 PHP
PHP7.1新功能之Nullable Type用法分析
Sep 26 PHP
使用php完成常见的文件上传功能(推荐)
Jan 13 PHP
PHP实现图片批量打包下载功能
Mar 01 PHP
PHP实现的数据对象映射模式详解
Mar 20 PHP
基于ThinkPHP删除目录及目录文件函数
Oct 28 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
德劲1107的电路分析与打磨
2021/03/02 无线电
PHP的FTP学习(三)
2006/10/09 PHP
PHP小技巧搜集,每个PHPer都来露一手
2007/01/02 PHP
PHP,ASP.JAVA,JAVA代码格式化工具整理
2010/06/15 PHP
php数据库配置文件一般做法分享
2012/07/07 PHP
PHP定时任务获取微信access_token的方法
2016/10/10 PHP
jQuery get和post 方法传值注意事项
2009/11/03 Javascript
prettify 代码高亮着色器google出品
2010/12/28 Javascript
JavaScript中变量提升 Hoisting
2012/07/03 Javascript
在ASP.NET中使用JavaScript脚本的方法
2013/11/12 Javascript
复制网页内容,粘贴之后自动加上网址的实现方法(脚本之家特别整理)
2014/10/16 Javascript
JQuery查找DOM节点的方法
2015/06/11 Javascript
javascript动态添加删除tabs标签的方法
2015/07/06 Javascript
直接拿来用的15个jQuery代码片段
2015/09/23 Javascript
基于jquery实现简单的手风琴特效
2015/11/24 Javascript
JavaScript常用代码书写规范的超全面总结
2016/09/11 Javascript
JavaScript常用正则验证函数实例小结【年龄,数字,Email,手机,URL,日期等】
2017/01/23 Javascript
JavaScript表单即时验证 验证不成功不能提交
2017/08/31 Javascript
vue使用 better-scroll的参数和方法详解
2018/01/25 Javascript
vue中动态绑定表单元素的属性方法
2018/02/23 Javascript
React 组件间的通信示例
2018/06/14 Javascript
JS二级菜单不同实现方法分析【4种方法】
2018/12/21 Javascript
angular4+百分比进度显示插件用法示例
2019/05/05 Javascript
微信小程序实现搜索指定景点周边美食、酒店
2019/05/18 Javascript
本地文件上传到七牛云服务器示例(七牛云存储)
2014/01/11 Python
python实现公司年会抽奖程序
2019/01/22 Python
canvas绘制文本内容自动换行的实现代码
2019/01/14 HTML / CSS
香港草莓网:Strawberrynet香港
2019/05/10 全球购物
JD Sports西班牙:英国领先的运动服装公司
2020/01/06 全球购物
公司员工的自我评价范例
2013/11/01 职场文书
咖啡店自主创业商业计划书
2014/01/22 职场文书
新浪微博实习心得体会
2014/01/27 职场文书
开学典礼决心书
2014/03/11 职场文书
2015年社区工会工作总结
2015/05/26 职场文书
酒吧七夕情人节宣传语
2015/11/24 职场文书
Python实现为PDF去除水印的示例代码
2022/04/03 Python