<?php
function image_get_available_toolkits() {
$toolkits = module_invoke_all('image_toolkits');
$output = array();
foreach ($toolkits as $name => $info) {
if ($info['available']) {
$output[$name] = $info['title'];
}
}
return $output;
}
function image_get_toolkit() {
static $toolkit;
if (!isset($toolkit)) {
$toolkits = image_get_available_toolkits();
$toolkit = variable_get('image_toolkit', 'gd');
if (!isset($toolkits[$toolkit]) || !function_exists('image_' . $toolkit . '_load')) {
reset($toolkits);
$toolkit = key($toolkits);
}
}
return $toolkit;
}
function image_toolkit_invoke($method, stdClass $image, array $params = array()) {
$function = 'image_' . $image->toolkit . '_' . $method;
if (function_exists($function)) {
array_unshift($params, $image);
return call_user_func_array($function, $params);
}
watchdog('image', 'The selected image handling toolkit %toolkit can not correctly process %function.', array(
'%toolkit' => $image->toolkit,
'%function' => $function,
), WATCHDOG_ERROR);
return FALSE;
}
function image_get_info($filepath, $toolkit = FALSE) {
$details = FALSE;
if (!is_file($filepath) && !is_uploaded_file($filepath)) {
return $details;
}
if (!$toolkit) {
$toolkit = image_get_toolkit();
}
if ($toolkit) {
$image = new stdClass();
$image->source = $filepath;
$image->toolkit = $toolkit;
$details = image_toolkit_invoke('get_info', $image);
if (isset($details) && is_array($details)) {
$details['file_size'] = filesize($filepath);
}
}
return $details;
}
function image_scale_and_crop(stdClass $image, $width, $height) {
$scale = max($width / $image->info['width'], $height / $image->info['height']);
$x = ($image->info['width'] * $scale - $width) / 2;
$y = ($image->info['height'] * $scale - $height) / 2;
if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
return image_crop($image, $x, $y, $width, $height);
}
return FALSE;
}
function image_dimensions_scale(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) {
$aspect = $dimensions['height'] / $dimensions['width'];
if ($width && !$height || $width && $height && $aspect < $height / $width) {
$height = (int) round($width * $aspect);
}
else {
$width = (int) round($height / $aspect);
}
if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) {
return FALSE;
}
$dimensions['width'] = $width;
$dimensions['height'] = $height;
return TRUE;
}
function image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) {
$dimensions = $image->info;
if (!image_dimensions_scale($dimensions, $width, $height, $upscale)) {
return TRUE;
}
return image_resize($image, $dimensions['width'], $dimensions['height']);
}
function image_resize(stdClass $image, $width, $height) {
$width = (int) round($width);
$height = (int) round($height);
return image_toolkit_invoke('resize', $image, array(
$width,
$height,
));
}
function image_rotate(stdClass $image, $degrees, $background = NULL) {
return image_toolkit_invoke('rotate', $image, array(
$degrees,
$background,
));
}
function image_crop(stdClass $image, $x, $y, $width, $height) {
$aspect = $image->info['height'] / $image->info['width'];
if (empty($height)) {
$height = $width / $aspect;
}
if (empty($width)) {
$width = $height * $aspect;
}
$width = (int) round($width);
$height = (int) round($height);
return image_toolkit_invoke('crop', $image, array(
$x,
$y,
$width,
$height,
));
}
function image_desaturate(stdClass $image) {
return image_toolkit_invoke('desaturate', $image);
}
function image_load($file, $toolkit = FALSE) {
if (!$toolkit) {
$toolkit = image_get_toolkit();
}
if ($toolkit) {
$image = new stdClass();
$image->source = $file;
$image->info = image_get_info($file, $toolkit);
if (isset($image->info) && is_array($image->info)) {
$image->toolkit = $toolkit;
if (image_toolkit_invoke('load', $image)) {
return $image;
}
}
}
return FALSE;
}
function image_save(stdClass $image, $destination = NULL) {
if (empty($destination)) {
$destination = $image->source;
}
if ($return = image_toolkit_invoke('save', $image, array(
$destination,
))) {
clearstatcache();
$image->info = image_get_info($destination, $image->toolkit);
if (drupal_chmod($destination)) {
return $return;
}
}
return FALSE;
}