【问题标题】:Duplicating an image from a file path, changing name and saving it to a different table in laravel从文件路径复制图像,更改名称并将其保存到 laravel 中的不同表
【发布时间】:2017-07-07 21:41:07
【问题描述】:

我在表上有一个文件路径,我想做的是从文件路径创建图像,然后用不同的名称保存“新”文件。

因为我只有文件路径,所以我现在不知道如何创建图像对象,以便我可以 getClientOriginalExtension();并将其保存到数据库中。我尝试了以下方法:

$img = $var->image_path;
$file = file_get_contents($img);
$filename = time() . '.' . $file->getExtension();

Image::make($file)->resize(300, 300)->save( public_path('/test' . $filename ) );

但是脚本错误:Call to a member function getExtension() on string 文件路径的正确方法是什么,创建对象,更改文件名,确保设置了正确的扩展名(可能超出了这个 q 的范围),然后保存新创建的图像到不同的文件夹,并将新创建的图像路径保存到数据库。

我希望这是有道理的。

更新:我应该改用 file_put_contents() 吗?

【问题讨论】:

    标签: php image laravel


    【解决方案1】:

    使用copy() 并迭代每个图像并相对调整图像,实现了我想要的。

    $title = $var->name;
    $string = str_replace(' ', '-', $title); // Replaces all spaces with hyphens.
    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
    
    copy($img, public_path('/test' . $string . time()));
    

    http://php.net/manual/en/function.copy.php

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多