zoukankan      html  css  js  c++  java
  • 一个图片缩略图方法

    /*
     *   缩略图
     * @param background 原图
     * @param width 缩略图的宽度
     * @param height 缩略图的高度
     * @param newfile 新图片的名称
     * @param object $geo_info  翻转角度   由函数geo_info 获得 主要针对苹果手机上传图片问题
     */
    function thumb($background, $width, $height, $newfile, $geo_info='') {
        if( isWap() && isset($geo_info['orientation'])){  //手机上传并且图片翻转
            $orientation = $geo_info['orientation'];
    
            if($orientation == 1){
                $degrees = 0;
            }else if($orientation == 3){
                $degrees = 180;
            }else if($orientation == 6){
                $degrees = 270;
            }else if($orientation == 8){
                $degrees = 90;
            }
        }
        //按比例获取缩略图的宽高
        $temp = getimagesize($background);
        $s_type = $temp['mime'];
        $s_w = $temp[0];
        $s_h = $temp[1];
    
        //90度的奇数倍时要宽高互换
        if($orientation == 6 || $orientation==8){
            $tmp_th = $s_w;
            $s_w = $s_h;
            $s_h = $tmp_th;
        }
    
    
        if ($width && ($s_w < $s_h)) {
            $width = ($height / $s_h) * $s_w;
        } else {
            $height = ($width / $s_w) * $s_h;
        }
    
        $new = imagecreatetruecolor($width, $height);
        switch($s_type){
            case 'image/png' :
                $img = imagecreatefrompng($background);
                break;
            case 'image/gif':
                $img = imagecreatefromgif($background);
                break;
            case 'image/jpg':
                $img = imagecreatefromjpeg($background);
                break;
            case 'image/jpeg':
                $img = imagecreatefromjpeg($background);
                break;
            default :
                return false;
        }
        if($degrees>0)$img = imagerotate($img, $degrees, 0xffffff);   //翻转图片
    
        $otsc = imagecolortransparent($img);
        if($otsc >=0 && $otsc < imagecolorstotal($img)){
            $tran=imagecolorsforindex($img, $otsc);
            $newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
            imagefill($new, 0, 0, $newt);
            imagecolortransparent($new, $newt);
        }
        imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
        switch($s_type){
            case 'image/png' :
                imagepng($new, $newfile);
                break;
            case 'image/gif':
                imagegif($new, $newfile);
                break;
            case 'image/jpg':
                imagejpeg($new, $newfile);
                break;
            case 'image/jpeg':
                imagejpeg($new, $newfile);
                break;
            default :
                return false;
        }
        imagedestroy($new);
        imagedestroy($img);
        return true;
    }
    

      

  • 相关阅读:
    年轻人的第一个 Spring Boot 应用,太爽了!
    面试问我 Java 逃逸分析,瞬间被秒杀了。。
    Spring Boot 配置文件 bootstrap vs application 到底有什么区别?
    坑爹的 Java 可变参数,把我整得够惨。。
    6月来了,Java还是第一!
    Eclipse 最常用的 10 组快捷键,个个牛逼!
    Spring Cloud Eureka 自我保护机制实战分析
    今天是 Java 诞生日,Java 24 岁了!
    厉害了,Dubbo 正式毕业!
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
  • 原文地址:https://www.cnblogs.com/jenqz/p/5647562.html
Copyright © 2011-2022 走看看