5

Hello fellow druaplistas!

I have a situation where I need to output an image field using different image styles depending on some checkbox settings.

I'm doing this inside a node template. Basically this goes like this:

if ($layout == 1) {
  print $small_image;
} 
if ($layout == 2 {
  print $medium_image;
}

To get the url to the correct sized image I can use image_style_url, but this way I have to build the links to the bigger version to open inside colorbox manually. Is there some function available that I can pass two image styles and it uses them to generate an image tag wrapped with a link to the bigger sized image?

Update: I took Clive's code (thanks again) and added a second parameter to be able to choose the preset that will be shown inside the colobox. Also Title and Alt is now included: As your code was better than mine I took it and extended the stuff I needed, maybe it's of use for someone:

function _colorbox_link($image, $style_name, $style_name_link = 'colorbox_full') {
  $image_style = theme('image_style', array(
    'style_name' => $style_name,
    'path'       => $image['uri'],
    'alt'        => $image['alt'],
    'title'      => $image['title']
  ));

  $url = image_style_url($style_name_link, $image['uri']);

  $link = l($image_style, $url, array(
    'html' => TRUE,
    'attributes' => array(
      'rel' => array('colorbox-gallery'),
      'class' => array('colorbox')
    )
   ));
  return $link;
}
5

I don't think there is, weirdly enough I've just been looking for pretty much the same thing...I couldn't find one so I just wrote a helper function:

function _lightbox_link($image, $style_name) {
  $image_style = theme('image_style', array(
    'style_name' => $style_name,
    'path' => $image['uri'],
    'alt' => $image['alt']
  ));

  $url = image_style_url($style_name, $image['uri']);

  $link = l($image_style, $url, array(
    'html' => TRUE,
    'attributes' => array(
      'rel' => array('lightbox-gallery')
    )
  ));

  return $link;
}
  • Thanks for sharing, I ended up doing something similar ;) – klickreflex Mar 8 '12 at 12:36

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.