Using setAttributes() returns a fatal error I have the following code in a rule:

$imgpath = file_load(($node-&gt;field_image[LANGUAGE_NONE][0]['fid']))-&gt;uri;
$image = theme('image_style', array('path' =&gt; $imgpath, 'style_name' =&gt; 'thumbnail'));

$link = l($image, "node/$node-&gt;nid", array('html' =&gt; TRUE));
print $link;


I need to add a class to the thumbnail picture. I change the code as follows, but I get a fatal error about calling a member function on a non-object.

$imgpath = file_load(($node-&gt;field_image[LANGUAGE_NONE][0]['fid']))-&gt;uri;
$image = theme('image_style', array('path' =&gt; $imgpath, 'style_name' =&gt; 'thumbnail'));
$image-&gt;setAttribute("class", "AnyClass");

$link = l($image, "node/$node-&gt;nid", array('html' =&gt; TRUE));
print $link;


What am I doing wrong?
The error is simply caused from the fact you are trying to access a member function for something that is an object; in fact, theme_image_style() is returning a string, not an object. This is true also for Drupal 8.


  Returns HTML for an image using a specific image style.


To set attributes, just use the following code.

$imgpath = file_load(($node-&gt;field_image[LANGUAGE_NONE][0]['fid']))-&gt;uri;
$image = theme('image_style', array('path' =&gt; $imgpath, 'style_name' =&gt; 'thumbnail', 'attributes' =&gt; array('class' =&gt; array('AnyClass'))));

$link = l($image, "node/$node-&gt;nid", array('html' =&gt; TRUE));
print $link;


It works because theme_image_style(), as theme_image(), accepts a list of attributes to add to the image tag.