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

      

  • 相关阅读:
    jsp文件中charset和pageEncoding的区别
    如果jsp表单元素的值为空,如何避免null出现在页面上?
    C# 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。
    正则表达式
    事件委托与键盘事件
    事件对象的兼容性
    作用域解析题
    事件冒泡与事件铺获的解析
    浏览器内核
    js中级总结
  • 原文地址:https://www.cnblogs.com/jenqz/p/5647562.html
Copyright © 2011-2022 走看看