zoukankan      html  css  js  c++  java
  • 学习笔记-php图像等比例剪裁-2016.4.7

    因为小区附近宽带升级,所以笔记暂时没有更新,但是学习一直继续!今天继续,

    <?php
    /**
    * Created by PhpStorm.
    * User: 兰小宇
    * Date: 2016/3/30
    * Time: 23:08
    */
    //图像处理类
    class Image{
    private $file; //图像地址
    private $width; //获取图像的宽度
    private $height; //获取图像的高度
    private $type; //获取图像的类型
    private $img; //原来图像的资源句柄
    private $new; //新的资源句柄
    //构造方法
    public function __construct($file){
    $this->file = $_SERVER['DOCUMENT_ROOT'].$file;
    list($this->width,$this->height,$this->type) = getimagesize($this->file);
    $this->img = $this->getType($this->file,$this->type);
    }


    public function thumb($new_height,$new_width){
    if ($this->width < $this->height) {
    //让长度和新高度等比例
    $new_width = ($new_height / $this->height) * $this->width;
    }else{
    //让新高度和新长度等比例
    $new_height = ($new_width / $this->width) * $this->height;
    }
    $this->new = imagecreatetruecolor($new_width,$new_height);
    //创建剪裁后的图像
    imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);
    }



    //判断图像类型,然后加载图像资源
    private function getType($file,$type){
    $img = '';
    switch($type){
    case 1:
    $img = imagecreatefromgif($file);
    break;
    case 2:
    $img = imagecreatefromjpeg($file);
    break;
    case 3:
    $img = imagecreatefrompng($file);
    break;
    default:
    Tool::alertBack('请上传图片类型为gif,jpg,png的文件!');
    }
    return $img;
    }
    //图像输出
    public function out(){
    imagepng($this->new,$this->file);//输出
    imagedestroy($this->img);//销毁资源
    imagedestroy($this->new);//销毁
    }
    }
  • 相关阅读:
    Spring+JCaptcha验证码使用示例
    Hibernate-Session使用的背后
    DWR+Spring配置使用
    Spring+Quartz配置定时任务
    利用HtmlParser解析网页内容
    利用HttpClient4访问网页
    利用Common-Fileupload上传文件图片
    利用Common-BeanUtils封装请求参数
    浮点数的一点东西
    基数排序——浮点数结构体进阶
  • 原文地址:https://www.cnblogs.com/lanxiaoyu/p/5361948.html
Copyright © 2011-2022 走看看