User-Profile-Image
hankin
  • 5
  • 首页
  • 仓库
  • 留言
  • 免费下载v1.3.5
  • 分类
    • Yii2
    • wordpress
    • typeecho
    • server
    • qrcode
    • postgresql
    • php
    • jquery
    • javascript
    • html5
    • go
    • css3
    • coding
  • 页面
    • icon图标
    • 专题
    • 建站维护
    • 留言
    • 站点地图
  • 友链
    • 老赵茶馆
    • 淮城一只猫
    • 雨林寒舍
    • 饼子的博客
    • 轩枫阁
    • 广告联盟大事记
    • 主题笔记
    • 八方博客
    • 张维龙的个人博客
    • 瑞课学院
    • BBJ不败君
    • 悠悠吧
    • Heanny Blog
    • 资源谷
    • Yolen
Help?

Please contact us on our email for need any support

Support
    首页   ›   正文
php

php如何实现图片压缩的同时保持清晰度

2020-04-14 04:59:04
406  0 0

直接展示详细代码:
免费学习视频教程分享:php视频教程
<?php

/**
* 图片压缩类:通过缩放来压缩。如果要保持源图比例,把参数$percent保持为1即可。
* 即使原比例压缩,也可大幅度缩小。数码相机4M图片。也可以缩为700KB左右。如果缩小比例,则体积会更小。
* 结果:可保存、可直接显示。
*/
class imgCompression{

   private $src;
   private $image;
   private $imageinfo;
   private $percent = 0.5;
   /**
    * 图片压缩
    * @param $src 源图
    * @param float $percent  压缩比例
    */
   public function __construct($src, $percent=1)
   {
          $this-&gt;src = $src;
          $this-&gt;percent = $percent;
   }


   /** 高清压缩图片
    * @param string $saveName  提供图片名(可不带扩展名,用源图扩展名)用于保存。或不提供文件名直接显示
    */
   public function compressImg($saveName='')
   {
          $this-&gt;_openImage();
          if(!empty($saveName)) $this-&gt;_saveImage($saveName);  //保存
          else $this-&gt;_showImage();
   }

   /**
    * 内部:打开图片
    */
   private function _openImage()
   {
          list($width, $height, $type, $attr) = getimagesize($this-&gt;src);
          $this-&gt;imageinfo = array(
                 'width'=&gt;$width,
                 'height'=&gt;$height,
                 'type'=&gt;image_type_to_extension($type,false),
                 'attr'=&gt;$attr
          );
          $fun = "imagecreatefrom".$this-&gt;imageinfo['type'];
          $this-&gt;image = $fun($this-&gt;src);
          $this-&gt;_thumpImage();
   }
   /**
    * 内部:操作图片
    */
   private function _thumpImage()
   {
          $new_width = $this-&gt;imageinfo['width'] * $this-&gt;percent;
          $new_height = $this-&gt;imageinfo['height'] * $this-&gt;percent;
          $image_thump = imagecreatetruecolor($new_width,$new_height);
          //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
          imagecopyresampled($image_thump,$this-&gt;image,0,0,0,0,$new_width,$new_height,
          $this-&gt;imageinfo['width'],$this-&gt;imageinfo['height']);
          imagedestroy($this-&gt;image);
          $this-&gt;image = $image_thump;
   }
   /**
    * 输出图片:保存图片则用saveImage()
    */
   private function _showImage()
   {
          header('Content-Type: image/'.$this-&gt;imageinfo['type']);
          $funcs = "image".$this-&gt;imageinfo['type'];
          $funcs($this-&gt;image);
   }
   /**
    * 保存图片到硬盘:
    * @param  string $dstImgName  1、可指定字符串不带后缀的名称,使用源图扩展名 。
    * 2、直接指定目标图片名带扩展名。
    */
   private function _saveImage($dstImgName)
   {
          if(empty($dstImgName)) return false;
          $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   
          //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
          $dstExt =  strrchr($dstImgName ,".");
          $sourseExt = strrchr($this-&gt;src ,".");
          if(!empty($dstExt)) $dstExt =strtolower($dstExt);
          if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);

          //有指定目标名扩展名
          if(!empty($dstExt) &amp;&amp; in_array($dstExt,$allowImgs)){
                 $dstName = $dstImgName;
          }elseif(!empty($sourseExt) &amp;&amp; in_array($sourseExt,$allowImgs)){
                 $dstName = $dstImgName.$sourseExt;
          }else{
                 $dstName = $dstImgName.$this-&gt;imageinfo['type'];
          }
          $funcs = "image".$this-&gt;imageinfo['type'];
          $funcs($this-&gt;image,$dstName);
   }

   /**
    * 销毁图片
    */
   public function __destruct(){
          imagedestroy($this-&gt;image);
   }

}

// eg:
$source = ‘img.jpg’;//原图
$dst_img = ‘img1.jpg’; //可加存放路径
$percent = 1; //压缩比例 原图压缩,不缩放

$image = new imgCompression($source,$percent);
$image->compressImg($dst_img);

?>
相关文章教程推荐:php教程
以上就是php如何实现图片压缩的同时保持清晰度的详细内容,更多请关注php中文网其它相关文章!

微信
分享

相关标签:php 实现 图片 压缩 保持 清晰度

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

上一篇:php调用python失败怎么办

下一篇:php如何实现简体繁体转换

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
hankin
smarty_hankin主题 持续为开发者免费开源!
143文章 290评论 760点赞 192685浏览

关注公众号 回复【下载主题】
加QQ群1018841266
最新评论
+207
随机文章
Yii2 查询3
3年前
h5页面跳转微信小程序页面
1年前
swoole laravel 区别
1年前
php中update是什么意思
1年前
Yii2 查询2
3年前
我的作品




tags
css3 html5 javascript php postgresql qrcode typeecho wordpress wordpress主题 Yii2
Copyright © 2021 网站备案号: 浙ICP备20002401号
smarty_hankin 主题. Designed by hankin
主页
页面
  • icon图标
  • 专题
  • 建站维护
  • 留言
  • 站点地图
博主
hankin
hankin 管理员
博客模板_网站模板_HTML模板_博客模板网站免费下载
143 文章 290 评论 192685 浏览
测试
测试
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付