You are here

function img_assist_render_image in Image Assist 6

Same name and namespace in other branches
  1. 5.3 img_assist.module \img_assist_render_image()
  2. 5 img_assist.module \img_assist_render_image()
  3. 5.2 img_assist.module \img_assist_render_image()
  4. 6.2 img_assist.module \img_assist_render_image()

Return image HTML.

Related topics

2 calls to img_assist_render_image()
img_assist_filter in ./img_assist.module
Implementation of hook_filter().
img_assist_properties_form_validate in ./img_assist.module

File

./img_assist.module, line 1259
Image Assist module

Code

function img_assist_render_image($attributes = array()) {
  global $user;
  if ($attributes['nid']) {
    $node = node_load($attributes['nid']);

    // Get size.
    $width = $attributes['width'];
    $height = $attributes['height'];
    if ($width || $height) {

      // Check to ensure that the dimensions don't exceed the max set in the
      // img_assist settings.
      $max_size = explode('x', variable_get('img_assist_max_size', '640x640'));
      $width = $width <= $max_size[0] ? $width : $max_size[0];
      $height = $height <= $max_size[1] ? $height : $max_size[1];

      // Get width and height of original size.
      $original_size = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
      if ($width == $original_size['width'] && $height == $original_size['height']) {

        // Nothing to process, this is the original image size.
        $closest_std_size = IMAGE_ORIGINAL;
        $create_custom = FALSE;
      }
      else {

        // Get width and height of preview size.
        $preview_size = image_get_info(file_create_path($node->images[IMAGE_PREVIEW]));
        $preview_width = $preview_size['width'];
        $preview_height = $preview_size['height'];
        if ($preview_width && $preview_height) {

          // Get aspect ratio from preview image dimensions.
          $aspect_ratio = round($preview_width / $preview_height, 6);

          // Get new width and height for this inline image.
          // If height is either left out or larger than width then
          // width is the controlling factor.
          if (!$height || round($width / $aspect_ratio) <= $height) {
            $height = round($width / $aspect_ratio);
          }
          else {
            $width = round($height * $aspect_ratio);
          }

          // Compare new width and height to existing image derivative sizes.
          $diag_size_new = sqrt(pow($width, 2) + pow($height, 2));
          $closest_difference = 9999;
          foreach (image_get_sizes() as $key => $stdsize) {
            $width_std = $stdsize['width'];
            $height_std = $stdsize['height'];

            // Get default width and height, taking the aspect ratio into account.
            if (round($width_std / $aspect_ratio) <= $height_std) {

              // Width is controlling factor.
              $height_std = round($width_std / $aspect_ratio);
            }
            else {

              // Height is controlling factor.
              $width_std = round($height_std * $aspect_ratio);
            }

            // Get the diagonal size of this standard image.
            $diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2));
            $difference = abs($diag_size_new - $diag_size_std);
            if ($difference < $closest_difference) {
              $closest_std_size = $key;
              $closest_difference = $difference;
            }
          }

          // Find out if desired width/height is the same (or extremely close)
          // to the size of a default image derivative; if so, we will use the
          // default size image instead of generating our own image.
          if ($closest_difference < 3) {
            $create_custom = FALSE;
          }
          else {
            $img_assist_create_derivatives = variable_get('img_assist_create_derivatives', array());

            // If all users are allowed to create custom sized images.
            if ($img_assist_create_derivatives['custom_all']) {
              $create_custom = TRUE;
            }
            elseif ($img_assist_create_derivatives['custom_advanced']) {

              // Note: The following line is NOT the right way to do this.
              // The user acount passed to user_access() should be the user who
              // CREATED this node, not the CURRENT user. I'm not sure how to
              // get the user who created the node, because this function
              // doesn't have access to the node object. I could probably figure
              // out some hack, but I think I'm going to completely rethink the
              // 'img_assist_create_derivatives' option. When I started it this
              // method made sense, but the more I've worked on this, the more
              // confusing it gets.
              if (user_access('access advanced options', $user)) {
                $create_custom = TRUE;
              }
              else {
                $create_custom = TRUE;
              }
            }
            else {
              $create_custom = FALSE;
            }
          }
        }
      }
      if ($create_custom) {

        // Try to get this page's nid to add to this custom image's derivative
        // label; otherwise you can't really have the same image inline in more
        // than one node at different custom sizes.
        $page_nid = is_numeric(arg(2)) ? '_' . arg(2) : '';
        $size['label'] = t('Custom');
        $size['key'] = 'img_assist_custom';
        $size['width'] = $width;
        $size['height'] = $height;
      }
      else {
        $size['key'] = $closest_std_size;
      }
    }
    else {
      $size = image_get_sizes();
      $size = $size[IMAGE_THUMBNAIL];
      $size['key'] = IMAGE_THUMBNAIL;
    }
    return theme('img_assist_inline', $node, $size, $attributes);
  }
  elseif ($attributes['fid']) {
    $img = img_assist_load_image($attributes['fid'], FALSE);
    $image = array_shift($img);
    $width = $attributes['width'] ? $attributes['width'] : $image->width;
    $height = $attributes['height'] ? $attributes['height'] : $image->height;
    $src = file_create_url($image->filepath);
    $class = $attributes['class'] ? $attributes['class'] : 'image';
    $img_template = theme('img_assist_legacy');
    $img_template = strtr($img_template, array(
      '%caption' => $attributes['caption'],
      '%node-link' => url('node/' . $image->nid),
      '%nid' => $image->nid,
      '%img-link' => $image->filepath,
      '%alt' => check_plain($attributes['alt']),
      '%width' => $width,
      '%height' => $height,
      '%src' => $src,
      '%image-class' => $class,
    ));
    return $img_template;
  }
}