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_width = 0,$new_height = 0){//为避免不没有传值,所以我们初始化了新宽度和和高度
    //另外这里需要一个判断
    if(empty($new_width) && empty($new_height)){
    $new_width = $this->width;
    $new_height = $this->height;
    }
    //如果传递过来的值不是数字而是字母或者其他,我们也需要进行处理
    if(!is_numeric($new_width) || !is_numeric($new_height)){
    $new_width = $this->width;
    $new_height = $this->height;
    }
    //固定生成图像的宽和高
    $n_w = $new_width;
    $n_h = $new_height;
    //初始化裁剪点
    $cut_w = 0;
    $cut_h = 0;
    //判断原始图像的宽和高
    if ($this->width < $this->height) { //如果原始图像的宽比他的高度小
    //让长度和新高度等比例
    $new_width = ($new_height / $this->height) * $this->width; //新的宽度等于新的高度除以原来的高度再乘以原来的宽度
    //公式解释:在等比例的裁剪中,我们首先要找到等比例的因子,就是按照什么样的比例来进行缩放的
    //如果款比高小,那我们就用新的高度,除以老的高度,得到一个等比例的百分数,然后乘以元来的宽度等于新的宽度
    //例如:原来的是500*1000 ,设定的宽和高为150 * 50
    //那么新的宽度等于 (50/1000)*500
    }else{
    //让新高度和新长度等比例
    $new_height = ($new_width / $this->width) * $this->height;
    }

    //这里我们需要通过另外一个小方法,寻找合适的裁剪点,如下:
    if ($new_width < $n_w) {
    //如果新高度小于新容器高度
    $r = $n_w / $new_width;
    //按长度求出等比例因子
    $new_width *= $r;
    //扩展填充后的长度
    $new_height *= $r;
    //扩展填充后的高度
    $cut_height = ($new_height - $n_h) / 2;//这里一定要用等比例后新的高度度减去容器的高度除以二得到剪裁的点
    //求出裁剪点的高度
    }
    if ($new_height < $n_h) {
    //如果新高度小于容器高度
    $r = $n_h / $new_height;
    //按高度求出等比例因子
    $new_width *= $r;
    // //扩展填充后的长度
    $new_height *= $r;
    // //扩展填充后的高度
    $cut_width = ($new_width - $n_w) / 2;//这里一定要用等比例后新的宽度减去容器的宽度除以二得到剪裁的点
    //求出裁剪点的长度
    }

    $this->new = imagecreatetruecolor($n_w,$n_h);
    //创建剪裁后的图像
    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);//销毁
    }
    }
  • 相关阅读:
    一些链接
    svn
    the source attachment does not contain the source for the file StrutsPrepareAndExecuteFilter.class
    hibernate学习
    hibernate
    mysql front查看所有数据库
    SSD: Single Shot MultiBox Detector 论文解读,附代码
    YOLO算法-论文笔记
    Deep Learning菜鸡篇,我的第一个深度神经网络
    小白成长之路:初识python(六) --python线程池
  • 原文地址:https://www.cnblogs.com/lanxiaoyu/p/5361949.html
Copyright © 2011-2022 走看看