3

I am running a Drupal 7 site that does a call to an external service using Feeds Module, creates nodes from that coming JSON.

The Feeds Module grabs an image, download it, and generate 4 different image styles. Usually in Drupal, you upload the image that goes through Imagecache, and boom the image is re-sized, why because after node_save you go to node_view that grabs a function which does the real resizing.

In my case, i do not view the nodes, so the images does not get generated unless i run a whole view/page i created with all images so they get generated.

I tried to curl and request the page on a time interval for the page to get generated but it seems you have to really view the page. What would a solution for this problem be ?

Would doing a batch operation to just view the nodes using node_view help ? What else ?

Note that on each Feeds request the site creates around 50 nodes X 4 images = 200 image.

Help is much appreciated.

migrated from stackoverflow.com Feb 14 '12 at 14:15

This question came from our site for professional and enthusiast programmers.

  • The resizing is done when the URL is requested for the cached image, not when the node is viewed. Just request the URL for the image and it will be generated by Imagecache – Clive Feb 14 '12 at 10:20
  • Yes that is right, i can run a page that has all images and i am good to go. But i am using these images in an external platform, requesting the images is not the best way to do it. I mean i cannot run the page every 1 hour to get my images. Any other solution for requesting images ? – Valor Feb 14 '12 at 13:50
  • I think you're missing the point of the image caching functionality in Drupal...you request the URL for the image file directly, not the node page. It's the request for the image URL that causes Drupal to either serve an already created image from cache, or create a new one if none exists. Viewing a node page causes this as a side effect because your browser requests the image URL to display it in your page. Viewing the node page has absolutely nothing to do with the images being generated other than what I've already mentioned. Hope that clears things up – Clive Feb 14 '12 at 14:09
10

A derivative image is generated on the fly when served over HTTP. When the derivative image is requested in HTTP, if the file doesn't exists, the HTTP request is handled by Drupal. Drupal will use the image_style_deliver() page callback to generate the derivative image and deliver it. Once the image exists, Drupal will not handle the request and the file is directly delivered.

If you need to access to files externally without using HTTP to access them, you can use image_style_create_derivative($style, $source, $destination) to create the derivatives images. You will need to provide the destination path for the derivative image, which you can get with the image_style_path($style_name, $uri) function.

The following (untested) code should ensure that the derivatives for the image file identified by $fid are generated for the styles whose names are in the $styles array.

$original = file_load($fid);
foreach ($styles as $style_name) {
  $derivative_uri = image_style_path($style_name, $original->uri);
  image_style_create_derivative($style_name, $original->uri, $derivative_uri);
}
  • That is a great explanation, thank you! So what will happen if you change an image style, say change the width from 100 to 80 pixels for the "thumbnail style"? Will Drupal delete all the images in the "thumbnail" directory? – uwe Feb 19 '12 at 21:00
  • 1
    Yes, when a style or its effects are edited, image_style_flush will be called to delete all the derivative files. After file deletion, hook_image_style_flush implementations are invoked and may be used to re-generate the derivative files if needed. Since this would be a long operations, I suggest using the Queue API to delay the operations and split it in smaller chunks of work. – Pierre Buyle Feb 20 '12 at 14:00

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.