logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll

Using image presets programmatically (programmatically use imagecache)

16/04/2025, by Ivan

! Legacy Drupal 7 code !

Compared to Drupal 6 where we used the imagecache API module, in Drupal 7 it has changed. In Drupal 6 we could use a preset programmatically as follows:

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

For example, like this:

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

But in Drupal 7, we write it a bit differently:

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'),
));

Now we use the theme_image_style function and pass an array of values:

style_name – the name of the preset.

path – the path in the form of the scheme and address, for example: public://product/421-a1 amt.jpg, where public is the public scheme.

alt – the image alt text.

width, height – width and height can be omitted, but doing so will cause a notice.

attributes – attributes are also passed as an array.