View source
<?php
define('IMAGE_ORIGINAL', '_original');
define('IMAGE_PREVIEW', 'preview');
define('IMAGE_THUMBNAIL', 'thumbnail');
define('IMAGE_LINK_HIDDEN', 0);
define('IMAGE_LINK_SHOWN', 1);
define('IMAGE_LINK_NEW', 2);
function image_help($path, $arg) {
switch ($path) {
case 'admin/help#image':
$output = '<p>' . t('The Image module is used to create and administer images for your site. Each image is stored as a post, with thumbnails of the original generated automatically. There are two default derivative image sizes, "thumbnail" and "preview". The "thumbnail" size is shown as preview image in posts and when browsing image galleries. The "preview" size is the default size when viewing an image node page.') . '</p>';
$output .= '<p>' . t('The settings page for Image module allows the image directory and the image sizes to be configured.') . '</p>';
$output .= '<p>' . t('Image module ships with contributed modules. Their settings can be accessed from the image settings page.') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Image attach is used to add an existing or new image to a node. The selected image will show up in a predefined spot on the selected node.') . '</li>';
$output .= '<li>' . t('Image gallery is used to organize and display images in galleries. The list tab allows users to edit existing image gallery names, descriptions, parents and relative position, known as a weight. The add galleries tab allows you to create a new image gallery defining name, description, parent and weight. If the <a href="@views-url">Views module</a> is installed, then the Image gallery module settings are mostly replaced by settings of the view.', array(
'@views-url' => 'http://drupal.org/project/views',
)) . '</li>';
$output .= '<li>' . t('Image import is used to import batches of images. The administration page lets you define the folder from which images will be imported.') . '</li>';
$output .= '<li>' . t('The separate <a href="@img-assist-url">Image assist module</a> can be installed to upload and insert images into posts.', array(
'@img-assist-url' => 'http://drupal.org/project/img_assist',
)) . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('You can:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('Configure image sizes and file directories at <a href="@image-settings-url">Administer » Site configuration » Image</a>.', array(
'@image-settings-url' => url('admin/settings/image'),
)) . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@image-url">Image module</a>.', array(
'@image-url' => 'http://drupal.org/handbook/modules/image',
)) . '</p>';
return $output;
}
}
function image_theme() {
return array(
'image_settings_sizes_form' => array(
'arguments' => array(
'form' => NULL,
),
),
'image_teaser' => array(
'arguments' => array(
'node' => NULL,
'size' => IMAGE_THUMBNAIL,
),
),
'image_body' => array(
'arguments' => array(
'node' => NULL,
'size' => IMAGE_PREVIEW,
),
),
'image_block_random' => array(
'arguments' => array(
'images' => NULL,
'size' => IMAGE_THUMBNAIL,
),
),
'image_block_latest' => array(
'arguments' => array(
'images' => NULL,
'size' => IMAGE_THUMBNAIL,
),
),
'image_display' => array(
'arguments' => array(
'node' => NULL,
'label' => NULL,
'url' => NULL,
'attributes' => NULL,
),
),
);
}
function image_node_info() {
return array(
'image' => array(
'name' => t('Image'),
'module' => 'image',
'description' => t('An image (with thumbnail). This is ideal for publishing photographs or screenshots.'),
),
);
}
function image_perm() {
return array(
'view original images',
'create images',
'edit own images',
'edit any images',
'delete own images',
'delete any images',
);
}
function image_access($op, $node, $account) {
switch ($op) {
case 'create':
if (user_access('create images', $account)) {
return TRUE;
}
break;
case 'update':
if (user_access('edit any images', $account) || $account->uid == $node->uid && user_access('edit own images', $account)) {
return TRUE;
}
break;
case 'delete':
if (user_access('delete any images', $account) || $account->uid == $node->uid && user_access('delete own images', $account)) {
return TRUE;
}
break;
}
}
function image_menu() {
$items = array();
$items['image/view'] = array(
'title' => 'image',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'page callback' => 'image_fetch',
);
$items['admin/settings/image'] = array(
'title' => 'Images',
'description' => 'Configure the location of image files and image sizes. Also, if enabled, configure image attachments and options for image galleries and image imports.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'image.admin.inc',
);
$items['admin/settings/image/nodes'] = array(
'title' => 'Files and sizes',
'description' => 'Configure the location of image files and image sizes.',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => '-10',
);
return $items;
}
function image_cron() {
$path = file_directory_path() . '/' . variable_get('image_default_path', 'images') . '/temp';
$files = file_scan_directory(file_create_path($path), '.*');
foreach ($files as $file => $info) {
if (time() - filemtime($file) > 60 * 60 * 6) {
file_delete($file);
}
}
}
function image_node_operations() {
$operations = array(
'rebuild_thumbs' => array(
'label' => t('Rebuild derivative images'),
'callback' => 'image_operations_rebuild',
),
);
return $operations;
}
function image_operations_rebuild($nids) {
foreach ($nids as $nid) {
if ($node = node_load($nid)) {
if ($node->type == 'image') {
$node->rebuild_images = TRUE;
image_update($node);
}
}
}
}
function image_file_download($filename) {
$filepath = file_create_path($filename);
$result = db_query("SELECT i.nid, f.filemime, f.filesize FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE f.filepath = '%s'", $filepath);
if ($file = db_fetch_object($result)) {
$node = node_load(array(
'type' => 'image',
'nid' => $file->nid,
));
if (node_access('view', $node)) {
$images = (array) $node->images;
unset($images[IMAGE_ORIGINAL]);
if (user_access('view original images') || in_array($filepath, $images)) {
return array(
'Content-Type: ' . mime_header_encode($file->filemime),
'Content-Length: ' . (int) $file->filesize,
);
}
}
return -1;
}
}
function image_link($type, $node, $main = 0) {
$links = array();
if ($type == 'node' && $node->type == 'image' && !$main) {
$request = isset($_GET['size']) ? $_GET['size'] : IMAGE_PREVIEW;
foreach (image_get_sizes() as $key => $size) {
if ($size['link']) {
if (isset($node->images[$key]) && $node->images[$key] != $node->images[$request]) {
if ($size['link'] == IMAGE_LINK_NEW) {
$links['image_size_' . $key] = array(
'title' => t($size['label']),
'href' => "image/view/{$node->nid}/{$key}",
'attributes' => array(
'target' => '_blank',
),
);
}
else {
$links['image_size_' . $key] = array(
'title' => t($size['label']),
'href' => 'node/' . $node->nid,
'query' => 'size=' . urlencode($key),
);
}
}
}
}
if (!user_access('view original images')) {
unset($links['image_size_' . IMAGE_ORIGINAL]);
}
}
return $links;
}
function image_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Latest image');
$block[1]['info'] = t('Random image');
return $block;
case 'configure':
$form['number_images'] = array(
'#type' => 'select',
'#title' => t('Number of images to display'),
'#options' => drupal_map_assoc(range(1, 99)),
'#default_value' => variable_get("image_block_{$delta}_number_images", 1),
);
return $form;
case 'view':
if (user_access('access content')) {
switch ($delta) {
case 0:
$images = image_get_latest(variable_get("image_block_{$delta}_number_images", 1));
$block['subject'] = t('Latest image');
$block['content'] = theme('image_block_latest', $images, IMAGE_THUMBNAIL);
break;
case 1:
$images = image_get_random(variable_get("image_block_{$delta}_number_images", 1));
$block['subject'] = t('Random image');
$block['content'] = theme('image_block_random', $images, IMAGE_THUMBNAIL);
break;
}
}
return $block;
case 'save':
variable_set("image_block_{$delta}_number_images", $edit['number_images']);
break;
}
}
function image_form_add_thumbnail($form, &$form_state) {
if ($form_state['values']['images'][IMAGE_THUMBNAIL]) {
$node = (object) $form_state['values'];
$form['#title'] = t('Thumbnail');
$form['#value'] = image_display($node, IMAGE_THUMBNAIL);
}
return $form;
}
function image_form(&$node, $form_state) {
_image_check_settings();
if (!$_POST && !empty($_SESSION['image_upload'])) {
unset($_SESSION['image_upload']);
}
$type = node_get_types('type', $node);
$form['#validate'][] = 'image_form_validate';
$form['#submit'][] = 'image_form_submit';
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => $node->title,
);
$form['images']['#tree'] = TRUE;
foreach (image_get_sizes() as $key => $size) {
$form['images'][$key] = array(
'#type' => 'value',
'#value' => isset($node->images[$key]) ? $node->images[$key] : '',
);
}
$form['new_file'] = array(
'#type' => 'value',
'#default_value' => isset($node->new_file) ? $node->new_file : FALSE,
);
$form['#attributes'] = array(
'enctype' => 'multipart/form-data',
);
$form['image'] = array(
'#prefix' => '<div class="image-field-wrapper">',
'#suffix' => '</div>',
);
$form['image']['thumbnail'] = array(
'#type' => 'item',
'#after_build' => array(
'image_form_add_thumbnail',
),
);
$form['image']['image'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#size' => 40,
'#description' => t('Select an image to upload.'),
);
$form['image']['rebuild_images'] = array(
'#type' => 'checkbox',
'#title' => t('Rebuild derivative images'),
'#default_value' => FALSE,
'#description' => t('Check this to rebuild the derivative images for this node.'),
'#access' => !isset($node->nid) ? FALSE : TRUE,
);
if ($type->has_body) {
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
}
return $form;
}
function image_form_validate($form, &$form_state) {
if ($form_state['values']['op'] == t('Delete')) {
return;
}
$validators = array(
'file_validate_is_image' => array(),
);
$temporary_file_path = file_create_path(file_directory_path() . '/' . variable_get('image_default_path', 'images') . '/temp');
if ($file = file_save_upload('image', $validators, $temporary_file_path)) {
$image_info = image_get_info($file->filepath);
$aspect_ratio = $image_info['height'] / $image_info['width'];
$original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
if (!empty($original_size['width']) && !empty($original_size['height'])) {
$result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']);
if ($result) {
clearstatcache();
$file->filesize = filesize($file->filepath);
drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array(
'%width' => $original_size['width'],
'%height' => $original_size['height'],
)));
}
}
if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) {
form_set_error('image', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array(
'%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024),
'%file_size' => format_size($file->filesize),
)));
file_delete($file->filepath);
return;
}
$form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath;
$form_state['values']['rebuild_images'] = FALSE;
$form_state['values']['new_file'] = TRUE;
module_invoke_all('image_alter', $form_state['values'], $form_state['values']['images'][IMAGE_ORIGINAL], IMAGE_ORIGINAL);
$form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE);
$_SESSION['image_upload'] = $form_state['values']['images'];
}
elseif (empty($form_state['values']['images'][IMAGE_ORIGINAL])) {
if (empty($_SESSION['image_upload'])) {
form_set_error('image', t('You must upload an image.'));
}
}
}
function image_form_submit($form, &$form_state) {
if (!empty($_SESSION['image_upload'])) {
$form_state['values']['images'] = $_SESSION['image_upload'];
$form_state['values']['new_file'] = TRUE;
unset($_SESSION['image_upload']);
}
}
function image_view($node, $teaser = 0, $page = 0) {
$sizes = image_get_sizes();
$size = IMAGE_PREVIEW;
if (isset($_GET['size'])) {
if (!isset($sizes[$_GET['size']])) {
drupal_goto("node/{$node->nid}");
}
$size = $_GET['size'];
if ($size == IMAGE_ORIGINAL && !user_access('view original images')) {
drupal_goto("node/{$node->nid}");
}
}
$node = node_prepare($node, $teaser);
$node->content['image'] = array(
'#value' => theme($teaser ? 'image_teaser' : 'image_body', $node, $size),
'#weight' => 0,
);
return $node;
}
function image_load(&$node) {
$result = db_query("SELECT i.image_size, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid);
$node->images = array();
while ($file = db_fetch_object($result)) {
$node->images[$file->image_size] = file_create_path($file->filepath);
}
$original_path = isset($node->images[IMAGE_ORIGINAL]) ? $node->images[IMAGE_ORIGINAL] : NULL;
if (empty($original_path)) {
return;
}
if (arg(0) != 'batch' && strpos($_GET['q'], 'admin/content/node') === FALSE) {
_image_build_derivatives_if_needed($node);
}
}
function _image_build_derivatives_if_needed(&$node) {
$node->rebuild_images = FALSE;
$all_sizes = image_get_sizes();
unset($all_sizes[IMAGE_ORIGINAL]);
$needed_sizes = array_keys(image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]));
$unneeded_sizes = array_diff(array_keys($all_sizes), $needed_sizes);
foreach ($unneeded_sizes as $key) {
if (empty($node->images[$key])) {
$node->images[$key] = $node->images[IMAGE_ORIGINAL];
}
else {
$node->rebuild_images = TRUE;
}
}
foreach ($needed_sizes as $key) {
if (empty($node->images[$key]) || !file_exists($node->images[$key])) {
$node->rebuild_images = TRUE;
}
elseif (filemtime($node->images[$key]) < variable_get('image_updated', 0)) {
$node->rebuild_images = TRUE;
}
}
if ($node->rebuild_images) {
image_update($node);
watchdog('image', 'Derivative images were regenerated for %title.', array(
'%title' => $node->title,
), WATCHDOG_INFO, l(t('view'), 'node/' . $node->nid));
}
}
function image_insert($node) {
if (empty($node->new_file) && !empty($node->translation_source)) {
db_query("INSERT INTO {image} (nid, fid, image_size) SELECT %d, fid, image_size FROM {image} WHERE nid = %d", $node->nid, $node->translation_source->nid);
return;
}
$original_path = $node->images[IMAGE_ORIGINAL];
_image_insert($node, IMAGE_ORIGINAL, $original_path);
$sizes = image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]);
foreach ($sizes as $key => $size_info) {
if (!empty($node->images[$key]) && $node->images[$key] != $original_path) {
_image_insert($node, $key, $node->images[$key]);
}
}
}
function image_update(&$node) {
if (!empty($node->new_file) || !empty($node->rebuild_images)) {
$original_path = $node->images[IMAGE_ORIGINAL];
if (!empty($node->new_file)) {
$result = db_query("SELECT f.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid);
while ($file = db_fetch_object($result)) {
db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid);
_image_file_remove($file);
}
_image_insert($node, IMAGE_ORIGINAL, $original_path);
}
else {
if (!empty($node->rebuild_images)) {
$original_file = db_fetch_object(db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND i.image_size = '%s'", $node->nid, IMAGE_ORIGINAL));
$result = db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND f.fid <> %d", $node->nid, $original_file->fid);
while ($file = db_fetch_object($result)) {
db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid);
if ($file->filepath != $original_file->filepath) {
_image_file_remove($file);
}
}
$node->images = _image_build_derivatives($node, FALSE);
$node->rebuild_images = FALSE;
}
}
$sizes = image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]);
foreach ($sizes as $key => $size_info) {
if (!empty($node->images[$key]) && $node->images[$key] != $original_path) {
_image_insert($node, $key, $node->images[$key]);
}
}
}
}
function image_delete($node) {
$result = db_query('SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d', $node->nid);
while ($file = db_fetch_object($result)) {
db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid);
_image_file_remove($file);
}
}
function image_display($node, $label = IMAGE_PREVIEW, $attributes = array()) {
if (empty($node->images[$label])) {
return;
}
$image_info = image_get_info(file_create_path($node->images[$label]));
$attributes['class'] = "image image-{$label} " . (isset($attributes['class']) ? $attributes['class'] : "");
if (!empty($image_info['width'])) {
$attributes['width'] = $image_info['width'];
}
if (!empty($image_info['height'])) {
$attributes['height'] = $image_info['height'];
}
return theme('image_display', $node, $label, file_create_url($node->images[$label]), $attributes);
}
function image_fetch($nid = 0, $size = IMAGE_PREVIEW) {
if ($size == IMAGE_ORIGINAL && !user_access('view original images')) {
return drupal_access_denied();
}
if (isset($nid)) {
$node = node_load(array(
'type' => 'image',
'nid' => $nid,
));
if ($node) {
if (!node_access('view', $node)) {
return drupal_access_denied();
}
if (isset($node->images[$size])) {
$file = $node->images[$size];
if (file_exists(file_create_path($file))) {
$headers = module_invoke_all('file_download', $file);
if ($headers == -1) {
return drupal_access_denied();
}
file_transfer($file, $headers);
}
}
}
}
return drupal_not_found();
}
function theme_image_teaser($node, $size) {
return l(image_display($node, IMAGE_THUMBNAIL), 'node/' . $node->nid, array(
'html' => TRUE,
));
}
function theme_image_body($node, $size) {
return image_display($node, $size);
}
function theme_image_display($node, $label, $url, $attributes) {
$title = isset($attributes['title']) ? $attributes['title'] : $node->title;
$alt = isset($attributes['alt']) ? $attributes['alt'] : $node->title;
unset($attributes['title']);
unset($attributes['alt']);
return theme('image', $url, $alt, $title, $attributes, FALSE);
}
function theme_image_block_random($images, $size) {
$output = '';
foreach ($images as $image) {
$output .= l(image_display($image, $size), 'node/' . $image->nid, array(
'html' => TRUE,
));
}
return $output;
}
function theme_image_block_latest($images, $size) {
$output = '';
foreach ($images as $image) {
$output .= l(image_display($image, $size), 'node/' . $image->nid, array(
'html' => TRUE,
));
}
return $output;
}
function image_get_random($count = 1, $tid = 0) {
if ($tid != 0) {
$result = db_query_range(db_rewrite_sql("SELECT DISTINCT(n.nid), RAND() AS rand FROM {term_node} tn LEFT JOIN {node} n ON n.nid = tn.nid WHERE n.type='image' AND n.status = 1 AND tn.tid = %d ORDER BY rand"), $tid, 0, $count);
}
else {
$result = db_query_range(db_rewrite_sql("SELECT DISTINCT(n.nid), RAND() AS rand FROM {node} n WHERE n.type = 'image' AND n.status = 1 ORDER BY rand"), 0, $count);
}
$output = array();
while ($nid = db_fetch_object($result)) {
$output[] = node_load(array(
'nid' => $nid->nid,
));
}
return $output;
}
function image_get_latest($count = 1, $tid = 0) {
if ($tid != 0) {
$result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {term_node} tn LEFT JOIN {node} n ON n.nid=tn.nid WHERE n.type='image' AND n.status=1 AND tn.tid=%d ORDER BY n.changed DESC"), $tid, 0, $count);
}
else {
$result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type = 'image' AND n.status = 1 ORDER BY n.changed DESC"), 0, $count);
}
$output = array();
while ($nid = db_fetch_object($result)) {
$output[] = node_load(array(
'nid' => $nid->nid,
));
}
return $output;
}
function _image_check_settings() {
$image_path = file_create_path(file_directory_path() . '/' . variable_get('image_default_path', 'images'));
$temp_path = $image_path . '/temp';
if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'image_default_path')) {
return FALSE;
}
if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'image_default_path')) {
return FALSE;
}
if (!image_get_toolkit()) {
drupal_set_message(t('No image toolkit is currently enabled. Without one the image module will not be able to resize your images. You can select one from the <a href="@link">image toolkit settings page</a>.', array(
'@link' => url('admin/settings/image-toolkit'),
)), 'error');
return FALSE;
}
return TRUE;
}
function image_get_derivative_sizes($image_path) {
$sizes = array();
if (!($image_info = image_get_info($image_path))) {
return $sizes;
}
$all_sizes = image_get_sizes(NULL, $image_info['height'] / $image_info['width']);
foreach ($all_sizes as $key => $size) {
if ($key == IMAGE_ORIGINAL) {
continue;
}
if ($image_info['width'] > $size['width'] || $image_info['height'] > $size['height']) {
$sizes[$key] = $size;
}
}
return $sizes;
}
function _image_build_derivatives($node, $temp = FALSE) {
$original_path = file_create_path($node->images[IMAGE_ORIGINAL]);
$all_sizes = image_get_sizes();
$needed_sizes = image_get_derivative_sizes($original_path);
$unneeded_sizes = array_diff(array_keys($all_sizes), array_keys($needed_sizes));
$images[IMAGE_ORIGINAL] = $original_path;
foreach ($unneeded_sizes as $key) {
$images[$key] = $original_path;
}
$image_info = image_get_info($original_path);
foreach ($needed_sizes as $key => $size) {
$destination = _image_filename($original_path, $key, $temp);
$status = FALSE;
switch ($size['operation']) {
case 'scale':
$status = image_scale($original_path, $destination, $size['width'], $size['height']);
break;
case 'scale_crop':
$status = image_scale_and_crop($original_path, $destination, $size['width'], $size['height']);
break;
}
if (!$status) {
drupal_set_message(t('Unable to create scaled %label image.', array(
'%label' => $size['label'],
)), 'error');
return FALSE;
}
@chmod($destination, 0664);
$images[$key] = $destination;
module_invoke_all('image_alter', $node, $destination, $key);
}
return $images;
}
function _image_filename($filepath, $label = IMAGE_ORIGINAL, $temp = FALSE) {
$path = file_directory_path() . '/' . variable_get('image_default_path', 'images');
if ($temp) {
$path .= '/temp';
}
$original_path = dirname($filepath);
$filename = basename($filepath);
if ($label && $label != IMAGE_ORIGINAL) {
if (!$temp && $original_path != '.') {
$path = $original_path;
}
$pos = strrpos($filename, '.');
if ($pos === FALSE) {
$image_info = image_get_info(file_create_path($path . '/' . $filename));
$filename = $filename . '.' . $label . '.' . $image_info['extension'];
}
else {
$filename = substr($filename, 0, $pos) . '.' . $label . substr($filename, $pos);
}
}
return file_create_path($path . '/' . $filename);
}
function image_get_sizes($size = NULL, $aspect_ratio = NULL) {
$defaults = array(
IMAGE_ORIGINAL => array(
'width' => '',
'height' => '',
'label' => t('Original'),
'operation' => 'scale',
'link' => IMAGE_LINK_SHOWN,
),
IMAGE_THUMBNAIL => array(
'width' => 100,
'height' => 100,
'label' => t('Thumbnail'),
'operation' => 'scale',
'link' => IMAGE_LINK_SHOWN,
),
IMAGE_PREVIEW => array(
'width' => 640,
'height' => 640,
'label' => t('Preview'),
'operation' => 'scale',
'link' => IMAGE_LINK_SHOWN,
),
);
$sizes = array();
foreach (variable_get('image_sizes', $defaults) as $key => $val) {
if (!empty($val['label'])) {
if ($aspect_ratio && (empty($val['width']) || empty($val['height']))) {
if (empty($val['height']) && !empty($val['width'])) {
$val['height'] = (int) round($val['width'] * $aspect_ratio);
}
elseif (empty($val['width']) && !empty($val['height'])) {
$val['width'] = (int) round($val['height'] / $aspect_ratio);
}
}
$sizes[$key] = $val;
}
}
if (isset($size)) {
return isset($sizes[$size]) ? $sizes[$size] : FALSE;
}
return $sizes;
}
function _image_get_sizes($size = NULL, $aspect_ratio = NULL) {
return image_get_sizes($size, $aspect_ratio);
}
function _image_is_required_size($size) {
return in_array($size, array(
IMAGE_THUMBNAIL,
IMAGE_PREVIEW,
IMAGE_ORIGINAL,
));
}
function _image_insert(&$node, $size, $image_path) {
$original_path = $node->images[IMAGE_ORIGINAL];
if (file_move($image_path, _image_filename($original_path, $size))) {
$node->images[$size] = $image_path;
$image_info = image_get_info($image_path);
$file = array(
'uid' => $node->uid,
'filename' => $size,
'filepath' => $image_path,
'filemime' => $image_info['mime_type'],
'filesize' => $image_info['file_size'],
'status' => FILE_STATUS_PERMANENT,
'timestamp' => time(),
);
drupal_write_record('files', $file);
$image = array(
'fid' => $file['fid'],
'nid' => $node->nid,
'image_size' => $size,
);
drupal_write_record('image', $image);
}
}
function _image_file_remove($file) {
if (!db_result(db_query("SELECT COUNT(*) FROM {image} WHERE fid = %d", $file->fid))) {
file_delete(file_create_path($file->filepath));
db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
}
}
function image_create_node_from($filepath, $title = NULL, $body = '', $taxonomy = NULL, $keep_original = FALSE) {
global $user;
if (!user_access('create images')) {
return FALSE;
}
if (!($image_info = image_get_info($filepath))) {
return FALSE;
}
if ($image_info['file_size'] > variable_get('image_max_upload_size', 800) * 1024) {
form_set_error('', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array(
'%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024),
'%file_size' => format_size($image_info['file_size']),
)), 'warning');
return FALSE;
}
$original_path = $filepath;
if (!file_copy($filepath, _image_filename($filepath, IMAGE_ORIGINAL, TRUE))) {
return FALSE;
}
$aspect_ratio = $image_info['height'] / $image_info['width'];
$size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
if (!empty($size['width']) && !empty($size['height'])) {
image_scale($filepath, $filepath, $size['width'], $size['height']);
}
$node = new stdClass();
$node->type = 'image';
$node->uid = $user->uid;
$node->name = $user->name;
$node->title = isset($title) ? $title : basename($filepath);
$node->body = $body;
$node_options = variable_get('node_options_' . $node->type, array(
'status',
'promote',
));
$node->status = in_array('status', $node_options);
$node->promote = in_array('promote', $node_options);
if (module_exists('comment')) {
$node->comment = variable_get("comment_{$node->type}", COMMENT_NODE_READ_WRITE);
}
if (module_exists('taxonomy')) {
$node->taxonomy = $taxonomy;
}
$node->new_file = TRUE;
$node->images[IMAGE_ORIGINAL] = $filepath;
$node = node_submit($node);
node_save($node);
if ($keep_original !== TRUE) {
file_delete($original_path);
}
return $node;
}
function image_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'image') . '/views',
);
}
function image_content_extra_fields($type_name) {
if ($type_name == 'image') {
$extra['image'] = array(
'label' => t('Image'),
'description' => t('Image display.'),
'weight' => 0,
);
return $extra;
}
}