免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2549 | 回复: 2

PHP图片缩放函数:实现等比例不失真缩放 [复制链接]

论坛徽章:
0
发表于 2015-07-02 09:58 |显示全部楼层
参数说明:
$im 图片对象,应用函数之前,你需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
$maxwidth 定义生成图片的最大宽度(单位:像素)
$maxheight 生成图片的最大高度(单位:像素)
$name 生成的图片名
$filetype 最终生成的图片类型(.jpg/.png/.gif)

特别说明:
  GD库1.6.2版以前支持GIF格式,但因GIF格式使用LZW演算法牵涉专利权,因此在GD1.6.2版之后不支持GIF的格式。如果你是WINDOWS的环境,你只要进入PHP.INI文件找到extension=php_gd2.dll,将#去除,重启APACHE即可,如果你是Linux环境,又想支持GIF,PNG,JPEG,你需要去下载libpng,zlib,以及freetype字体并安装。

[PHP]代码
  1. function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
  2. {
  3.     $pic_width = imagesx($im);
  4.     $pic_height = imagesy($im);
  5.     if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
  6.     {
  7.         if($maxwidth && $pic_width>$maxwidth)
  8.         {
  9.             $widthratio = $maxwidth/$pic_width;
  10.             $resizewidth_tag = true;
  11.         }
  12.         if($maxheight && $pic_height>$maxheight)
  13.         {
  14.             $heightratio = $maxheight/$pic_height;
  15.             $resizeheight_tag = true;
  16.         }
  17.         if($resizewidth_tag && $resizeheight_tag)
  18.         {
  19.             if($widthratio<$heightratio)
  20.                 $ratio = $widthratio;
  21.             else
  22.                 $ratio = $heightratio;
  23.         }
  24.         if($resizewidth_tag && !$resizeheight_tag)
  25.             $ratio = $widthratio;
  26.         if($resizeheight_tag && !$resizewidth_tag)
  27.             $ratio = $heightratio;
  28.         $newwidth = $pic_width * $ratio;
  29.         $newheight = $pic_height * $ratio;
  30.         if(function_exists("imagecopyresampled"))
  31.         {
  32.             $newim = imagecreatetruecolor($newwidth,$newheight);
  33.            imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  34.         }
  35.         else
  36.         {
  37.             $newim = imagecreate($newwidth,$newheight);
  38.            imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  39.         }
  40.         $name = $name.$filetype;
  41.         imagejpeg($newim,$name);
  42.         imagedestroy($newim);
  43.     }
  44.     else
  45.     {
  46.         $name = $name.$filetype;
  47.         imagejpeg($im,$name);
  48.     }         
  49. }
复制代码

论坛徽章:
26
2015亚冠之胡齐斯坦钢铁
日期:2015-06-25 21:40:202015亚冠之柏斯波利斯
日期:2015-08-31 17:03:192015亚冠之柏斯波利斯
日期:2015-11-07 13:10:00程序设计版块每日发帖之星
日期:2015-11-10 06:20:00每日论坛发贴之星
日期:2015-11-10 06:20:00程序设计版块每日发帖之星
日期:2015-11-26 06:20:00程序设计版块每日发帖之星
日期:2015-12-02 06:20:00黄金圣斗士
日期:2015-12-07 17:57:4615-16赛季CBA联赛之天津
日期:2015-12-23 18:34:14程序设计版块每日发帖之星
日期:2016-01-02 06:20:00程序设计版块每日发帖之星
日期:2016-01-06 06:20:00每日论坛发贴之星
日期:2016-01-06 06:20:00
发表于 2015-07-05 20:57 |显示全部楼层
LZ真是个天才
实现了JPEG不失真缩放

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
发表于 2015-07-10 10:05 |显示全部楼层
这个要对图像技术有充分的了解才能做到。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP