how would i force a certain image style to be made for a image upload when it is not used in the same content type.
and yes i know that sounds confusing so i will explain what im trying to do.

i got a content type setup with a imagefield that uses one style and that part works fine but i also made a 2nd style that makes a 960x220 version of the same image for use by a script i got in my header which shows a slideshow of images in my header and since this extra image style is used nowhere in this content type there wont be any images made using my extra image style so i need a way to force the image style system to generate images for this style even when it thinks there not in use.

Comments

David D’s picture

Views-type slideshow displays aren't ported quite yet for D7, but such an approach should be able to do what you want. Views in D7 supports designating a particular image style for a given display. I realize that's not something that's going to help you today, though.

http://drupal.org/node/774798 suggests a way you might get the system to generate the image style you need, by creating your own custom display type and designating the style for that field there.

cmcpasserby’s picture

ya thanks

the slideshow banner i already had working with my own code and to force a image style to be made for it i just added a unused display type and used that to set it up.

since my site doesn't use rss i just added my modified display to the rss display type and made that one use the images i want.

though i actully do provide RSS feeds in a later version of the site once there is more content so i need to find a other work around or find a way to make a custom display type.

RobW’s picture

If you don't mind coding the image directly into your theme files you could try something like:

 // load node that contains your imagefield
 $your_node = node_load('nid_of_your_node');

 // create whole themed image with all the attributes
  $image = array(
    'item' => $your_node->field_image['und'][0],
    'image_style' => 'image_style_name',
    'path' => '' // I don't know what path and options do here, but it works fine for me when it's empty.
 
  print theme_image_formatter($image);

 // or just get the path

  $path = image_style_url('image_style_name', $node->field_image['und'][0]['uri']);

  print '<img src="' . $path .  '">'; 

[Edit] I was fooling around with theme_image_formatter() and it throws up an error (although still creates the image) if you don't include a path array in $image. Don't really know what it does though. Also, didn't like me substituting $language for 'und'.

travis-cunningham’s picture

The path array is for "An array containing the link 'path' and link 'options'". Basically it gives you the option of linking your image with all the options.

More on the api here:
http://api.drupal.org/api/drupal/modules--image--image.field.inc/functio...

I hadn't seen the image_style_url yet though.

Thanks for the tip!

I decided to play with the path array and noticed this post:
http://drupal.org/node/1038932

Basically you have to include the options even if they are empty which is being patched.

Example:

$styled_image = array(
'item' => $image, 
'image_style' => 'your_style',
'path' => array('path' => 'your_link_path', 'options' => '')
);
upunkt’s picture

I had to pre-generate images derived from a field in one content type for use with Facebook-Like s. What I did is:

  • Create an image style, 'square_facebook_225x225' in my case. Facebook recommends to have at least 200px in each dimension, the smallest dimension of my images was 225px, so that's why. The style itself uses "Scale & Crop".
  • Create a new block, I called mine 'Create square Facebook Image 225x225'. Place the block anywhere in the list of blocks. Activate the block for a content type.
  • Place the following code in the for block body selecting 'php' as text format and make sure to replace 'field_YOUR_FIELD' with an image field name in your content type.
<?php 
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  $nid = arg(1); //get NID
  $node = node_load($nid); //load node
  if ($files = field_get_items('node', $node, 'field_YOUR_FIELD', $node->language)) {
    foreach ($files as $file) { //loop through all images of that field
      if ($file_loaded = file_load($file['fid'])) {
        $style_name = 'square_facebook_225x225';
        $original_path = file_uri_target($file_loaded->uri); //path to original image
        $style_path = image_style_path($style_name, $file_loaded->uri); //generate path to FB-style
        if (!file_exists($style_path)) { //do not generate if styled image already exists
          image_style_create_derivative(image_style_load($style_name), $file_loaded->uri, $style_path);
        }
      }
    }
  }
}
?>

The code should generate styled images even for fields hosting more than one image. I did not need that so this is untested.
I found much of the code in Gabor Szanto's Image Style Pregenerate-Module so big hands for him. Hope this helps out, especially if you do not want to install views just for this one task.