4

By default, Drupal 7 ships with 3 images styles: thumbnail, medium, and large.

screenshot

For example, I upload a image file ('abc.jpg') with the image field (I assume the field name is called 'image'); while I am creating an "Article" node, the image file will be uploaded and stored in "sites/default/files/field/image/abc.jpg," and a record of this uploaded file is saved into database in the "file_managed" table.
By that time, the medium and large styles versions of "abc.jpg" is not yet created.

The teaser of the "Article" node displays the image field with the medium style.

screenshot

Then, I navigate to the teaser viewmode of that node for the first time. The medium version of "abc.jpg" is generated, and sits in "sites/default/files/styles/medium/public/field/image/abc.jpg."

The default view is set to display the image using the "large" style.

screenshot

I view the node using its default view, and the large version of "abc.jpg" is created in "sites/default/files/styles/large/public/field/image/abc.jpg."

Basically, the image upload is quite smart; when an image has been upload, Drupal won't create all the different styles version of that image. Drupal only creates the style of an image when you first browse to the view using that specific image style.

Which functions/hooks are creating the file for the styled images?

1 Answer 1

5

The function that generates the image URL basing on the style is theme('image_style'), which is called by theme('image_formatter'), which then called from image_field_formatter_view(); this function is the implementation of hook_field_formatter_view(), the hook that in a field formatter outputs the content of the field in the view for the entity containing that field.

The image is only generated when you access its URL. I tried the following code:

theme("image_formatter", array("image_style" => "thumbnail", "item" => array("uri" => "test.png", "title" => "Image title", "alt" => "Alternative text")));

The output that I obtained is the following:

<img typeof="foaf:Image" src="http://tero.local/dr72/sites/default/files/styles/thumbnail/public/test.png" alt="Alternative text" title="Image title" />

If you look at the code of image_menu(), you will find the following lines:

  $directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
  $items[$directory_path . '/styles/%image_style'] = array(
    'title' => 'Generate image style', 
    'page callback' => 'image_style_deliver', 
    'page arguments' => array(count(explode('/', $directory_path)) + 1), 
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK,
  );

image_style_deliver() is the function that generates the image.

2
  • thanks @kiamlaluno. I can see that these functions are doing some processing of the original image, but I still can't figure out which one actually write/generate the styled file to the disk.
    – gilzero
    Dec 23, 2011 at 16:31
  • I expanded my answer: The image is generated on-demand.
    – apaderno
    Dec 23, 2011 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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