logo

パレット - カラフルに🎨

Palette — ビジュアルページビルダー、デザインの専門知識は不要です。

ライブデモ パレットをダウンロード

Scroll

画像プリセットをプログラムから使用する(programmatically use imagecache)

03/12/2019, by Ivan

imagecache モジュールを使用していた Drupal 6 と比べて、Drupal 7 では API が変わりました。Drupal 6 ではプリセットを次のようにプログラムから使用できました。

<?php print theme('imagecache', ‘my_preset’, $file_path, $alt, $title, $attributes); ?>

例えばこのように:

theme('imagecache', '300x300crop', $first_photo["filepath"], $node->title, $node->title, array('itemprop' => 'image'));

Drupal 7 では少し違う書き方をします。

print theme_image_style(array(
  'style_name' => 'home_category_image',
  'path' => $result['node']->field_image['und'][0]['uri'],
  'alt' => $title,
  'width' => '',
  'height' => '',
  'attributes' => array('class' => 'search-result-img'),
));

今度は theme_image_style 関数を使用して、値の配列を渡します。

style_name — プリセットの名前。

path — 読み込み方式とアドレスの形式のパス。例えば public://product/421-a1 amt.jpg。public は公開読み込み方式です。

alt — 画像の alt。

width、height — 幅と高さは挿入しなくても構いませんが、その場合は notice が発生します。

attributes — 属性も配列で渡します。