【问题标题】:Adjust a width and height to an image (Model-Blade) Laravel调整图像的宽度和高度(模型刀片)Laravel
【发布时间】:2017-09-28 23:58:50
【问题描述】:

我正在尝试调整具有特定宽度和高度的图像,但它与刀片相呼应,我不知道如何。

这是代码:

在刀片中:

<li style="text-align:center"><strong>Fotos:</strong>
    @forelse($publication->photos()->get() as $key => $photo)
    <div>
        {{ $photo->image }}
    </div>
    @empty
    EL usuario no ha agregado fotos
    @endforelse
</li>

在模型中的方法photos():

public function photos()
{
    return $this->hasMany('BookPublicationPhoto', 'published_book_id');
}

这是结果(照片超出页面宽度)

http://imgur.com/5b5ui5J

知道我可以如何进行吗?

解决方案

我有其他具有image方法的模型,所以我只添加了宽度和高度:

public function getImageAttribute()
{
    return HTML::image('img/publication/'.$this->published_book_id.'/'.$this->image_file_name, '-', ['class' => 'thumb', 'width' => 150, 'height' => 200]);
}

【问题讨论】:

  • {{ $photo->image }} 包含什么?图片 URL 或图片标签?你能再描述一下吗?
  • 您好,@PandhiBhaumik 感谢您的回答。 {{ $photo-&gt;image }} 的输出为:kinbu.localhost/img/publication/3/Selección_065.png" class="thumb" alt="-">
  • 错误做法,为什么要将整个图像标签存储在数据库中?
  • @PandhiBhaumik 有什么建议吗?
  • 不,你的做法是对的,我以为你在 DB 中保存了 img 标签,但你使用了自定义模型方法,所以它是正确的。

标签: php laravel laravel-4 blade


【解决方案1】:

您可以安装 LaravelCollective 包,并使用以下代码设置高度、宽度。

{{ HTML::image('image_url','image alt title',['width'=>'100','height'=>'200']) }}

【讨论】:

  • 我解决了。如果你看到我正在调用一个方法 image 这是在其他模型中定义的,例如: public function getImageAttribute() { return HTML::image('img/publication/'.$this->published_book_id.'/'.$ this->image_file_name, '-', ['class' => 'thumb']); } 所以我只是将宽度和高度添加为: public function getImageAttribute() { return HTML::image('img/publication/'.$this->published_book_id.'/'.$this- >image_file_name, '-', ['class' => 'thumb', 'width' => 150, 'height' => 200]); }
  • 我需要15点声望。我不能:(,但希望对标记的答案有所帮助
  • 感谢您的关心
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 2019-01-27
  • 2019-06-06
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多