调用方式很简单 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('ThinkPHP\Library\Vendor\phpthumb\PhpThumbFactory.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'];
}

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

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

posted on 2014-06-26 13:01  鸟叔手擒大数据  阅读(572)  评论(0编辑  收藏  举报