百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> PHP按一定比例紧缩图片(维持清晰度)
分享文章到:

PHP按一定比例紧缩图片(维持清晰度)

发布时间:09/01 来源:未知 浏览: 关键词:
图片紧缩是我们日常开发中经常使用的操纵,在如今需求许多的状况往往,上传的一张图片会被紧缩成不一样比例的图片,每次去操纵也是一件非常繁琐的事情,于是停止了封装了一个紧缩图片的操纵类,但愿大家碰到后,不消再为写许多紧缩图片代码懊恼了。

紧缩图片的工具类:

<?php
/**
 图片紧缩操纵类
 v1.0
*/
   class Image{
   
   private $src;
   private $imageinfo;
   private $image;
   public  $percent = 0.1;
   public function __construct($src){
   
   $this->src = $src;
   
   }
   /**
   翻开图片
   */
   public function openImage(){
   
   list($width, $height, $type, $attr) = getimagesize($this->src);
   $this->imageinfo = array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
   );
   $fun = "imagecreatefrom".$this->imageinfo['type'];
   $this->image = $fun($this->src);
   }
   /**
   操纵图片
   */
   public function thumpImage(){
   
    $new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//将原图复制带图片载体上面,并且依照必然比例紧缩,极大的保持了清楚度
imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
   }
   /**
   输出图片
   */
   public function showImage(){
   
    header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);
   
   }
   /**
   留存图片到硬盘
   */
   public function saveImage($name){
   
    $funcs = "image".$this->imageinfo['type'];
$funcs($this->image,$name.'.'.$this->imageinfo['type']);
   
   }
   /**
   烧毁图片
   */
   public function __destruct(){
   
   imagedestroy($this->image);
   }
   
   }
 
 
?>

测试:

<?php
require 'image.class.php';
$src = "001.jpg";
$image = new Image($src);
$image->percent = 0.2;
$image->openImage();
$image->thumpImage();
$image->showImage();
$image->saveImage(md5("aa123"));
 
 
?>

结果:

678ad1cf96701d3b40bbb530adfaf66.png

更多PHP相关知识,请拜访PHP教程!

以上就是PHP按必然比例紧缩图片(保持清楚度)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板