View source
<?php
function image_get_available_toolkits() {
$toolkits = file_scan_directory('includes', 'image\\..*\\.inc$');
$output = array();
foreach ($toolkits as $file => $toolkit) {
include_once $file;
$function = str_replace('.', '_', $toolkit->name) . '_info';
if (function_exists($function)) {
$info = $function();
$output[$info['name']] = $info['title'];
}
}
$output['gd'] = t('Built-in GD2 toolkit');
return $output;
}
function image_get_toolkit() {
static $toolkit;
if (!$toolkit) {
$toolkit = variable_get('image_toolkit', 'gd');
$toolkit_file = 'includes/image.' . $toolkit . '.inc';
if ($toolkit != 'gd' && file_exists($toolkit_file)) {
include_once $toolkit_file;
}
elseif (!image_gd_check_settings()) {
$toolkit = false;
}
}
return $toolkit;
}
function image_toolkit_invoke($method, $params = array()) {
if ($toolkit = image_get_toolkit()) {
$function = 'image_' . $toolkit . '_' . $method;
if (function_exists($function)) {
return call_user_func_array($function, $params);
}
else {
watchdog('php', t("The selected image handling toolkit '%toolkit' can not correctly process '%function'.", array(
'%toolkit' => "<em>{$toolkit}</em>",
'%function' => "<em>{$function}</em>",
)), WATCHDOG_ERROR);
return false;
}
}
else {
if ($method == 'settings') {
return image_gd_settings();
}
}
}
function image_get_info($file) {
if (!is_file($file)) {
return false;
}
$details = false;
$data = @getimagesize($file);
if (is_array($data)) {
$extensions = array(
'1' => 'gif',
'2' => 'jpg',
'3' => 'png',
);
$extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
$details = array(
'width' => $data[0],
'height' => $data[1],
'extension' => $extension,
'mime_type' => $data['mime'],
);
}
return $details;
}
function image_scale($source, $destination, $width, $height) {
$info = image_get_info($source);
if ($width > $info['width'] && $height > $info['height']) {
return false;
}
$aspect = $info['height'] / $info['width'];
if ($aspect < $height / $width) {
$width = (int) min($width, $info['width']);
$height = (int) round($width * $aspect);
}
else {
$height = (int) min($height, $info['height']);
$width = (int) round($height / $aspect);
}
return image_toolkit_invoke('resize', array(
$source,
$destination,
$width,
$height,
));
}
function image_resize($source, $destination, $width, $height) {
return image_toolkit_invoke('resize', array(
$source,
$destination,
$width,
$height,
));
}
function image_rotate($source, $destination, $degrees) {
return image_toolkit_invoke('rotate', array(
$source,
$destination,
$degrees,
));
}
function image_crop($source, $destination, $x, $y, $width, $height) {
return image_toolkit_invoke('crop', array(
$source,
$destination,
$x,
$y,
$width,
$height,
));
}
function image_gd_settings() {
if (image_gd_check_settings()) {
return t('The built-in GD2 toolkit is installed and working properly.');
}
else {
form_set_error('image_toolkit', t("The built-in GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see %url.", array(
'%url' => '<a href="http://php.net/image">http://php.net/image</a>',
)));
return false;
}
}
function image_gd_check_settings() {
if ($check = get_extension_funcs('gd')) {
if (in_array('imagegd2', $check)) {
return true;
}
}
return false;
}
function image_gd_resize($source, $destination, $width, $height) {
if (!file_exists($source)) {
return false;
}
$info = image_get_info($source);
if (!$info) {
return false;
}
$im = image_gd_open($source, $info['extension']);
if (!$im) {
return false;
}
$res = imageCreateTrueColor($width, $height);
imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
$result = image_gd_close($res, $destination, $info['extension']);
imageDestroy($res);
imageDestroy($im);
return $result;
}
function image_gd_rotate($source, $destination, $degrees, $bg_color = 0) {
if (!function_exists('imageRotate')) {
return false;
}
$info = image_get_info($source);
if (!$info) {
return false;
}
$im = image_gd_open($source, $info['extension']);
if (!$im) {
return false;
}
$res = imageRotate($im, $degrees, $bg_color);
$result = image_gd_close($res, $destination, $info['extension']);
return $result;
}
function image_gd_crop($source, $destination, $x, $y, $width, $height) {
$info = image_get_info($source);
if (!$info) {
return false;
}
$im = image_gd_open($source, $info['extension']);
$res = imageCreateTrueColor($width, $height);
imageCopy($res, $im, 0, 0, $x, $y, $width, $height);
$result = image_gd_close($res, $destination, $info['extension']);
imageDestroy($res);
imageDestroy($im);
return $result;
}
function image_gd_open($file, $extension) {
$extension = str_replace('jpg', 'jpeg', $extension);
$open_func = 'imageCreateFrom' . $extension;
if (!function_exists($open_func)) {
return false;
}
return $open_func($file);
}
function image_gd_close($res, $destination, $extension) {
$extension = str_replace('jpg', 'jpeg', $extension);
$close_func = 'image' . $extension;
if (!function_exists($close_func)) {
return false;
}
return $close_func($res, $destination);
}