当前位置:首页 > PHP教程 > php图像处理 > 列表

php实现图片压缩处理

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-26 08:57:49 浏览: 评论:0 

这篇文章主要为大家详细介绍了php实现图片压缩处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

本文实例为大家分享了php实现图片压缩处理的具体代码,供大家参考,具体内容如下

说明

在项目中,经常会遇到在前端页面展示用户自己上传的图片,当部分图片尺寸过大,页面图片过多的情况下(如论坛里需要显示用户头像),会引起页面加载缓慢的问题。由于用户图片已存储导数据库,无法改变库里的图片大小,只能在获取图片路径时,压缩图片。

示例

以下函数为图片压缩方法

  1. /** 
  2.  * 图片压缩处理 
  3.  * @param string $sFile 图片路径 
  4.  * @param int $iWidth 自定义图片宽度 
  5.  * @param int $iHeight 自定义图片高度 
  6.  */ 
  7. function getThumb($sFile,$iWidth,$iHeight){ 
  8.   //判断该图片是否存在 
  9.   if(!file_exists(public_path().$sFile)) return $sFile; 
  10.   //判断图片格式 
  11.   $attach_fileext = get_filetype($sFile); 
  12.   if (!in_array($attach_fileext, array('jpg','png','jpeg'))){ 
  13.     return $sFile; 
  14.   } 
  15.   //压缩图片 
  16.   $sFileNameS = str_replace(".".$attach_fileext, "_".$iWidth.'_'.$iHeight.'.'.$attach_fileext, $sFile); 
  17.   //判断是否已压缩图片,若是则返回压缩图片路径 
  18.   if(file_exists(public_path().$sFileNameS)){ 
  19.     return $sFileNameS; 
  20.   } 
  21.   //解决手机端上传图片被旋转问题 
  22.   if (in_array($attach_fileext, array('jpeg')) ){ 
  23.     adjustPicOrientation(public_path().$sFile); 
  24.   } 
  25.   //生成压缩图片,并存储到原图同路径下 
  26.   resizeImage(public_path().$sFile, public_path().$sFileNameS, $iWidth, $iHeight); 
  27.   if(!file_exists(public_path().$sFileNameS)){ 
  28.     return $sFile; 
  29.   } 
  30.   return $sFileNameS; 
  31. } 
  32.  
  33. /** 
  34.  *获取文件后缀名 
  35.  */ 
  36. function get_filetype($filename) { 
  37.   $extend = explode("." , $filename); 
  38.   return strtolower($extend[count($extend) - 1]); 
  39. } 
  40.  
  41. /** 
  42.  * 解决手机上传图片被旋转问题 
  43.  * @param string $full_filename 文件路径 
  44.  */ 
  45. function adjustPicOrientation($full_filename){ 
  46.   $exif = exif_read_data($full_filename); 
  47.   if($exif && isset($exif['Orientation'])) { 
  48.     $orientation = $exif['Orientation']; 
  49.     if($orientation != 1){ 
  50.       $img = imagecreatefromjpeg($full_filename); 
  51.  
  52.       $mirror = false; 
  53.       $deg  = 0; 
  54.  
  55.       switch ($orientation) { 
  56.         case 2: 
  57.           $mirror = true; 
  58.           break; 
  59.         case 3: 
  60.           $deg = 180; 
  61.           break; 
  62.         case 4: 
  63.           $deg = 180; 
  64.           $mirror = true; 
  65.           break; 
  66.         case 5: 
  67.           $deg = 270; 
  68.           $mirror = true; 
  69.           break; 
  70.         case 6: 
  71.           $deg = 270; 
  72.           break; 
  73.         case 7: 
  74.           $deg = 90; 
  75.           $mirror = true; 
  76.           break; 
  77.         case 8: 
  78.           $deg = 90; 
  79.           break; 
  80.       } 
  81.       if ($deg) $img = imagerotate($img, $deg, 0); 
  82.       if ($mirror) $img = _mirrorImage($img); 
  83.       //$full_filename = str_replace('.jpg', "-O$orientation.jpg", $full_filename);新文件名 
  84.       imagejpeg($img, $full_filename, 95); 
  85.     } 
  86.   } 
  87.   return $full_filename; 
  88. } 
  89.  
  90. resizeImage(public_path().$sFile, public_path().$sFileNameS, $iWidth, $iHeight); 
  91.  
  92. /** 
  93.  * 生成图片 
  94.  * @param string $im 源图片路径 
  95.  * @param string $dest 目标图片路径 
  96.  * @param int $maxwidth 生成图片宽 
  97.  * @param int $maxheight 生成图片高 
  98.  */ 
  99. function resizeImage($im, $dest, $maxwidth, $maxheight) { 
  100.   $img = getimagesize($im); 
  101.   switch ($img[2]) { 
  102.     case 1: 
  103.       $im = @imagecreatefromgif($im); 
  104.       break; 
  105.     case 2: 
  106.       $im = @imagecreatefromjpeg($im); 
  107.       break; 
  108.     case 3: 
  109.       $im = @imagecreatefrompng($im); 
  110.       break; 
  111.   } 
  112.  
  113.   $pic_width = imagesx($im); 
  114.   $pic_height = imagesy($im); 
  115.   $resizewidth_tag = false; 
  116.   $resizeheight_tag = false; 
  117.   if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { 
  118.     if ($maxwidth && $pic_width > $maxwidth) { 
  119.       $widthratio = $maxwidth / $pic_width; 
  120.       $resizewidth_tag = true; 
  121.     } 
  122.  
  123.     if ($maxheight && $pic_height > $maxheight) { 
  124.       $heightratio = $maxheight / $pic_height; 
  125.       $resizeheight_tag = true; 
  126.     } 
  127.  
  128.     if ($resizewidth_tag && $resizeheight_tag) { 
  129.       if ($widthratio < $heightratio) 
  130.         $ratio = $widthratio; 
  131.       else 
  132.         $ratio = $heightratio; 
  133.     } 
  134.  
  135.  
  136.     if ($resizewidth_tag && !$resizeheight_tag) 
  137.       $ratio = $widthratio; 
  138.     if ($resizeheight_tag && !$resizewidth_tag) 
  139.       $ratio = $heightratio; 
  140.     $newwidth = $pic_width * $ratio; 
  141.     $newheight = $pic_height * $ratio; 
  142.  
  143.     if (function_exists("imagecopyresampled")) { 
  144.       $newim = imagecreatetruecolor($newwidth, $newheight); 
  145.       imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); 
  146.     } else { 
  147.       $newim = imagecreate($newwidth, $newheight); 
  148.       imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height); 
  149.     } 
  150.  
  151.     imagejpeg($newim, $dest); 
  152.     imagedestroy($newim); 
  153.   } else { 
  154.     imagejpeg($im, $dest); 
  155.   } 
  156. }

Tags: php图片压缩

分享到: