2014-09-05 144 views
0

如何使用Laravel 4刀片模板使用getimagesize?用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图像属性,这些属性的特定图像的高度和宽度。每次我尝试自己做,但无法正常工作。

编辑

这是我到目前为止已经试过。我认为我最大的问题是我不知道如何访问我的公共目录中的小文件夹。我尝试使用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) 

我不确定是否是这个问题。当我复制并粘贴到我的浏览器就会加载图像没有问题

+2

你如何获得控制器内所需的信息? – sikhlana 2014-09-05 15:40:28

+0

我同意@sikhlana。没有理由不在控制器中执行此操作,并将信息作为阵列的一部分传递到刀片模板。 – Ohgodwhy 2014-09-05 15:43:13

+0

当我在刀片模板中使用foreach循环时,不知道如何将其通过控制器。 – Lynx 2014-09-05 15:49:28

回答

1

试试这个

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

现在你可以使用$宽度和$高度。

+0

我认为这会奏效。但我得到一个数组字符串转换错误。不知道为什么会这样。 – Lynx 2014-09-05 15:51:30

+0

你能显示你的代码吗? – 2014-09-05 15:54:17

+0

将其添加到我编辑的问题中。谢谢您的帮助! – Lynx 2014-09-05 15:58:11

0

我能够不使用刀片托架和插入

<?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的小项目::宏观

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的原始实现The ::图像here