PHPプログラムに関する各種メモ書き

画像ファイルの width, height を取得して挿入するSmarty Plugin

画像ファイルの width, height を取得して挿入するSmarty Plugin

ファイル名 : modifier.image_wh.php で保存

<?php
function smarty_modifier_image_wh($file_name='') {
    if (! $file_name ) { return "error:{$file_name}"; }
    if (!is_file($file_name)) { return "error:{$file_name}"; }
    list($width, $height, $type, $attr) = getimagesize($file_name);
    if ($width && $height){
        return 'width="' .$width. '" height="'.$height.'"';
    }
    else{
        return "error: can not get width height:{$file_name}";
    }
}

使い方

<img src="{$image_file}" {$image_file|image_wh} />{/if}

とすると width="100" height="200" といった情報が入ります。

なお変数に入ったディレクトリも指定する場合は

{$image_file|image_wh}
  ↓
{"`$data_dir`/`$image_file`"|image_wh}

とします。


関連エントリー

No.833
01/05 11:43

edit

Smarty
画像
ファイル