2014-09-05 143 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