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 相关文章推荐
多重?l件?合查?(二)
Oct 09 PHP
php 清除网页病毒的方法
Dec 05 PHP
PHP中的string类型使用说明
Jul 27 PHP
PHP插入排序实现代码
Apr 04 PHP
PHP转换文件夹下所有文件编码的实现代码
Jun 06 PHP
CentOS 6.2使用yum安装LAMP以及phpMyadmin详解
Jun 17 PHP
php使用base64加密解密图片示例分享
Jan 20 PHP
CI框架验证码CAPTCHA辅助函数用法实例
Nov 05 PHP
递归实现php数组转xml的代码分享
May 14 PHP
Yii2创建控制器(createController)方法详解
Jul 23 PHP
PHP中模糊查询并关联三个select框
Jun 19 PHP
PHP isset empty函数相关面试题及解析
Dec 11 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
在Windows系统上安装PHP运行环境文字教程
2010/07/19 PHP
PHP获取搜索引擎关键字来源的函数(支持百度和谷歌等搜索引擎)
2012/10/03 PHP
php网页标题中文乱码的有效解决方法
2014/03/05 PHP
PHP处理JSON字符串key缺少双引号的解决方法
2014/09/16 PHP
解析PHP的Yii框架中cookie和session功能的相关操作
2016/03/17 PHP
PHP面向对象程序设计之类与反射API详解
2016/12/02 PHP
PHP递归统计系统中代码行数
2019/09/19 PHP
javascript实现rgb颜色转换成16进制格式
2015/07/10 Javascript
JS实现黑色风格的网页TAB选项卡效果代码
2015/10/09 Javascript
使用vue.js制作分页组件
2016/06/27 Javascript
用AngularJS的指令实现tabs切换效果
2016/08/31 Javascript
jQuery实现手机上输入后隐藏键盘功能
2017/01/04 Javascript
基于JavaScript实现的插入排序算法分析
2017/04/14 Javascript
jQuery实现的淡入淡出图片轮播效果示例
2018/08/29 jQuery
七行JSON代码把你的网站变成移动应用过程详解
2019/07/09 Javascript
vue组件库的在线主题编辑器的实现思路
2020/04/03 Javascript
用Python代码来绘制彭罗斯点阵的教程
2015/04/03 Python
Django 添加静态文件的两种实现方法(必看篇)
2017/07/14 Python
python机器学习之神经网络(三)
2017/12/20 Python
Python 字符串换行的多种方式
2018/09/06 Python
python下PyGame的下载与安装过程及遇到问题
2019/08/04 Python
美国最受欢迎的童装品牌之一:The Children’s Place
2016/07/23 全球购物
阿联酋航空丹麦官方网站:Emirates DK
2019/08/25 全球购物
新浪网技术部笔试题
2016/08/26 面试题
名词解释型面试题(主要是网络)
2013/12/27 面试题
Servlet都有哪些方法?主要作用是什么?
2014/03/04 面试题
大专学生推荐信范文
2013/11/19 职场文书
哈弗商学院毕业生求职信
2014/02/26 职场文书
希特勒的演讲稿
2014/05/23 职场文书
党的群众路线教育实践活动对照检查材料(个人)
2014/09/24 职场文书
镇班子对照检查材料思想汇报
2014/09/24 职场文书
党的群众路线教育实践活动心得体会(教师)
2014/10/31 职场文书
2015年环卫工作总结
2015/04/28 职场文书
2015年乡镇残联工作总结
2015/05/13 职场文书
刘胡兰观后感
2015/06/16 职场文书
angular4实现带搜索的下拉框
2022/03/25 Javascript