You are here

function rich_snippets_create_styled_image in Rich Snippets 7

Generates a styled image programmatically.

Parameters

string $image_uri: The URI of the source image.

string $style_name: The machine name of the image style.

Return value

stdClass|FALSE The loaded file object for the styled image, FALSE if the styled image could not be generated.

See also

image_style_deliver()

1 call to rich_snippets_create_styled_image()
rich_snippets_apachesolr_image_indexing_callback in ./rich_snippets.apachesolr.inc
Indexing callback that stores the image uri.

File

./rich_snippets.apachesolr.inc, line 36
Apache Solr Search Integration hook implementations and helper functions.

Code

function rich_snippets_create_styled_image($image_uri, $style_name) {

  // Get the URI of the styled image.
  if (!($derivative_uri = image_style_path($style_name, $image_uri))) {
    watchdog('rich_snippets', 'Error getting URI of styled image for @uri', array(
      '@uri' => $image_uri,
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // If the file doesn't exist, attempt to generate it.
  if (!file_exists($derivative_uri)) {

    // Ensure that the style is loaded.
    if (!($style = image_style_load($style_name))) {
      watchdog('rich_snippets', 'Image style @style could not be loaded.', array(
        '@style' => $style,
      ), WATCHDOG_ERROR);
      $generated = FALSE;
    }

    // Acquire lock, abort if the lock cannot be acquired.
    $lock_name = 'image_style_deliver:' . $style . ':' . drupal_hash_base64($image_uri);
    if (!($lock_acquired = lock_acquire($lock_name))) {
      watchdog('rich_snippets', 'Could not acquire lock @lock.', array(
        '@lock' => $lock_name,
      ), WATCHDOG_WARNING);
      return FALSE;
    }

    // Generates the styled image.
    if (!($created = image_style_create_derivative($style, $image_uri, $derivative_uri))) {
      watchdog('rich_snippets', 'Error generating styled image for @uri', array(
        '@uri' => $image_uri,
      ), WATCHDOG_ERROR);
    }
    lock_release($lock_name);
    if (!$created) {
      return FALSE;
    }
  }

  // Load the file object of the styled image.
  if (!($file = image_load($derivative_uri))) {
    watchdog('rich_snippets', 'Image @uri could not be loaded.', array(
      '@uri' => $derivative_uri,
    ), WATCHDOG_ERROR);
  }
  return $file;
}