使用 Laravel 4 Blade 模板获取图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25689441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:02:49  来源:igfitidea点击:

getimagesize with Laravel 4 Blade template

phplaravel

提问by Lynx

How can I use getimagesize using Laravel 4's blade template?

如何使用 Laravel 4 的刀片模板使用 getimagesize?

I have the following foreach loop:

我有以下 foreach 循环:

@foreach($category as $inventory)
   {{ HTML::image('small/' . $inventory->image . '', '' . $inventory->name . '', array('class' => 'scale-with-grid', 'height' => '280', 'width' => 'auto')); }}

@endforeach

I want the getimagesizefunction to display the height and width for that specific image in the heightand widthimage attributes. Every time I attempted to do it myself but could not get it working correctly.

我希望该getimagesize函数在heightwidth图像属性中显示该特定图像的高度和宽度。每次我尝试自己做但无法正常工作时。

EDIT

编辑

This is what I have tried so far. I think my biggest problem is I am not sure how to access the small folder inside my public directory. I tried using public_path but it's returning a array to string conversion error. However, it seems like when I dump public_path it shows the correct path.

这是我迄今为止所尝试的。我认为我最大的问题是我不确定如何访问公共目录中的小文件夹。我尝试使用 public_path 但它返回一个数组到字符串转换错误。但是,当我转储 public_path 时,它似乎显示了正确的路径。

{{ $image = getimagesize(public_path('small/' . $inventory->image . '')) }}

{{ var_dump($image); die; }}

I am using wamp and when I dump public_path this is the first source I get.

我正在使用 wamp,当我转储 public_path 时,这是我得到的第一个来源。

string 'C:\wamp\www\product\public/small/image.jpg' (length=71)

I am not sure if that's the issue. When I copy and paste that into my browser it will load the image with no problem

我不确定这是否是问题所在。当我将其复制并粘贴到我的浏览器中时,它会毫无问题地加载图像

回答by Alley Shairu

Try this

尝试这个

list($width, $height) = getimagesize("Full path of the image"); 

Now you can use $width and $height.

现在您可以使用 $width 和 $height。

回答by Lynx

I was able to solve it by not using blade brackets and inserting

我能够通过不使用刀片支架并插入来解决它

   <?php list($width, $height) = getimagesize(asset('small/' . $inventory->image . ''));  ?>

   {{ HTML::image('small/' . $inventory->image . '', '' . $inventory->name . '', array('class' => 'scale-with-grid', 'height' => 280, 'width' => $width)); }}

回答by Velja Matic

I would suggest against writing such a code in controller. If you need to repeat this somewhere else (which is the case most of the time) then writing it in controller is a bad idea. Keep you controllers simple. I would probably solve this in my repository or something like that... but if its small project that i suggest defining HTML::macro

我建议不要在控制器中编写这样的代码。如果您需要在其他地方重复此操作(大多数情况下都是这种情况),那么将其写入控制器是一个坏主意。让您的控制器保持简单。我可能会在我的存储库或类似的东西中解决这个问题......但是如果它的小项目我建议定义 HTML::macro

HTML::macro('imageWithSize', function($url, $alt = null, $attributes = array(), $secure = null)
{
    // original - HTMl::image($url, $alt = null, $attributes = array(), $secure = null);

    // get image path  - change this to whatever suits you
    $image_path = public_path($url);

    // get image size 
    list($width, $height) = getimagesize($image_path);
    // add those to attributes array
    $attributes['width'] = $width;
    $attributes['height'] = $height;

    return HTML::image($url, $alt, $attributes, $secure );
});

You can see the original implementatio of HTML::image here

你可以在这里看到 HTML::image 的原始实现