如何优雅的处理上传的图片的URL?

上传图片接口是应该返回绝对路径还是相对路径?返回绝对路径的话就要用修改器处理URL,如何优雅的处理
我现在就是把url替换掉,感觉不优雅,有没有更好的方式?

strtr($image, [config('filesystems.disks.public.url') => '']);
让PHP再次伟大
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案
上传完肯定是全路径返回,只有保存到数据库的时候才会把地址前缀去掉保存
2年前 评论
勇敢的心 (楼主) 2年前
luyang (作者) 2年前
讨论数量: 5

上传文件前一半会先定义好存储路径+文件名吧,这两个拼接的结果就是相对路径

2年前 评论
上传完肯定是全路径返回,只有保存到数据库的时候才会把地址前缀去掉保存
2年前 评论
勇敢的心 (楼主) 2年前
luyang (作者) 2年前

返回相对路径吧,然后用这两个助手函数转化为 url 和 绝对路径

if (!function_exists('storageUrl')) {
    /**
     * @param $path string image/material/2021/05/27/26rZIFXN2OYYrXs2tMnHe5NAEheVTKPNGgb4lf3g.png
     * @return string http://api.material.local:81/storage/image/material/2021/05/27/26rZIFXN2OYYrXs2tMnHe5NAEheVTKPNGgb4lf3g.png
     */
    function storageUrl($path)
    {
        $path = ltrim($path, '/');
        $url = config('app.http_api_url') . '/storage/' . $path;
        return $url;
    }
}
if (!function_exists('storageFilePath')) {
    /**
     * @param $path string image/system/flag_return_back.jpg
     * @return string /var/www/storage/app/public/image/system/flag_return_back.jpg
     */
    function storageFilePath($path)
    {
        $path = ltrim($path, '/');
        $path = ltrim($path, '\\');
        $path = storage_path('app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . $path);
        return $path;
    }
}
2年前 评论

数据库存的是相对,DRY :flushed:

2年前 评论

这是 laravel-jetstream 处理用户头像的代码,我觉得挺优雅的。

<?php

namespace Laravel\Jetstream;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Laravel\Jetstream\Features;

trait HasProfilePhoto
{
    /**
     * Update the user's profile photo.
     *
     * @param  \Illuminate\Http\UploadedFile  $photo
     * @return void
     */
    public function updateProfilePhoto(UploadedFile $photo)
    {
        tap($this->profile_photo_path, function ($previous) use ($photo) {
            $this->forceFill([
                'profile_photo_path' => $photo->storePublicly(
                    'profile-photos', ['disk' => $this->profilePhotoDisk()]
                ),
            ])->save();

            if ($previous) {
                Storage::disk($this->profilePhotoDisk())->delete($previous);
            }
        });
    }

    /**
     * Delete the user's profile photo.
     *
     * @return void
     */
    public function deleteProfilePhoto()
    {
        if (! Features::managesProfilePhotos()) {
            return;
        }

        Storage::disk($this->profilePhotoDisk())->delete($this->profile_photo_path);

        $this->forceFill([
            'profile_photo_path' => null,
        ])->save();
    }

    /**
     * Get the URL to the user's profile photo.
     *
     * @return string
     */
    public function getProfilePhotoUrlAttribute()
    {
        return $this->profile_photo_path
                    ? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
                    : $this->defaultProfilePhotoUrl();
    }

    /**
     * Get the default profile photo URL if no profile photo has been uploaded.
     *
     * @return string
     */
    protected function defaultProfilePhotoUrl()
    {
        return 'https://ui-avatars.com/api/?name='.urlencode($this->name).'&color=7F9CF5&background=EBF4FF';
    }

    /**
     * Get the disk that profile photos should be stored on.
     *
     * @return string
     */
    protected function profilePhotoDisk()
    {
        return isset($_ENV['VAPOR_ARTIFACT_NAME']) ? 's3' : config('jetstream.profile_photo_disk', 'public');
    }
}
2年前 评论

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