用php和imagemagick来处理图片文件的上传和缩放处理
啥也不说,直接上代码,大家可以自行添加增加水印功能:
- <?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;
- }
摘自:http://www.icode100.com/posts/view/64
本文非原创,感谢作者提供学习
用php和imagemagick来处理图片文件的上传和缩放处理的更多相关文章
- web操作文件的上传到服务器 并可下载 并且读取出来
1.文件的上传-servlet实现文件上传---核心API—DiskFileItemFactory 一.文件上传概述 l 实现web开发中的文件上传功能,需完成如下二步操作: • 在web页面 ...
- 带进度条的文件批量上传插件uploadify
有时项目中需要一个文件批量上传功能时,个人认为uploadify是快速简便的解决方案. 先上效果图: 一. 下载uploadify 从官网下载uploadify的Flash版本(Flash版本免费,另 ...
- C# 用原生JS进行文件的上传
1.此文章是用原生JS来进行文件的上传,有两个版本,一个不用ajax,一个用ajax. 1)非AJAX <!DOCTYPE html> <html> <head> ...
- ssh整合问题总结--在添加商品模块实现图片(文件)的上传
今天在做毕设(基于SSH的网上商城项目)中碰到了一个文件上传的需求,就是在后台管理员的商品模块中,有一个添加商品,需要将磁盘上的图片上传到tomcat保存图片的指定目录中: 完成这个功能需要两个步,第 ...
- 文件的上传(如何兼容火狐与IE)与国际化的原理
1.文件的上传 [1] 简介 > 将本地的文件上传到服务器中 > 用户需要通过一个表单将文件上传到服务器中 [2] 表单的设置 ...
- java实现ftp文件的上传与下载
最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...
- 在SpringMVC框架下实现文件的 上传和 下载
在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...
- .Net多文件同时上传(Jquery Uploadify)
前提:领导给了我一个文件夹,里面有4000千多张产品图片,每张图片已产品编号+产品名称命名,要求是让我把4000多张产品图片上传到服务器端,而且要以产品编码创建n个文件夹,每张图片放到对应的文件夹下. ...
- mac下svn问题——“.a”(静态库)文件无法上传解决
mac下svn问题——“.a”(静态库)文件无法上传解决 “.a”(静态库)文件无法上传(svn工具:Versions) 网上查询了一下,说是Xcode自带的svn和Versi ...
随机推荐
- tabBar 选中默认蓝色 ,取消选中(自定义)
- (void)viewDidLoad { [super viewDidLoad]; // [self _initSubViewControllers]; // [self _custom ...
- hibernate异常:org.hibernate.NonUniqueObjectException
异常:org.hibernate.NonUniqueObjectException 提示:a different object with the same identifier value was a ...
- LeetCode 之 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【转】MapReduce的优化
相信每个程序员在编程时都会问自己两个问题“我如何完成这个任务”,以及“怎么能让程序运行得更快”.同样,MapReduce计算模型的多次优化也是为了更好地解答这两个问题. MapReduce计算模型的优 ...
- 判断是否点击键盘enter键
//enter键监测触发事件 function checkKeyCode() { document.onkeydown = function(e) { if(!e) e = window.event; ...
- geekNews 学习总结
1:下移隐藏,上移出现 //android.support.v4.widget.NestedScrollView nsvScroller; //FrameLayout llDetailBottom; ...
- 版本控制器:SVN
版本控制器:SVN 开发中的实际问题 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流--需求之一:备份! 这个项目中需要一个很复杂的功 ...
- HDU 5860 Death Sequence
用线段树可以算出序列.然后o(1)询问. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...
- 4、js内置函数
前言:上一篇我介绍了函数的基本概念,和一些简单的Demo.其实很多函数是js内置的,我们无需自己去写,直接拿过来用即可.内置函数分为全局函数和js内置对象的函数区别:全局函数不属于任何一个内置对象.理 ...
- 为什么PHP(CLI)同一个错误信息会打印两次?
第一个信息是display_errors输出的,在fpm环境下输出到浏览器那里,而在CLI环境下会打印到屏幕上. display_errors = On 第二个信息是log_errors输出的. lo ...