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;
    }
    

      

  • 相关阅读:
    struts2中<s:select>标签的使用
    正则表达式(括号)、[中括号]、{大括号}的区别小结
    解读邮箱正则表达式:^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$
    Struts2国际化-getText()方法
    Eclipse的Tomcat热部署,免重启的方法
    hdu 5056Boring count
    纸板上的虚拟现实和代码中的Cardboard
    OC-Protocol实现业务代理
    UVA 11237
    mysql相关日志汇总
  • 原文地址:https://www.cnblogs.com/jenqz/p/5647562.html
Copyright © 2011-2022 走看看