laravel admin用阿里云的文件上传如何获取缩略图

项目业务把图片上传到了阿里云的oss,下面是使用方法
github.com/jacobcyl/Aliyun-oss-sto...
返回的是图片路径,我想根据业务需求拿缩略图和原图,比如商品列表时拿缩略图,查看商品详情时显示原图,目前只知道上传后返回的是一个路径,存到了数据库 然后使用时用保存的路径调用Storage::url(‘path/to/img.jpg’)方法获取到一个链接地址,这个应该是原图的链接地址,想知道缩略图如何获取,在线请指教

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

见: 图片处理参数

w_50/quality,q_20

https://cdn.duyumy.com/common/no_image4.png?x-oss-process=image/resize,w_50/quality,q_20

w_500/quality,q_80

https://cdn.duyumy.com/common/no_image4.png?x-oss-process=image/resize,w_500/quality,q_80

1年前 评论
讨论数量: 2

阿里的oss图片处理支持直接通过url传参,很灵活的,我们简单封装了一个类来处理,可以供参考

<?php

namespace Mobile\Logic\utils;

/**
 *
 * Class Picture
 */
class Thumbnail
{
    /** @var string 标准缩略图 320w */
    const THUMB = 'thumb';
    /** @var string 巨图 1280w */
    const LARGE = 'large';
    /** @var string 大图 640w */
    const BIG = 'big';
    /** @var string 小图 160w */
    const SMALL = 'small';
    /** @var string 迷你图 80w */
    const MINI = 'mini';
    /** @var string 微型缩略图 40w */
    const NANO = 'nano';
    /** @var string 头像 100w */
    const AVATAR = 'avatar';
    /** @var string 商品封面 320w 320H */
    const TRADE_THUMB = 'trade-thumb';

    /**
     * 传入一组图片并返回缩略图数组
     * @param string[] $picUrls
     * @param string $thumbScheme
     * @return string[]
     */
    public static function getThumbs($picUrls, $thumbScheme = 'thumb')
    {
        if (is_array($picUrls)) {
            $pics = [];
            foreach ($picUrls as $item) {
                $pics[] = self::getThumb($item, $thumbScheme);
            }
            return $pics;
        }

        return $picUrls;
    }

    /**
     * 获取阿里云图片处理地址
     * @param string $picUrl 原图路径
     * @param string $thumbScheme 缩略图方案
     * @return string 缩略图地址
     */
    public static function getThumb($picUrl, $thumbScheme = 'thumb')
    {
        if (empty($picUrl)) return '';

        $OSS_PIC_DOMAINS = C('OSS_PIC_DOMAINS');

        $exists = false;
        if (substr($picUrl, 0, 4) == 'http') {
            foreach ($OSS_PIC_DOMAINS as $domains => $replace_domains) {
                if (strpos($picUrl, $domains) === 0) {
                    // 替换为CDN域名
                    $picUrl = str_replace($domains, $replace_domains, $picUrl);
                    $exists = true;
                    break;
                } elseif (strpos($picUrl, $replace_domains) === 0) {
                    $exists = true;
                    break;
                }
            }

            // 如果不属于可替换的域名,则原图返回
            if (!$exists) {
                return $picUrl;
            }

            // 已经处理过的图片直接返回
            if (strpos($picUrl, '?x-oss-process=')) {
                return $picUrl;
            } elseif (strpos($picUrl, '!')) {
                $resource = substr($picUrl, 0, strpos($picUrl, '!'));
            } elseif (strpos($picUrl, '@')) {
                $resource = substr($picUrl, 0, strpos($picUrl, '@'));
            } else {
                $resource = $picUrl;
            }

            if ($thumbScheme) {
                return $resource . '!' . $thumbScheme;
            } else {
                return $resource;
            }
        }
        return $picUrl;
    }

    /**
     * 获取原图地址(替换oss域名)
     * @param string $picUrl
     * @return string
     */
    public static function getOriginal($picUrl = '')
    {
       return self::getThumb($picUrl, '');
    }
}
1年前 评论

见: 图片处理参数

w_50/quality,q_20

https://cdn.duyumy.com/common/no_image4.png?x-oss-process=image/resize,w_50/quality,q_20

w_500/quality,q_80

https://cdn.duyumy.com/common/no_image4.png?x-oss-process=image/resize,w_500/quality,q_80

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!