首页 文章

使用Laravel 4 Blade模板进行getimagesize

提问于
浏览
0

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

我有以下foreach循环:

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

@endforeach

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

EDIT

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

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

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

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

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

我不确定这是不是问题 . 当我将其复制并粘贴到我的浏览器中时,它将加载图像没有问题

3 回答

  • 1

    试试这个

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

    现在你可以使用$ width和$ height .

  • 1

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

    <?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)); }}
    
  • 0

    我建议不要在控制器中编写这样的代码 . 如果你需要在其他地方重复这个(大多数时候就是这种情况),那么把它写在控制器中是一个坏主意 . 让控制器变得简单 . 我可能会在我的存储库或类似的东西中解决这个问题......但如果我的小项目我建议定义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 );
    });
    

    你可以看到HTML :: image here的原始实现

相关问题