将laravel存储与Image干预canvas()一起使用 [英] Use laravel storage with Image intervention canvas()

查看:177
本文介绍了将laravel存储与Image干预canvas()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与流浪汉和宅基地一起运行Laraval 5.4.

Running Laraval 5.4 with Vagrant and Homestead.

看到了有关此问题的其他一些问题,但没有一个提供使用 canvas()的解决方案通过干预/图像方法

Saw a few other questions regarding this issue but none provided a solution which uses the canvas() method by Intervention/Image

Laravel引入了自5.3以来更轻松的存储系统

Laravel introduced a easier storage system since 5.3

我当前的代码:

$path = $request->file('logo')->store('/clients/logos','public');

$canvas = Image::canvas($width, $height);
$image = Image::make($path)->resize($width, $height, function ($constraint)
{
    $constraint->aspectRatio();
});

$canvas->insert($image, 'center');
$canvas->save($path);

$this->logo_path = $path;

此代码创建一个画布,并在其中放置调整大小的图像.

This code creates a canvas and places a resized image inside it.

此代码给出以下错误:

AbstractDecoder.php第335行中的NotReadableException:没有图像源 在AbstractDecoder.php第335行中可读 AbstractDecoder-> init('clients/logos/UupUn1iuDGRsy5Z0bkWHJ6S4v79bfZiXapTO7vLk.jpeg') 在AbstractDriver.php第64行中

NotReadableException in AbstractDecoder.php line 335: Image source not readable in AbstractDecoder.php line 335 at AbstractDecoder->init('clients/logos/UupUn1iuDGRsy5Z0bkWHJ6S4v79bfZiXapTO7vLk.jpeg') in AbstractDriver.php line 64

第一行有效,因为该图像存储在我的存储文件夹中的以下位置:

The first line works, because the image is stored inside my storage folder at the following location:

"/storage/app/public/clients/logo/UupUn1iuDGRsy5Z0bkWHJ6S4v79bfZiXapTO7vLk.jpeg"

"/storage/app/public/clients/logo/UupUn1iuDGRsy5Z0bkWHJ6S4v79bfZiXapTO7vLk.jpeg"

,但是图像以原尺寸存储,因此代码在图像干预部分失败.

but the image is stored in full-size, so the code fails at the image-intervention part.

我尝试过的事情: 我尝试将Image :: make()中的$path变量更改为此:

Things i tried: I tried changing the $path variable inside Image::make() to this:

Storage::disk('public')->url($path)

这将导致以下错误: 无法将图像数据写入路径

which results in following error: Can't write image data to path

( http://test.dev/storage/clients/logos/owUNA5Fn9Os jpeg )

关于该错误的怪异部分是在该错误内部看不到"app"目录.

The weird part about that error is that the 'app' directory is not visible inside that error.

我已经没有足够的想法来解决这个问题了.

I'm running out of ideas to solve this issue.

让它可以在不使用canvas的情况下正常工作,但仍然想知道一种使用canvas()的方法

Got it working without using canvas, but still would like to know a way to use canvas()

这是我目前的工作方式:

This is how i currently got it working:

$path = $logo->hashName('public/clients/logos');

$image = Image::make($logo);

$image->resize($width, $height, function ($constraint)
{
    $constraint->aspectRatio();
});

Storage::put($path, (string) $image->encode(), 'public');

$this->logo_path = $path;

检索图像

{{Storage::url($client->logo_path)}}

推荐答案

我首先想到的是您有错字-代码中同时包含logologos路径.

My first thought was that you have a typo - code contains both logo and logos paths.

然后,我在画布上安装了laravel,并得到了相同的错误.因此,我对路径做了一些改进,现在可以使用了.关键是返回的$ path不是相对于您的存储路径,而是相对于storage/app/public .

Then I installed laravel with canvas and got the same error. So I've just made a little path improvements and now it works. The key point is that the returned $path is relative not to your storage path but to storage/app/public.

$width = 50;
$height = 50;

// here $path is set to "clients/logos/FWGXEf9AJ0NOspFoxelTtGUqmr0YP4ztUMUcqkXc.png"
$path = $request->file('logo')->store('/clients/logos','public');

// creating a canvas
$canvas = Image::canvas($width, $height);

// pass the right full path to the file. Remember that $path is a path inside app/public !
$image = Image::make(storage_path("app/public/" . $path))->resize($width, $height, 
    function ($constraint) {
        $constraint->aspectRatio();
});

$canvas->insert($image, 'center');

// pass the full path. Canvas overwrites initial image with a logo
$canvas->save(storage_path("app/public/" . $path . ".png"));

画布需要完整的路径或chdir(),例如,可以很容易地对其进行检查. Intervention\Image\Image源文件(./vendor/intervention/image/src/Intervention/Image/Image.php).

Canvas needs full paths or chdir() to it, it can be easy checked in e.g. Intervention\Image\Image source file (./vendor/intervention/image/src/Intervention/Image/Image.php).

save方法包含简单的$saved = @file_put_contents($path, $data);,并且没有任何chdir调用.

The save method contains simple $saved = @file_put_contents($path, $data); and no any chdir call.

这篇关于将laravel存储与Image干预canvas()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆