zoukankan      html  css  js  c++  java
  • thinkphp3.2.x版本中图片上传缩略图的解决方案

    调用方式很简单 get_sc($cover_id,[$width=180,$height=auto,$cut])

    @param $cover_id 图片ID___

    @param $width 宽度___

    @param $height 高___

    @param $cut 是否切割 默认不切割___

    直接返回新图片的url

    可以替换get_cover($cover_id,'path');

    get_cover里做了简单的默认图片判断,大家可以自己改一下。


    /**
     * 获取缩略图
     * @param unknown_type $filename 原图路劲、url
     * @param unknown_type $width 宽度
     * @param unknown_type $height 高
     * @param unknown_type $cut 是否切割 默认不切割
     * @return string
     */
    function getThumbImage($filename, $width = 100, $height = 'auto',$cut=false, $replace = false)
    {
        define('UPLOAD_URL', '');
        define('UPLOAD_PATH', '');
        $filename = str_ireplace(UPLOAD_URL, '', $filename); //将URL转化为本地地址
        $info = pathinfo($filename);
        $oldFile = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $info['extension'];
        $thumbFile = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '_' . $width . '_' . $height . '.' . $info['extension'];
    
        $oldFile = str_replace('\', '/', $oldFile);
        $thumbFile = str_replace('\', '/', $thumbFile);
    
    
        $filename = ltrim($filename, '/');
        $oldFile = ltrim($oldFile, '/');
        $thumbFile = ltrim($thumbFile, '/');
        //原图不存在直接返回
        if (!file_exists(UPLOAD_PATH . $oldFile)) {
            @unlink(UPLOAD_PATH . $thumbFile);
            $info['src'] = $oldFile;
            $info['width'] = intval($width);
            $info['height'] = intval($height);
            return $info;
            //缩图已存在并且 replace替换为false
        } elseif (file_exists(UPLOAD_PATH . $thumbFile) && !$replace) {
            $imageinfo = getimagesize(UPLOAD_PATH . $thumbFile);
            //dump($imageinfo);exit;
            $info['src'] = $thumbFile;
            $info['width'] = intval($imageinfo[0]);
            $info['height'] = intval($imageinfo[1]);
            return $info;
            //执行缩图操作
        } else {
            $oldimageinfo = getimagesize(UPLOAD_PATH . $oldFile);
            $old_image_width = intval($oldimageinfo[0]);
            $old_image_height = intval($oldimageinfo[1]);
            if ($old_image_width <= $width && $old_image_height <= $height) {
                @unlink(UPLOAD_PATH . $thumbFile);
                @copy(UPLOAD_PATH . $oldFile, UPLOAD_PATH . $thumbFile);
                $info['src'] = $thumbFile;
                $info['width'] = $old_image_width;
                $info['height'] = $old_image_height;
                return $info;
            } else {
                //生成缩略图
                // tsload( ADDON_PATH.'/library/Image.class.php' );
                // if($cut){
                //     Image::cut(UPLOAD_PATH.$filename, UPLOAD_PATH.$thumbFile, $width, $height);
                // }else{
                //     Image::thumb(UPLOAD_PATH.$filename, UPLOAD_PATH.$thumbFile, '', $width, $height);
                // }
                //生成缩略图 - 更好的方法
                if ($height == "auto") $height = 0;
                //import('phpthumb.PhpThumbFactory');
                require_once('ThinkPHPLibraryVendorphpthumbPhpThumbFactory.class.php');
    
                $thumb = PhpThumbFactory::create(UPLOAD_PATH . $filename);
                //dump($thumb);exit;
                if ($cut) {
                    $thumb->adaptiveResize($width, $height);
                } else {
                    $thumb->resize($width, $height);
                }
    
                $res = $thumb->save(UPLOAD_PATH . $thumbFile);
    
                //缩图失败
                if (!$res) {
                    $thumbFile = $oldFile;
                }
                $info['width'] = $width;
                $info['height'] = $height;
                $info['src'] = $thumbFile;
    
                return $info;
            }
        }
    }
    
    
    function get_sc($cover_id, $width = 100, $height = 'auto', $cut = false, $replace = false)
    {
    
        $picture = M('Picture')->where(array('status' => 1))->getById($cover_id);
    
        if(empty($picture))
        {
            return 'Public/static/assets/img/nopic.png';
        }
        $attach = getThumbImage($picture['path'], $width, $height, $cut, $replace);
    
        return $attach['src'];
    }

    调用的第三方类库,请到这里类库下载

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    手机端上传图片及java后台接收和ajaxForm提交
    JEECG中datagrid方法自定义查询条件
    微信分享到朋友圈按钮 右上角提示
    Js获取后台集合List的值和下标的方法
    redis系列之数据库与缓存数据一致性解决方案
    替换{0}为指定的字符串(MessageFormat)
    java中对array数组的常用操作
    面试题-Java Web-网络通信
    你应该知道的JAVA面试题
    各大互联网公司java开发面试常问问题
  • 原文地址:https://www.cnblogs.com/stevin-john/p/4768994.html
Copyright © 2011-2022 走看看