【问题标题】:laravel Backpack Original Image Namelaravel 背包原图名称
【发布时间】:2021-12-02 19:40:43
【问题描述】:

我在我的网站上使用 laravel 背包,我需要上传一些图片,我从他们的网站复制代码并且工作正常,但我需要图片上的原始名称,我尝试了一些东西,但没有工作。

public function setImageAttribute($value)
{
    $attribute_name = "image";
    $disk = "uploads"; 
    $destination_path = 'storage/services/' .date('FY').DIRECTORY_SEPARATOR;
    if ($value==null) {
        Storage::disk($disk)->delete($this->{$attribute_name});
        // set null in the database column
        $this->attributes[$attribute_name] = null;
    }
    if (str_starts_with($value, 'data:image'))
{
    $file = $value;
    $filename = $this->generateFileName($file, $destination_path);
    $image = InterventionImage::make($file)->orientate();
    $fullPath = $destination_path.$filename.'.'.$file->getClientOriginalExtension();
    $this->attributes[$attribute_name] = $fullPath;
}


protected function generateFileName($file, $destination_path)
{
        $filename = basename($file->getClientOriginalName(), '.'.$file->getClientOriginalExtension());            
        $filename = Str::random(20);
        while (Storage::disk('uploads')->exists($destination_path.$filename.'.'.$file->getClientOriginalExtension())) {
            $filename = Str::random(20);
        }
    return $filename;
}

为什么 value 总是采用 base64 图像

有没有办法 git 图片原名?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    据我所知,您将无法检索文件名,因为它始终是 base64 文件。但也许这个解决方法可以帮助你:

    图像字段刀片文件可以在以下位置找到:

    \vendor\backpack\crud\src\resources\views\crud\fields\image.blade.php

    您可以通过在以下位置创建一个同名文件来覆盖它:

    \resources\views\vendor\backpack\crud\fields\image.blade.php

    你可以在那里改变任何东西,也许你可以添加一个带有文件名的隐藏输入。

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2020-11-16
      • 1970-01-01
      • 2021-07-13
      相关资源
      最近更新 更多