啥也不说,直接上代码,大家可以自行添加增加水印功能:

  1. <?php
  2. /**
  3. *
  4. * @author zhao jinhan
  5. * @date 2014年1月13日11:54:30
  6. * @email xb_zjh@126.com
  7. *
  8. */
  9. header('Content-type:text/html; charset=utf-8');
  10. //定义缩略图的宽高
  11. define('THUMB_WIDTH',300);
  12. define('THUMB_HEIGHT',300);
  13. /**
  14. * 重新生成上传的文件名
  15. * @return string
  16. * @author zhao jinhan
  17. *
  18. */
  19. function _file_type($filetype = null){
  20. switch($filetype)
  21. {
  22. case "image/jpeg":
  23. $fileextname  = "jpg";
  24. break;
  25. case "image/gif":
  26. $fileextname  = "gif";
  27. break;
  28. case "image/png":
  29. $fileextname  =  "png";
  30. break;
  31. default:
  32. $fileextname = false;
  33. break;
  34. }
  35. return $fileextname?date('YmdHis',time()).'.'.$fileextname:false;
  36. }
  37. /**
  38. *
  39. * @param string $filename
  40. * @param string $width
  41. * @param string $height
  42. * @param string $quality
  43. * @param string $savepath
  44. * @return boolean
  45. */
  46. function _make_thumb($filename='', $width=THUMB_WIDTH, $height=THUMB_HEIGHT, $savepath='./upload'){
  47. if(file_exists($filename)){
  48. //上传图片的尺寸
  49. $imagesize=getimagesize($filename);
  50. $imagewidth=$imagesize[0];
  51. $imageheight=$imagesize[1];
  52. $mime = $imagesize['mime'];
  53. //宽高比例
  54. $ratio = $imagewidth/$imageheight;
  55. //新建一个背景图片
  56. $bgimg = imagecreatetruecolor($width, $height);
  57. $white = imagecolorallocate($bgimg, 255, 255, 255);
  58. //填充背景色为白色
  59. imagefill($bgimg,0,0,$white);
  60. if($mime == 'image/gif'){
  61. $im = @imagecreatefromgif($filename); /* Attempt to open */
  62. $outfun = 'imagegif';
  63. }elseif($mime == 'image/png'){
  64. $im = @imagecreatefrompng($filename); /* Attempt to open */
  65. $outfun = 'imagepng';
  66. }else{
  67. $im = @imagecreatefromjpeg($filename); /* Attempt to open */
  68. $outfun = 'imagejpeg';
  69. }
  70. if($ratio > 1){
  71. //宽度较大
  72. if($imagewidth > $width){
  73. //缩放图片到背景图片上
  74. $new_width = $width;
  75. $new_height = ($width*$imageheight)/$imagewidth;
  76. $bg_y = ceil(abs(($height-$new_height)/2));
  77. imagecopyresampled($bgimg, $im, 0, $bg_y, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
  78. }else{
  79. //复制图片到背景图片上
  80. $copy = true;
  81. }
  82. }else{
  83. //高度较大
  84. if($imageheight > $height){
  85. //缩放图片
  86. $new_height = $height;
  87. $new_width = ($height*$imagewidth)/$imageheight;
  88. $bg_x = ceil(($width-$new_width)/2);
  89. imagecopyresampled($bgimg, $im, $bg_x, 0, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
  90. }else{
  91. //复制图片到背景图片上
  92. $copy = true;
  93. }
  94. }
  95. if($copy){
  96. //复制图片到背景图片上
  97. $bg_x = ceil(($width-$imagewidth)/2);
  98. $bg_y = ceil(($height-$imageheight)/2);
  99. imagecopy($bgimg, $im, $bg_x, $bg_y, 0, 0, $imagewidth, $imageheight);
  100. }
  101. $ext = _file_type($mime);
  102. $outfun($bgimg, $savepath.'/'.$ext);
  103. imagedestroy($bgimg);
  104. return $savepath.'/'.$ext;
  105. }else{
  106. return false;
  107. }
  108. }
  109. if($_POST){
  110. $size = $_POST['size']?strtoupper(trim($_POST['size'])):'2M';
  111. $imgsize = $_FILES['img']['size']?$_FILES['img']['size']/(1024*1024):0;
  112. $imgwidth = $imgheight = $_POST['width-height']?intval($_POST['width-height']):300;
  113. //自定定义文件上传大小
  114. ini_set('upload_max_filesize',$size);
  115. $mathsize = str_replace('M','',$size);
  116. if($imgsize>$mathsize){
  117. echo "图片大小不得超过{$size}!";
  118. return;
  119. }
  120. if($file_name = _file_type($_FILES['img']['type'])){
  121. if($_FILES['img']['error'] == UPLOAD_ERR_OK){
  122. $savepath = 'upload/';
  123. if(!is_dir($savepath)){
  124. mkdir($savepath,0644);
  125. }
  126. //生成缩略图
  127. $thumb_file = _make_thumb($_FILES['img']['tmp_name'], $imgwidth, $imgheight, $savepath);
  128. //move_uploaded_file($_FILES['img']['tmp_name'],$savepath.$file_name);
  129. echo "生成后的图片为:<img src='".$thumb_file."' />";
  130. }else{
  131. echo $_FILES['img']['error'];
  132. return;
  133. }
  134. }else{
  135. echo "图片格式不正确,请上传jpg,gif,png的格式!";
  136. return;
  137. }
  138. }else{
  139. echo <<<EOT
  140. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  141. <html>
  142. <head>
  143. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  144. <title>缩放图片保存成正方形</title>
  145. </head>
  146. <body>
  147. <form action="" method="POST" enctype="multipart/form-data">
  148. <div>
  149. <label>上传一张图片:</label>
  150. <input type="file" name="img" />
  151. </div>
  152. <div>
  153. <label>生成缩略图的宽高(单位px):</label>
  154. <input type="text" name="width-height" value="300" />
  155. </div>
  156. <div>
  157. <label>文件大小上限:</label>
  158. <input type="text" name="size" value="2M" />
  159. </div>
  160. <div><input type="submit" name="submit" value="提交" /></div>
  161. </form>
  162. </body>
  163. </html>
  164. EOT;
  165. }

摘自:http://www.icode100.com/posts/view/64

本文非原创,感谢作者提供学习

用php和imagemagick来处理图片文件的上传和缩放处理的更多相关文章

  1. web操作文件的上传到服务器 并可下载 并且读取出来

    1.文件的上传-servlet实现文件上传---核心API—DiskFileItemFactory 一.文件上传概述 l  实现web开发中的文件上传功能,需完成如下二步操作: •    在web页面 ...

  2. 带进度条的文件批量上传插件uploadify

    有时项目中需要一个文件批量上传功能时,个人认为uploadify是快速简便的解决方案. 先上效果图: 一. 下载uploadify 从官网下载uploadify的Flash版本(Flash版本免费,另 ...

  3. C# 用原生JS进行文件的上传

    1.此文章是用原生JS来进行文件的上传,有两个版本,一个不用ajax,一个用ajax. 1)非AJAX <!DOCTYPE html> <html> <head> ...

  4. ssh整合问题总结--在添加商品模块实现图片(文件)的上传

    今天在做毕设(基于SSH的网上商城项目)中碰到了一个文件上传的需求,就是在后台管理员的商品模块中,有一个添加商品,需要将磁盘上的图片上传到tomcat保存图片的指定目录中: 完成这个功能需要两个步,第 ...

  5. 文件的上传(如何兼容火狐与IE)与国际化的原理

    1.文件的上传     [1] 简介         > 将本地的文件上传到服务器中         > 用户需要通过一个表单将文件上传到服务器中       [2] 表单的设置     ...

  6. java实现ftp文件的上传与下载

    最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...

  7. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  8. .Net多文件同时上传(Jquery Uploadify)

    前提:领导给了我一个文件夹,里面有4000千多张产品图片,每张图片已产品编号+产品名称命名,要求是让我把4000多张产品图片上传到服务器端,而且要以产品编码创建n个文件夹,每张图片放到对应的文件夹下. ...

  9. mac下svn问题——“.a”(静态库)文件无法上传解决

    mac下svn问题——“.a”(静态库)文件无法上传解决    “.a”(静态库)文件无法上传(svn工具:Versions)          网上查询了一下,说是Xcode自带的svn和Versi ...

随机推荐

  1. 优秀代码要求(转自http://www.cnblogs.com/brishenzhou/p/6284188.html)

    一段优秀的代码,它一般需要满足以下几个条件: #统一规范# 所有的代码,第一前提必须是统一规范,而常见的统一规范主要包括有以下内容: 1)统一编辑器规范 在团队开发中,我们并不对各个开发人员使用的编辑 ...

  2. 没事抽空学——c语言指针操作基础概念

    指针基础 理解指针的最佳方法是画图,学习使用基本指针,不要产生空指针. 存储控件分配 存储控件分配是指在内存预留空间的过程.就像一个虚拟菜谱一样,指针对应菜名,其所指的内存空间中的数据对应实际的菜. ...

  3. libev中timer时间事件监控器

    1.数据结构 #define ev_at(w) ((WT)(w))->at#define ev_active(w) ((W)(w))->active typedef ev_watcher_ ...

  4. Python第二天课程

    创建列表的方式 list= [XX,XX] 或 list1 = list()使用list方法,将字符串或元祖创建为列表 列表名[其实位置:结束位置] 取列表中的特定元素 >>> na ...

  5. MySQL 5.5 禁用 innodb

    MySQL 5.5 禁用 innodb 编辑: my.ini 添加: default-storage-engine=MYISAM skip-innodb

  6. CSS中RGBA的兼容方法以及透明度计算方法

    CSS对IE使用背景透明 实现 rgba 效果 height:35px; background: -webkit-gradient(linear,left top,left bottom,from(r ...

  7. DPDK l2fwd 浅注

    l2fwd是DPDK中的非常经典的例子.二层转发模型. 就是在相邻的网卡接口间互相传递报文. 网口0和网口1之间报文互传. 网口2和网口3之间报文互传. ............ 运行参数 . 在目录 ...

  8. iOS中的base64加密

    #import <UIKit/UIKit.h> @interface Base64String : NSObject + (NSString *)base64String:(NSStrin ...

  9. 谈一谈第九届移动互联网开发者大会( MDCon 2016 )

    4G时代的开启以及移动终端设备的凸显必将为移动互联网的发展注入巨大的能量,2016年移动互联网产业必将带来前所未有的飞跃.第九届移动互联网开发者大会以"DT时代的移动开发技术创新" ...

  10. PAT 团体程序设计天梯赛-练习集 L1-005. 考试座位号

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...