function img_assist_render_image in Image Assist 5.2
Same name and namespace in other branches
- 5.3 img_assist.module \img_assist_render_image()
- 5 img_assist.module \img_assist_render_image()
- 6.2 img_assist.module \img_assist_render_image()
- 6 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 1356 - Image Assist module
Code
function img_assist_render_image($attributes = array()) {
global $user;
if ($attributes['nid']) {
$node = node_load($attributes['nid']);
// Get image size.
$width = (int) $attributes['width'];
$height = (int) $attributes['height'];
$default_sizes = image_get_sizes();
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 controlling factor.
if (!$height || $width && round($width / $aspect_ratio) <= $height) {
$height = round($width / $aspect_ratio);
}
else {
$width = round($height * $aspect_ratio);
}
// Find out whether the given width/height is the same as (or extremely
// close to) a default image derivative size; if so, we will use the
// default size instead of generating a custom image.
$diag_size_new = sqrt(pow($width, 2) + pow($height, 2));
$closest_difference = 9999;
foreach ($default_sizes as $key => $stdsize) {
$width_std = $stdsize['width'];
$height_std = $stdsize['height'];
// Diagonal size calculations require a width or height.
if (!$height_std && !$width_std) {
// For the original image, we can fall back to actual dimensions
// though. In fact, IMAGE_ORIGINAL can either have maximum
// dimensions (so aspect ratio based calculations below will apply)
// or the real dimensions (which must be considered as valid
// "derivative size").
// Note that this annuls the 'use original size' user permission.
if ($key !== IMAGE_ORIGINAL) {
continue;
}
else {
$width_std = $original_size['width'];
$height_std = $original_size['height'];
}
}
// Calculate default width and height based on aspect ratio.
// Width is controlling factor.
if (!$height_std && ($width_std && round($width_std / $aspect_ratio) <= $height_std)) {
$height_std = round($width_std / $aspect_ratio);
}
else {
$width_std = round($height_std * $aspect_ratio);
}
// Calculate diagonal size of this default size.
$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;
}
}
// If, for any reason, no default image derivative size has a width or
// height, fall back to IMAGE_THUMBNAIL.
if (!isset($closest_std_size)) {
$closest_std_size = IMAGE_THUMBNAIL;
}
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) {
$size['label'] = t('Custom');
// Add width and height to make it possible to have multiple custom
// sizes of the same image.
$size['key'] = 'img_assist_custom-' . $width . 'x' . $height;
$size['width'] = $width;
$size['height'] = $height;
}
else {
$size['key'] = $closest_std_size;
}
}
else {
$size = $default_sizes[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;
}
}