PHP图片压缩功能(按比例图片缩放)(转载)

 1 <?php
 2 function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
 3  {
 4   $pic_width = imagesx($im);
 5   $pic_height = imagesy($im);
 6  
 7   if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
 8   {
 9    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("1.jpg");//参数是图片的存方路径
60 $maxwidth="600";//设置图片的最大宽度
61 $maxheight="400";//设置图片的最大高度
62 $name="123";//图片的名称,随便取吧
63 $filetype=".jpg";//图片类型
64 resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用上面的函数

 

转载于:https://www.cnblogs.com/HoverM/p/8111566.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值