在Laravel中生成虚假图像

huangapple go评论61阅读模式
英文:

Seed fake images in laravel

问题

我试图生成一个虚假的图像来填充种子数据。

我使用以下代码创建了一个种子:

$this->faker->image(storage_path('app/public/products'), 500, 500)

当我尝试通过 Laravel Nova 访问它们以查看它们时,我得到这种类型的URL:http://localhost/storage/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png

在数据库中,它保存为:/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png

我做错了什么,有什么想法吗?

英文:

I'm trying to generate a fake image to populate seeder data.

I created a seeder with this:

$this->faker->image(storage_path('app/public/products'), 500, 500)

When I try to access them through Laravel Nova to see them, I get this kind of URL: http://localhost/storage/var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png

In the DB it's saved as: /var/www/html/storage/app/public/products/c8831002794cc55fe046c5e2b65794d4.png

Any idea what I've done wrong?

答案1

得分: 1

请查看 image()方法签名

function image(
    ?string $dir = null,
    int $width = 640,
    int $height = 480,
    ?string $category = null,
    bool $fullPath = true,
    bool $randomize = true,
    ?string $word = null,
    bool $gray = false,
    string $format = 'png'
)

$fullPath 设置为 false 将解决您的问题:

$this->faker->image(storage_path('app/public/products'), 500, 500, null, false)
英文:

See the method signature for image():

function image(
    ?string $dir = null,
    int $width = 640,
    int $height = 480,
    ?string $category = null,
    bool $fullPath = true,
    bool $randomize = true,
    ?string $word = null,
    bool $gray = false,
    string $format = 'png'
)

Setting $fullPath to false will fix your issue:

$this->faker->image(storage_path('app/public/products'), 500, 500, null, false)

答案2

得分: 0

尝试这个:

$this->faker->image('app/public/products', 400, 300, null, false)

如果你有一个视图,那么:

<img src="/my/path/{{ $item->image }}" >
英文:

Try this:

$this-&gt;faker-&gt;image(&#39;app/public/products&#39;,400,300, null, false) 

if you have a view then:

&lt;img src=&quot;/my/path/{{ $item-&gt;image}}&quot; &gt;

huangapple
  • 本文由 发表于 2023年2月19日 06:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定