00001 <?php
00002
00003
00039 function image_get_available_toolkits() {
00040 $toolkits = file_scan_directory('includes', 'image\..*\.inc$');
00041
00042 $output = array();
00043 foreach ($toolkits as $file => $toolkit) {
00044 include_once "./$file";
00045 $function = str_replace('.', '_', $toolkit->name) . '_info';
00046 if (function_exists($function)) {
00047 $info = $function();
00048 $output[$info['name']] = $info['title'];
00049 }
00050 }
00051
00052 return $output;
00053 }
00054
00061 function image_get_toolkit() {
00062 static $toolkit;
00063
00064 if (!$toolkit) {
00065 $toolkit = variable_get('image_toolkit', 'gd');
00066 $toolkit_file = './includes/image.' . $toolkit . '.inc';
00067 if (isset($toolkit) && file_exists($toolkit_file)) {
00068 include_once $toolkit_file;
00069 }
00070 elseif (!image_gd_check_settings()) {
00071 $toolkit = FALSE;
00072 }
00073 }
00074
00075 return $toolkit;
00076 }
00077
00088 function image_toolkit_invoke($method, $params = array()) {
00089 if ($toolkit = image_get_toolkit()) {
00090 $function = 'image_' . $toolkit . '_' . $method;
00091 if (function_exists($function)) {
00092 return call_user_func_array($function, $params);
00093 }
00094 else {
00095 watchdog('php', 'The selected image handling toolkit %toolkit can not correctly process %function.', array('%toolkit' => $toolkit, '%function' => $function), WATCHDOG_ERROR);
00096 return FALSE;
00097 }
00098 }
00099 }
00100
00101
00116 function image_get_info($file) {
00117 if (!is_file($file)) {
00118 return FALSE;
00119 }
00120
00121 $details = FALSE;
00122 $data = @getimagesize($file);
00123 $file_size = @filesize($file);
00124
00125 if (isset($data) && is_array($data)) {
00126 $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
00127 $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
00128 $details = array('width' => $data[0],
00129 'height' => $data[1],
00130 'extension' => $extension,
00131 'file_size' => $file_size,
00132 'mime_type' => $data['mime']);
00133 }
00134
00135 return $details;
00136 }
00137
00157 function image_scale_and_crop($source, $destination, $width, $height) {
00158 $info = image_get_info($source);
00159
00160 $scale = max($width / $info['width'], $height / $info['height']);
00161 $x = round(($info['width'] * $scale - $width) / 2);
00162 $y = round(($info['height'] * $scale - $height) / 2);
00163
00164 if (image_toolkit_invoke('resize', array($source, $destination, $info['width'] * $scale, $info['height'] * $scale))) {
00165 return image_toolkit_invoke('crop', array($destination, $destination, $x, $y, $width, $height));
00166 }
00167 return FALSE;
00168 }
00169
00187 function image_scale($source, $destination, $width, $height) {
00188 $info = image_get_info($source);
00189
00190
00191 if ($width >= $info['width'] && $height >= $info['height']) {
00192 return FALSE;
00193 }
00194
00195 $aspect = $info['height'] / $info['width'];
00196 if ($aspect < $height / $width) {
00197 $width = (int)min($width, $info['width']);
00198 $height = (int)round($width * $aspect);
00199 }
00200 else {
00201 $height = (int)min($height, $info['height']);
00202 $width = (int)round($height / $aspect);
00203 }
00204
00205 return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
00206 }
00207
00222 function image_resize($source, $destination, $width, $height) {
00223 return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
00224 }
00225
00242 function image_rotate($source, $destination, $degrees, $background = 0x000000) {
00243 return image_toolkit_invoke('rotate', array($source, $destination, $degrees, $background));
00244 }
00245
00264 function image_crop($source, $destination, $x, $y, $width, $height) {
00265 return image_toolkit_invoke('crop', array($source, $destination, $x, $y, $width, $height));
00266 }
00267