6

I am trying to display thumbnails of uploaded images in a custom form. While I can see the images in my form array after uploading them to the tmp directory in Drupal, here is what I am trying to use to display them:

$url = file_create_url($form_object->uploads[$key]->uri);
$output .= '<div class="grid-3">'.theme_image_style(array('style' => 'form-preview', 'path' => $url)).'</div>';

While $url points to the file resource, I cannot show a resized image for the life of me. If I use an img tag and put $url as its source, the images do show up, but they are huge.

Can I not call theme_image_style() because these are just temp images and not permanent? I can make them appear with theme_image().

4

As suggested, use theme('image_style', ...), from the docs - just change your code to this:

url = file_create_url($form_object->uploads[$key]->uri);
$output .= '<div class="grid-3">'.theme('image_style', array('style_name' => 'form-preview', 'path' => $url)).'</div>';
| improve this answer | |
  • Should be style_name, not just style (see associative array options for $variables in googletorp's answer). – Joshua Stewardson May 18 '12 at 18:51
9

As always, the Drupal documentation has the answer. You need to move the file to the files directory. Also you should never call a theme function directly but instead call the wrapper theme function so:

theme_image_style(...) // Don't do this ever

theme('image_style', ...) // Do it like this and make themers happy.

From the API docs:

$variables An associative array containing:

  • style_name: The name of the style to be used to alter the original image.
  • path: The path of the image file relative to the Drupal files directory. This function does not work with images outside the files directory nor with remotely hosted images.
  • alt: The alternative text for text-based browsers.
  • title: The title text is displayed when the image is hovered in some popular browsers.
  • attributes: Associative array of attributes to be placed in the img tag.
| improve this answer | |
1

is better add your class with attributes property something like

theme_image_style(array('style' => 'form-preview', 'path' => $url,'attributes'=>array('class'=>array('class1', 'class2')));
| improve this answer | |
0

I had to resolve this for serving files from sites/all/modules and the answer can be seen in Theme Image Style for image located in module directory

Involves generating a custom theme callback that handles any file and creates the image derivatives during execution of the page request, so you link directly to the file rather than doing an async request that creates the image.

Better suggestions / workarounds welcome!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.