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中的一个中文字符串截取函数
Feb 14 PHP
使用Xdebug调试和优化PHP程序之[1]
Apr 17 PHP
PHP 服务器配置(使用Apache及IIS两种方法)
Jun 01 PHP
php select,radio和checkbox默认选择的实现方法
May 15 PHP
php算开始时间到过期时间的相隔的天数
Jan 12 PHP
解决phpmyadmin中缺少mysqli扩展问题的方法
May 06 PHP
php数组声明、遍历、数组全局变量使用小结
Jun 05 PHP
PHP session文件独占锁引起阻塞问题解决方法
May 12 PHP
举例详解PHP脚本的测试方法
Aug 05 PHP
ThinkPHP中create()方法自动验证实例
Apr 26 PHP
使用YII2框架实现微信公众号中表单提交功能
Sep 04 PHP
php双层循环(九九乘法表)
Oct 23 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 array_filter除去数组中的空字符元素
2020/06/21 PHP
PHP 使用redis简单示例分享
2015/03/05 PHP
php工具型代码之印章抠图
2018/07/18 PHP
JQuery自适应IFrame高度(支持嵌套 兼容IE,ff,safafi,chrome)
2011/03/28 Javascript
基于jQuery的图片左右无缝滚动插件
2012/05/23 Javascript
jQuery实现图片放大预览实现原理及代码
2013/09/12 Javascript
JQuery中dataGrid设置行的高度示例代码
2014/01/03 Javascript
用自定义图片代替原生checkbox实现全选,删除以及提交的方法
2016/10/18 Javascript
jquery,js简单实现类似Angular.js双向绑定
2017/01/13 Javascript
JavaScript实现简单的双色球(实例讲解)
2017/07/31 Javascript
新手vue构建单页面应用实例代码
2017/09/18 Javascript
Vue手把手教你撸一个 beforeEnter 钩子函数
2018/04/24 Javascript
JS使用正则表达式获取小括号、中括号及花括号内容的方法示例
2018/06/01 Javascript
Vue作用域插槽slot-scope实例代码
2018/09/05 Javascript
Vue2.x Todo之自定义指令实现自动聚焦的方法
2019/01/08 Javascript
vue模式history下在iis中配置流程
2019/04/17 Javascript
[43:51]2014 DOTA2国际邀请赛中国区预选赛 Dream Times VS TongFu
2014/05/22 DOTA
用Python的pandas框架操作Excel文件中的数据教程
2015/03/31 Python
python中如何使用正则表达式的集合字符示例
2017/10/09 Python
Python中join函数简单代码示例
2018/01/09 Python
Python 类的特殊成员解析
2018/06/20 Python
关于Django ForeignKey 反向查询中filter和_set的效率对比详解
2018/12/15 Python
将pip源更换到国内镜像的详细步骤
2019/04/07 Python
Django 用户认证组件使用详解
2019/07/23 Python
python tornado使用流生成图片的例子
2019/11/18 Python
Matplotlib中%matplotlib inline如何使用
2020/07/28 Python
python openpyxl模块的使用详解
2021/02/25 Python
Crocs美国官方网站:卡骆驰洞洞鞋
2017/08/04 全球购物
信息与计算科学专业推荐信
2014/02/23 职场文书
学生安全责任书范本
2014/07/24 职场文书
协会周年庆活动方案
2014/08/26 职场文书
2015年档案室工作总结
2015/05/23 职场文书
2016入党培训心得体会范文
2016/01/08 职场文书
利用python做表格数据处理
2021/04/13 Python
nginx常用配置conf的示例代码详解
2022/03/21 Servers
使用CSS实现按钮边缘跑马灯动画
2023/05/07 HTML / CSS