【问题标题】:Save resized image with PHP GD in Storage:在存储中使用 PHP GD 保存调整大小的图像:
【发布时间】:2020-01-02 21:31:17
【问题描述】:

如果您能帮助破译我如何将修改后的图像保存在 存储::('用户')。 imagejpeg($imgResized$image_path_name,90);将它保存在公共根文件夹中并以修改后的大小保存,但我不希望它公开,我希望它在存储用户中。提前谢谢你。

$image_path = $request->file('image_path');
if ($image_path) {
    $image_path_name = time().$image_path->getClientOriginalName();

    $img = imagecreatefromjpeg($image_path);
    $imgResized = imagescale($img, 100);
    imagejpeg($imgResized, $image_path_name,90);
    imagedestroy($img);
    imagedestroy($imgResized);

    Storage::disk('users')->put($image_path_name, File::get($image_path));

    $user->image = $image_path_name;
}

    $user->update();

【问题讨论】:

  • 你好@David,欢迎来到 Stack Overflow,没有这样的默认磁盘命名用户,如果你在定义它的地方显示你的 filesystem.php 配置文件会很有帮助
  • 'users' => ['driver' => 'local', 'root' => storage_path('app/users'), 'url' => env('APP_URL').' /storage', 'visibility' => 'public', ], 由我创建以保存用户图像。感谢您的回复@caddy DZ。
  • 查看我更新的答案,你需要的是imagejpeg$to 参数

标签: php laravel


【解决方案1】:

如果您不希望图像文件可公开访问,请从磁盘配置中删除公共可见性属性和 Web 访问 URL
config/filesystems.php

'users' => [
    'driver' => 'local',
    'root' => storage_path('app') . '/users',
],

假设在您的视图中是这样的表单,例如:resources/views/welcome.blade.php

<form action="/file" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="file" name="image_path">
    <button type="submit">Submit</button>
</form>

还有一个存储函数在你的控制器或路由闭包中,例如:routes/web.php 编辑:将存储路径作为第二个参数传递给imagejpeg 函数。

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

Route::get('/', function () {
    return view('welcome');
});
Route::post('file', function (Request $request) {
    $image_path = $request->file('image_path');
    if ($image_path) {
        $image_path_name = time() . $image_path->getClientOriginalName();
        $img = imagecreatefromjpeg($image_path);
        $imgResized = imagescale($img, 100);
        // Specify storage directory
        imagejpeg($imgResized, storage_path('app/users/') . $image_path_name, 90);
        return view('test', compact('imgResized'));
        imagedestroy($img);
        imagedestroy($imgResized);
        Storage::disk('users')->put($image_path_name, File::get($image_path));
    }
});

然后图片会被私下存储在storage/app/users/timestampfilename.extension

【讨论】:

  • 非常感谢@Caddy。现在经过一些修改,代码就像丝绸一样工作。非常感谢
猜你喜欢
  • 2012-08-12
  • 2017-08-10
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
相关资源
最近更新 更多