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);
if (module_exists('views')) {
include drupal_get_path('module', 'image') . '/views.inc';
}
function image_help($section) {
switch ($section) {
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 thumbnail sizes, thumbnail and preview. The thumbnail size is shown as the preview for image posts and when browsing image galleries. The preview is the default size when first displaying an image node.') . '</p>';
$output .= '<p>' . t('Image administration allows the image directory and the image sizes to be set.</p><p>
Image galleries are 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.') . '</p>';
$output .= t('<p>You can</p>
<ul>
<li>view image handling messages in <a href="!admin-settings">administer >> settings</a>.</li>
<li>configure image sizes and file directories at <a href="!admin-settings-image">administer >> settings >> image</a>.</li>
<li>use the <a href="!external-http-drupal-org-project-img_assist">image assist module</a> to upload and insert images into posts.</li>
', array(
'!admin-settings' => url('admin/settings'),
'!admin-image-galleries' => url('admin/image/galleries'),
'!admin-settings-image' => url('admin/settings/image'),
'!external-http-drupal-org-project-img_assist' => 'http://drupal.org/project/img_assist',
)) . '</ul>';
$output .= '<p>' . t('For more information please read the configuration and customization handbook <a href="!image">Image page</a>.', array(
'!image' => 'http://www.drupal.org/handbook/modules/image/',
)) . '</p>';
return $output;
}
}
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(
'create images',
'view original images',
'edit own images',
'edit images',
);
}
function image_access($op, $node) {
global $user;
if ($op == 'create' && user_access('create images')) {
return TRUE;
}
if ($op == 'update' || $op == 'delete') {
if (user_access('edit images')) {
return TRUE;
}
if (user_access('edit own images') && $user->uid == $node->uid) {
return TRUE;
}
}
}
function image_admin_settings() {
_image_check_settings();
$form['#submit'] = array(
'image_settings_sizes_submit' => array(),
);
$form['image_updated'] = array(
'#type' => 'hidden',
'#value' => variable_get('image_updated', time()),
);
$form['paths'] = array(
'#type' => 'fieldset',
'#title' => t('File paths'),
);
$form['paths']['image_default_path'] = array(
'#type' => 'textfield',
'#title' => t('Default image path'),
'#default_value' => variable_get('image_default_path', 'images'),
'#description' => t('Subdirectory in the directory "%dir" where pictures will be stored. Do not include trailing slash.', array(
'%dir' => variable_get('file_directory_path', 'files'),
)),
);
$form['image_max_upload_size'] = array(
'#type' => 'textfield',
'#title' => t('Maximum upload size'),
'#default_value' => variable_get('image_max_upload_size', 800),
'#field_suffix' => t('KB'),
'#size' => 12,
'#description' => t('Maximum file size for image uploads. When a maximum image dimensions is specified for original images the size is checked after resizing. '),
);
$form['image_sizes'] = array(
'#type' => 'fieldset',
'#title' => t('Image sizes'),
'#tree' => TRUE,
'#theme' => 'image_settings_sizes_form',
'#description' => t('The dimensions on the "Original" image will be used to resize the images when they are first uploaded. If both dimensions are specified the image will be resized to fit with in that size. If only one dimension is specified the image will be resized to exactly that dimension with the other determined by the image aspect ratio.'),
'#validate' => array(
'image_settings_sizes_validate' => array(),
),
);
$link_options = array(
IMAGE_LINK_HIDDEN => t('Hidden'),
IMAGE_LINK_SHOWN => t('Same window'),
IMAGE_LINK_NEW => t('New window'),
);
$sizes = image_get_sizes();
for ($i = count($sizes); $i < 6; $i++) {
$sizes['new' . $i] = array(
'label' => '',
'width' => '',
'height' => '',
'link' => IMAGE_LINK_SHOWN,
'new' => TRUE,
);
}
foreach ($sizes as $key => $size) {
$form['image_sizes'][$key]['label'] = array(
'#type' => 'textfield',
'#default_value' => $size['label'],
'#size' => 25,
'#maxlength' => 32,
);
if (_image_is_required_size($key)) {
$form['image_sizes'][$key]['label']['#attributes'] = array(
'disabled' => 'disabled',
);
$form['image_sizes'][$key]['label']['#required'] = TRUE;
}
$form['image_sizes'][$key]['width'] = array(
'#type' => 'textfield',
'#default_value' => $size['width'],
'#size' => 5,
'#maxlength' => 5,
);
$form['image_sizes'][$key]['height'] = array(
'#type' => 'textfield',
'#default_value' => $size['height'],
'#size' => 5,
'#maxlength' => 5,
);
$form['image_sizes'][$key]['link'] = array(
'#type' => 'select',
'#default_value' => $size['link'],
'#options' => $link_options,
);
}
return system_settings_form($form);
}
function image_settings_sizes_validate(&$form) {
foreach (element_children($form) as $key) {
if ($key != IMAGE_ORIGINAL && !empty($form[$key]['label']['#value'])) {
if (empty($form[$key]['width']['#value']) && empty($form[$key]['height']['#value'])) {
form_set_error("image_sizes][{$key}][width", t('Must specify a width or height.'));
}
}
}
}
function image_settings_sizes_submit($form_id, &$form_values) {
$old_sizes = image_get_sizes();
$rebuild = FALSE;
foreach ($form_values['image_sizes'] as $key => $size) {
if ($key == IMAGE_ORIGINAL) {
continue;
}
if (empty($size['label'])) {
unset($form_values['image_sizes'][$key]);
}
if (isset($form_values['image_sizes'][$key]) ^ isset($old_sizes[$key])) {
$rebuild |= TRUE;
if (isset($form_values['image_sizes'][$key])) {
unset($form_values['image_sizes'][$key]);
$new_key = drupal_strtolower(drupal_substr($size['label'], 0, 32));
$form_values['image_sizes'][$new_key] = $size;
}
}
else {
if (isset($form_values['image_sizes'][$key]) && isset($old_sizes[$key])) {
foreach (array(
'height',
'width',
) as $field) {
$rebuild |= $form_values['image_sizes'][$key][$field] != $old_sizes[$key][$field];
}
}
}
}
if ($rebuild) {
drupal_set_message(t('Changes to the images sizes mean that the derivative images will need to be regenerated.'));
$form_values['image_updated'] = time();
}
return system_settings_form_submit($form_id, $form_values);
}
function theme_image_settings_sizes_form(&$form) {
$header = array(
t('Label'),
t('Width'),
t('Height'),
t('Link'),
);
foreach (element_children($form) as $key) {
$row = array();
$row[] = drupal_render($form[$key]['label']);
$row[] = drupal_render($form[$key]['width']);
$row[] = drupal_render($form[$key]['height']);
$row[] = drupal_render($form[$key]['link']);
$rows[] = $row;
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
function image_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'node/add/image',
'title' => t('Image'),
'access' => user_access('create images'),
);
$items[] = array(
'path' => 'image/view',
'title' => t('image'),
'access' => user_access('access content'),
'type' => MENU_CALLBACK,
'callback' => 'image_fetch',
);
$items[] = array(
'path' => 'admin/settings/image',
'title' => t('Image'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'image_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Image module settings.'),
);
}
return $items;
}
function image_cron() {
$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 image thumbnails'),
'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;
drupal_set_message(t("Rebuilding %node-title's resized images.", array(
'%node-title' => $node->title,
)));
image_update($node);
}
}
}
}
function image_prepare(&$node, $field_name) {
if (is_null($field_name)) {
$field_name = 'image';
}
if ($file = file_check_upload($field_name)) {
$image_info = image_get_info($file->filepath);
if (!$image_info || empty($image_info['extension'])) {
form_set_error($field_name, t('Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.'));
return;
}
$file = file_save_upload($field_name, _image_filename($file->filename, IMAGE_ORIGINAL, TRUE));
if (!$file) {
return;
}
$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($field_name, 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;
}
$node->images[IMAGE_ORIGINAL] = $file->filepath;
$node->rebuild_images = FALSE;
$node->new_file = TRUE;
module_invoke_all('image_alter', $node, $file->filepath, IMAGE_ORIGINAL);
_image_build_derivatives($node, TRUE);
}
}
function image_file_download($filename) {
$filepath = file_create_path($filename);
$result = db_query("SELECT f.nid, f.filename, f.filesize, f.filemime FROM {files} f WHERE f.filepath = '%s'", $filepath);
if ($file = db_fetch_object($result)) {
$node = node_load(array(
'type' => 'image',
'nid' => $file->nid,
));
if ($node) {
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 = $_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' => $size['label'],
'href' => "image/view/{$node->nid}/{$key}",
'attributes' => array(
'target' => '_blank',
),
);
}
else {
$links['image_size_' . $key] = array(
'title' => $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) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Latest image');
$block[1]['info'] = t('Random image');
return $block;
case 'view':
if (user_access('access content')) {
switch ($delta) {
case 0:
$images = image_get_latest();
$block['subject'] = t('Latest image');
$block['content'] = l(image_display($images[0], IMAGE_THUMBNAIL), 'node/' . $images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
break;
case 1:
$images = image_get_random();
$block['subject'] = t('Random image');
$block['content'] = l(image_display($images[0], IMAGE_THUMBNAIL), 'node/' . $images[0]->nid, array(), NULL, NULL, FALSE, TRUE);
break;
}
}
return $block;
}
}
function image_form_add_thumbnail($form_id, $edit) {
if ($edit['images']['thumbnail']) {
$node = (object) $edit;
$form = array(
'#type' => 'item',
'#title' => t('Thumbnail'),
'#value' => image_display($node, IMAGE_THUMBNAIL),
'#weight' => -10,
);
}
return $form;
}
function image_form(&$node, &$param) {
_image_check_settings();
$type = node_get_types('type', $node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#default_value' => $node->title,
);
if ($node->new_file) {
$form['new_file'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
$form['images']['#tree'] = TRUE;
foreach (image_get_sizes() as $key => $size) {
if ($node->new_file) {
$form['images'][$key] = array(
'#type' => 'hidden',
'#value' => $node->images[$key],
);
}
else {
$form['images'][$key] = array(
'#type' => 'hidden',
'#default_value' => $node->images[$key],
);
}
}
$form['thumbnail']['#after_build'][] = 'image_form_add_thumbnail';
$form['#attributes'] = array(
"enctype" => "multipart/form-data",
);
$form['image'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#size' => 40,
'#description' => t('Click "Browse..." to select an image to upload.'),
'#weight' => -3,
);
if ($type->has_body) {
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#required' => $type->min_word_count > 0,
'#default_value' => $node->body,
);
$form['body_filter']['format'] = filter_form($node->format);
}
return $form;
}
function image_validate($node) {
if (empty($node->images[IMAGE_ORIGINAL])) {
form_set_error('image', t('You must upload an image.'));
}
}
function image_submit(&$node) {
if ($node->new_file) {
$node->rebuild_images = FALSE;
}
}
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 filename, filepath FROM {files} WHERE nid=%d", $node->nid);
$node->images = array();
while ($file = db_fetch_object($result)) {
$node->images[$file->filename] = file_create_path($file->filepath);
}
$original_path = $node->images[IMAGE_ORIGINAL];
if (empty($original_path)) {
return;
}
$node->rebuild_images = FALSE;
$all_sizes = image_get_sizes();
unset($all_sizes[IMAGE_ORIGINAL]);
$needed_sizes = array_keys(image_get_derivative_sizes($original_path));
$unneeded_sizes = array_diff(array_keys($all_sizes), $needed_sizes);
foreach ($unneeded_sizes as $key) {
if (empty($node->images[$key])) {
$node->images[$key] = $original_path;
}
else {
$node->rebuild_images = TRUE;
}
}
foreach ($needed_sizes as $key) {
if (empty($node->images[$key]) || !file_exists($node->images[$key])) {
$node->rebuild_images = TRUE;
}
else {
if (filemtime($node->images[$key]) < variable_get('image_updated', 0)) {
$node->rebuild_images = TRUE;
}
}
}
if ($node->rebuild_images) {
image_update($node);
watchdog('image', t('Derivative images were regenerated for %title.', array(
'%title' => $node->title,
)), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
}
}
function image_insert($node) {
$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 ($node->new_file || $node->rebuild_images) {
$original_path = $node->images[IMAGE_ORIGINAL];
if ($node->new_file) {
$result = db_query("SELECT f.fid, f.filepath FROM {files} f WHERE nid=%d", $node->nid);
while ($file = db_fetch_object($result)) {
file_delete(file_create_path($file->filepath));
}
db_query("DELETE FROM {files} WHERE nid = %d", $node->nid);
db_query("DELETE FROM {file_revisions} WHERE vid = %d", $node->vid);
_image_insert($node, IMAGE_ORIGINAL, $original_path);
}
else {
if ($node->rebuild_images) {
$original_file = db_fetch_object(db_query("SELECT * FROM {files} WHERE nid=%d AND filename = '%s'", $node->nid, IMAGE_ORIGINAL));
$result = db_query("SELECT * FROM {files} WHERE nid=%d AND fid <> %d", $node->nid, $original_file->fid);
while ($file = db_fetch_object($result)) {
if ($file->filepath != $original_file->filepath) {
file_delete(file_create_path($file->filepath));
}
}
db_query("DELETE FROM {files} WHERE nid = %d AND fid <> %d", $node->nid, $original_file->fid);
db_query("DELETE FROM {file_revisions} WHERE vid = %d AND fid <> %d", $node->vid, $original_file->fid);
_image_build_derivatives($node, FALSE);
if (image_access('update', $node)) {
drupal_set_message(t('The derivative images for <a href="!link">%title</a> have been regenerated.', array(
'!link' => url('node/' . $node->nid),
'%title' => $node->title,
)));
}
$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 fid, filepath FROM {files} WHERE nid = %d', $node->nid);
while ($file = db_fetch_object($result)) {
file_delete(file_create_path($file->filepath));
db_query("DELETE FROM {file_revisions} WHERE fid = %d", $file->fid);
}
db_query('DELETE FROM {files} WHERE nid = %d', $node->nid);
}
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'] : "");
$attributes['width'] = $image_info['width'];
$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];
$headers = image_file_download($file);
file_transfer($file, $headers);
}
}
}
return drupal_not_found();
}
function theme_image_teaser($node) {
return l(image_display($node, IMAGE_THUMBNAIL), 'node/' . $node->nid, array(), NULL, NULL, TRUE, TRUE);
}
function theme_image_body($node, $size) {
return image_display($node, $size);
}
function theme_image_display($node, $label, $url, $attributes) {
return theme('image', $url, $node->title, $node->title, $attributes, FALSE);
}
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 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(variable_get('image_default_path', 'images'));
$temp_path = rtrim($image_path, '/') . '/temp';
if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY, 'image_default_path')) {
return false;
}
if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY, '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) {
if (!_image_check_settings()) {
return 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));
foreach ($unneeded_sizes as $key) {
$node->images[$key] = $original_path;
}
foreach ($needed_sizes as $key => $size) {
$destination = _image_filename($original_path, $key, $temp);
if (!image_scale($original_path, $destination, $size['width'], $size['height'])) {
drupal_set_message(t('Unable to create scaled %label image', array(
'%label' => $size['label'],
)), 'error');
return FALSE;
}
@chmod($destination, 0664);
$node->images[$key] = $destination;
module_invoke_all('image_alter', $node, $destination, $key);
}
}
function _image_filename($filename, $label = IMAGE_ORIGINAL, $temp = FALSE) {
$path = variable_get('image_default_path', 'images') . '/';
if ($temp) {
$path .= 'temp/';
}
$filename = basename($filename);
if ($label && $label != IMAGE_ORIGINAL) {
$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'),
'link' => IMAGE_LINK_SHOWN,
),
IMAGE_THUMBNAIL => array(
'width' => 100,
'height' => 100,
'label' => t('Thumbnail'),
'link' => IMAGE_LINK_SHOWN,
),
IMAGE_PREVIEW => array(
'width' => 640,
'height' => 640,
'label' => t('Preview'),
'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);
$fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $fid, $node->nid, $size, $image_path, $image_info['mime_type'], $image_info['file_size']);
db_query("INSERT INTO {file_revisions} (fid, vid, description, list) VALUES (%d, %d, '%s', %d)", $fid, $node->vid, '', 0);
}
}
function image_create_node_from($filepath, $title = NULL, $body = '', $taxonomy = NULL) {
global $user;
if (!user_access('create images')) {
drupal_access_denied();
}
if (!($image_info = image_get_info($filepath))) {
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);
file_delete($original_path);
return $node;
}