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 相关文章推荐
将数字格式的计算结果转为汉字格式
Oct 09 PHP
php获得当前的脚本网址
Dec 10 PHP
PHP Google的translate API代码
Dec 10 PHP
PHP源码之 ext/mysql扩展部分
Jul 17 PHP
用来解析.htpasswd文件的PHP类
Sep 05 PHP
PHP使用curl模拟post上传及接收文件的方法
Mar 04 PHP
php遍历替换目录下文件指定内容的方法
Nov 10 PHP
PHP编程计算文件或数组中单词出现频率的方法
May 22 PHP
[原创]php正则删除html代码中class样式属性的方法
May 24 PHP
Laravel中的Auth模块详解
Aug 17 PHP
PHP生成随机字符串实例代码(字母+数字)
Sep 11 PHP
PHP基于openssl实现非对称加密代码实例
Jun 19 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 Ajax乱码
2008/04/09 PHP
无需重新编译php加入ftp扩展的解决方法
2013/02/07 PHP
使用php发送有附件的电子邮件-(PHPMailer使用的实例分析)
2013/04/26 PHP
PHP实现获取FLV文件的时间
2015/02/10 PHP
laravel框架模型、视图与控制器简单操作示例
2019/10/10 PHP
laravel数据库查询结果自动转数组修改实例
2021/02/27 PHP
Ajax::prototype 源码解读
2007/01/22 Javascript
用prototype实现的简单小巧的多级联动菜单
2007/03/24 Javascript
javascript实现iframe框架延时加载的方法
2014/10/30 Javascript
javascript实现简单的进度条
2015/07/02 Javascript
Javascript动画效果(1)
2016/10/11 Javascript
JS简单判断滚动条的滚动方向实现方法
2017/04/28 Javascript
angularjs2中父子组件的数据传递的实例代码
2017/07/05 Javascript
前端开发不得不知的10个最佳ES6特性
2017/08/30 Javascript
jQuery实现点击DIV同时点击CheckBox,并为DIV上背景色的实例
2017/12/18 jQuery
vue中引入第三方字体文件的方法示例
2018/12/17 Javascript
使用nodejs分离html文件里的js和css详解
2019/04/12 NodeJs
Flutter实现仿微信底部菜单栏功能
2019/09/18 Javascript
vue使用@scroll监听滚动事件时,@scroll无效问题的解决方法详解
2019/10/15 Javascript
react quill中图片上传由默认转成base64改成上传到服务器的方法
2019/10/30 Javascript
JavaScript实现alert弹框效果
2020/11/19 Javascript
[55:45]LGD vs OG 2019国际邀请赛淘汰赛 胜者组 BO3 第三场 8.24
2019/09/10 DOTA
python3使用tkinter实现ui界面简单实例
2014/01/10 Python
用Python实现一个简单的能够上传下载的HTTP服务器
2015/05/05 Python
python中快速进行多个字符替换的方法小结
2016/12/15 Python
Tensorflow实现神经网络拟合线性回归
2019/07/19 Python
MoviePy简介及Python视频剪辑自动化
2020/12/18 Python
CSS3属性box-sizing使用指南
2014/12/09 HTML / CSS
高中打架检讨书
2014/02/13 职场文书
小学生竞选班干部演讲稿
2014/04/24 职场文书
商场消防安全责任书
2014/07/29 职场文书
2014年乡镇个人工作总结
2014/12/03 职场文书
工作自我评价范文
2015/03/05 职场文书
2016年三八红旗手先进事迹材料
2016/02/26 职场文书
导游词之介休绵山
2019/12/31 职场文书
PyTorch 如何自动计算梯度
2021/05/23 Python