有时上传图片时因为图片太大了,不仅占用空间,消耗流量,而且影响浏(图片的尺寸大小不一)。下面分享一种等比例不失真缩放图片的方法,这样,不管上传的图片尺有多大,都会自动压缩到我们设置尺寸值的范围之内。经过测试,证明实用。

01 <?php
02 function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
03  {
04   $pic_width = imagesx($im);
05   $pic_height = imagesy($im);
06   
07   if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
08   {
09    if($maxwidth && $pic_width>$maxwidth)
10    {
11     $widthratio = $maxwidth/$pic_width;
12     $resizewidth_tag = true;
13    }
14   
15    if($maxheight && $pic_height>$maxheight)
16    {
17     $heightratio = $maxheight/$pic_height;
18     $resizeheight_tag = true;
19    }
20   
21    if($resizewidth_tag && $resizeheight_tag)
22    {
23     if($widthratio<$heightratio)
24      $ratio = $widthratio;
25     else
26      $ratio = $heightratio;
27    }
28   
29    if($resizewidth_tag && !$resizeheight_tag)
30     $ratio = $widthratio;
31    if($resizeheight_tag && !$resizewidth_tag)
32     $ratio = $heightratio;
33   
34    $newwidth = $pic_width * $ratio;
35    $newheight = $pic_height * $ratio;
36   
37    if(function_exists("imagecopyresampled"))
38    {
39     $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数
40       imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数
41    }
42    else
43    {
44     $newim = imagecreate($newwidth,$newheight);
45       imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
46    }
47   
48    $name = $name.$filetype;
49    imagejpeg($newim,$name);
50    imagedestroy($newim);
51   }
52   else
53   {
54    $name = $name.$filetype;
55    imagejpeg($im,$name);
56   }
57  }
58 //使用方法:
59 $im=imagecreatefromjpeg("./20140416103023202.jpg");//参数是图片的存方路径
60 $maxwidth="600";//设置图片的最大宽度
61 $maxheight="400";//设置图片的最大高度
62 $name="123";//图片的名称,随便取吧
63 $filetype=".jpg";//图片类型
64 resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用上面的函数

处理前图片大小:1187*846

图片处理后大小:561*400

处理后的图片名称:123.jpg

写 在最后:因为客户要求使用php实现等比例不失真缩放上传图片,本来要自己写的,但百度一下发现了这个函数,于是乎就拿来用了,呵呵,省了我不少时间啊! 其实我们想到的一些新功能,网络早已有之,犹其在中国,很多的创新,其实都是从国外翻译过来的,在代码这方面,老外的脑子确实很好使。上面的函数,作者不 详,但还是要感谢作者的辛苦付出。

php 上传缩放图片的更多相关文章

  1. javascript异步上传压缩图片并立即显示图片

    javascript异步上传压缩图片并立即显示图片<pre><!doctype html><html><head><meta charset=&q ...

  2. thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

    今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...

  3. ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合

    一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...

  4. 如何预览将要上传的图片-使用H5的FileAPI

    这篇将要说的东西已经不新鲜了. 参考资料: Reading files in JavaScript using the File APIs (鉴于作者在美国, 我姑且认为作者母语是英语, 当然链接中有 ...

  5. js上传压缩图片

    原文链接:http://blog.csdn.net/iefreer/article/details/53039848 手机用户拍的照片通常会有2M以上,这对服务器带宽产生较大压力. 因此在某些应用下( ...

  6. php 实现接收客户端上传的图片

    今天,遇到一个服务端接收客户端上传图片的需求,经过学习.我写了个简单的demo 以备下次学习. 首先服务器接收的发送图片的请求一定要是post请求,而且请求一定要加上 enctype="mu ...

  7. KindEditor上传本地图片在ASP.NET MVC的配置

    http://www.cnblogs.com/upupto/archive/2010/08/24/1807202.html 本文解决KindEditor上传本地图片在ASP.NET MVC中的配置. ...

  8. .net mvc4 利用 kindeditor 上传本地图片

    http://blog.csdn.net/ycwol/article/details/41824371?utm_source=tuicool&utm_medium=referral 最近在用k ...

  9. 解决uploadify多图片上传部分图片丢失,且不提示任何错误的问题

    这两天用到uploadify的flash版本进行批量图片上传并生成缩略图的功能,之前用uploadify用的好好的,这次突然出现了一个奇怪的问题. 问题描述如下:当我选择单个图片上传的时候,图片上传都 ...

随机推荐

  1. 数数字(Digit Counting,ACM/ICPC Danang 2007,UVa1225)

    #include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[10000]; in ...

  2. 解决maven web项目Cannot detect Web Project version. Please specify version of Web Project through...的错误

    前面已经创建maven web工程,但是问题来了,创建maven web工程之后会出现如下的错误,在pom.xml文件头部 有以下的错误 Description Resource Path Locat ...

  3. doT模板

    框架源码地址 https://github.com/olado/doT <div id="main"> <script id="banner-templ ...

  4. php系统无法上传图片问题

    PHP Warning:  File upload error - unable to create a temporary file in Unknown on line 0   我的测试环境是 w ...

  5. Finish and error to: Error Domain=NSURLErrorDomain Code=-1001 "请求超时。

    错误显示:Finish and error to: Error Domain=NSURLErrorDomain Code=-1001 "请求超时." UserInfo={NSUnd ...

  6. mui 访问手机自带是否连接网络

    //mui检测是否连接网络 function getSysInfo() { //  var str = ""; //  str += "名称:" + plus. ...

  7. JavaBean 反射机制实现自动配置数据

    声明:该版本是没完成的.该博文只做记录代码用 String memberType(String name) throws Exception { return getType(getClass().g ...

  8. 一个简单的jQuery插件开发实例

    两年前写的一个简单的jQuery插件开发实例,还是可以看看的: <script type="text/javascript" src="jquery-1.7.2.m ...

  9. SpringMVC 学习-拦截器 HandlerInterceptor 类

    一.拦截器 HandlerInterceptor 类的作用 SpringMVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 二.怎么使用呢? 1. ...

  10. 《Windows编程循序渐进》——MFC封装机制详解

    单文档