View source
<?php
define('MAX_IMAGE_SIZE_DEFAULT_WIDTH', 2560);
define('MAX_IMAGE_SIZE_DEFAULT_HEIGHT', 1600);
function max_image_size_file_presave($file) {
if (!variable_get('max_image_size_presave_enabled', TRUE)) {
return;
}
if ($image = max_image_size_resize_file($file)) {
$file->filesize = $image->info['file_size'];
if (module_exists('file_entity')) {
$file->metadata['width'] = $image->info['width'];
$file->metadata['height'] = $image->info['height'];
}
}
}
function max_image_size_cron() {
if (date('ymd', variable_get('cron_last')) == date('ymd')) {
return;
}
if (variable_get('max_image_size_cron_enabled', TRUE)) {
$count = max_image_size_queue_unprocessed_images();
watchdog('max_image_size', 'Added @count items to the process table.', array(
'@count' => $count,
), WATCHDOG_INFO);
}
}
function max_image_size_cron_queue_info() {
$queues = array();
$queues['max_image_size'] = array(
'worker callback' => 'max_image_size_resize_callback',
'time' => 30,
);
return $queues;
}
function max_image_size_help($path, $arg) {
$message = NULL;
switch ($path) {
case 'admin/config/media/max-image-size':
$message = '<p>' . t('Max Image Size will resize uploaded images to be below the following dimensions: @widthx@height. Once an image has been processed it is non-reversible, so please be sure that you set the correct dimensions.', array(
'@width' => variable_get('max_image_size_width', MAX_IMAGE_SIZE_DEFAULT_WIDTH),
'@height' => variable_get('max_image_size_height', MAX_IMAGE_SIZE_DEFAULT_HEIGHT),
)) . '</p>';
break;
case 'admin/help#max_image_size':
$message = '<p>' . t('Max Image Size will resize uploaded images to be below the following dimensions: @widthx@height. To adjust these settings visit <a href="/admin/config/media/max-image-size">the configuration page</a> and set the width and height to the desired values.', array(
'@width' => variable_get('max_image_size_width', MAX_IMAGE_SIZE_DEFAULT_WIDTH),
'@height' => variable_get('max_image_size_height', MAX_IMAGE_SIZE_DEFAULT_HEIGHT),
)) . '</p>';
break;
}
return $message;
}
function max_image_size_resize_callback($fid) {
$file = file_load($fid);
if (max_image_size_resize_file($file)) {
file_save($file);
}
}
function max_image_size_menu() {
$items = array();
$items['admin/config/media/max-image-size'] = array(
'title' => 'Max Image Size',
'description' => 'Configure the behavior of the Max Image Size module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'max_image_size_settings_form',
),
'access arguments' => array(
'administer max_image_size',
),
'file' => 'max_image_size.admin.inc',
);
return $items;
}
function max_image_size_permission() {
return array(
'administer max_image_size' => array(
'title' => t('Administer max image size'),
'description' => t('Allows administration of the max image size module.'),
),
);
}
function max_image_size_get_unprocessed_images() {
max_image_size_discover_images();
$dimensions = max_image_size_get_max_dimensions();
$fids = db_select('max_image_size', 's')
->fields('s', array(
'fid',
))
->condition(db_or()
->condition('s.width', $dimensions['width'], '>')
->condition('s.height', $dimensions['height'], '>'))
->addTag('max_image_size')
->addTag('unprocessed_images')
->execute()
->fetchCol();
return $fids;
}
function max_image_size_get_max_dimensions() {
$width = intval(variable_get('max_image_size_width', MAX_IMAGE_SIZE_DEFAULT_WIDTH));
$height = intval(variable_get('max_image_size_height', MAX_IMAGE_SIZE_DEFAULT_HEIGHT));
if ($width <= 0) {
drupal_set_message(t('Max image width is set to an invalid size (must be greater than zero).'), 'warn');
$width = MAX_IMAGE_SIZE_DEFAULT_WIDTH;
}
if ($height <= 0) {
drupal_set_message(t('Max image height is set to an invalid size (must be greater than zero).'), 'warn');
$height = MAX_IMAGE_SIZE_DEFAULT_HEIGHT;
}
return array(
'width' => $width,
'height' => $height,
);
}
function max_image_size_resize_file($file) {
if (empty($file->fid) || empty($file->type) || 'image' != $file->type) {
return FALSE;
}
$dimensions = max_image_size_get_max_dimensions();
$is_bad_size = empty($dimensions['width']) || $dimensions['width'] <= 0;
$is_bad_size |= empty($dimensions['height']) || $dimensions['height'] <= 0;
if ($is_bad_size) {
watchdog('max_image_size', 'Invalid image dimensions specified: @widthx@height', array(
'@width' => $dimensions['width'],
'@height' => $dimensions['height'],
), WATCHDOG_NOTICE);
return FALSE;
}
$image = image_load($file->uri);
if (empty($image)) {
watchdog('max_image_size', 'Unable to load image @uri (@fid) for resizing.', array(
'@fid' => $file->fid,
'@uri' => $file->uri,
), WATCHDOG_NOTICE);
return FALSE;
}
$original = clone $image;
if (!image_scale($image, $dimensions['width'], $dimensions['height'])) {
watchdog('max_image_size', 'Failed to scale image @uri (@fid).', array(
'@fid' => $file->fid,
'@uri' => $file->uri,
), WATCHDOG_NOTICE);
return FALSE;
}
$is_unchanged = $original->info['width'] == $image->info['width'];
$is_unchanged &= $original->info['height'] == $image->info['height'];
if ($is_unchanged) {
return FALSE;
}
if (!image_save($image)) {
watchdog('max_image_size', 'Unable to save image @fid', array(
'@fid' => $file->fid,
), WATCHDOG_NOTICE);
return FALSE;
}
db_update('max_image_size')
->fields(array(
'changed' => REQUEST_TIME,
'width' => $image->info['width'],
'height' => $image->info['height'],
))
->condition('fid', $file->fid)
->execute();
module_invoke_all('max_image_size_post_resize', array(
'file' => $file,
'image' => $image,
'original' => $original,
'max_dimensions' => $dimensions,
));
return $image;
}
function max_image_size_discover_images() {
$fids = max_image_size_get_unknown_fids();
if (empty($fids)) {
return false;
}
$inserted_count = 0;
$base_query = db_insert('max_image_size', array(
'return' => Database::RETURN_AFFECTED,
))
->fields(array(
'fid',
'created',
'width',
'height',
));
$insert_chunks = array_chunk($fids, 500);
foreach ($insert_chunks as $chunk) {
$query = clone $base_query;
foreach ($chunk as $fid) {
$file = file_load($fid);
if (empty($file)) {
watchdog('max_image_size', 'Unable to load file (@fid) for insertion.', array(
'@fid' => $fid,
), WATCHDOG_NOTICE);
continue;
}
$image = image_load($file->uri);
if (empty($image)) {
watchdog('max_image_size', 'Unable to load image (@fid) for insertion.', array(
'@fid' => $fid,
), WATCHDOG_NOTICE);
continue;
}
watchdog('max_image_size', 'Adding image @uri (@fid) with @widthx@height.', array(
'@uri' => $file->uri,
'@fid' => $file->fid,
'@width' => $image->info['width'],
'@height' => $image->info['height'],
), WATCHDOG_DEBUG);
$query
->values(array(
'fid' => $fid,
'created' => REQUEST_TIME,
'width' => $image->info['width'],
'height' => $image->info['height'],
));
$inserted_count += $query
->execute();
}
}
return $inserted_count;
}
function max_image_size_get_unknown_fids() {
$subquery = db_select('max_image_size', 's')
->fields('s', array(
'fid',
))
->distinct();
$query = db_select('file_managed', 'm')
->fields('m', array(
'fid',
))
->condition('m.status', FILE_STATUS_PERMANENT)
->condition('m.type', 'image')
->condition('m.fid', $subquery, 'NOT IN');
$filesize_threshold = variable_get('max_image_size_filesize_threshold', FALSE);
if ($filesize_threshold) {
$query
->condition('filesize', $filesize_threshold, '>=');
}
$fids = $query
->execute()
->fetchCol();
return $fids;
}
function max_image_size_queue_unprocessed_images() {
$queue = DrupalQueue::get('max_image_size');
$queue
->deleteQueue();
$queue
->createQueue();
$fids = max_image_size_get_unprocessed_images();
if (!empty($fids)) {
foreach ($fids as $fid) {
$queue
->createItem($fid);
}
watchdog('max_image_size', 'Added @count items to process queue.', array(
'@count' => count($fids),
), WATCHDOG_NOTICE);
}
return count($fids);
}
function max_image_size_file_delete($file) {
db_delete('max_image_size')
->condition('fid', $file->fid)
->execute();
}