| 1 |
<?php
|
| 2 |
// $Id: image.inc,v 1.1 2005/02/01 16:27:43 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Return a list of available toolkits.
|
| 6 |
*
|
| 7 |
* @return An array of toolkit name => descriptive title
|
| 8 |
*/
|
| 9 |
function image_get_available_toolkits() {
|
| 10 |
$toolkits = file_scan_directory('includes', 'image\..*\.inc$');
|
| 11 |
|
| 12 |
$output = array();
|
| 13 |
foreach ($toolkits as $file => $toolkit) {
|
| 14 |
include_once($file);
|
| 15 |
$function = str_replace('.', '_', $toolkit->name) . '_info';
|
| 16 |
if (function_exists($function)) {
|
| 17 |
$info = $function();
|
| 18 |
$output[$info['name']] = $info['title'];
|
| 19 |
}
|
| 20 |
}
|
| 21 |
$output['gd'] = t('Built-in GD Toolkit');
|
| 22 |
return $output;
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Retrieve the name of the currently used toolkit.
|
| 27 |
*
|
| 28 |
* @return String containing the name of the toolkit.
|
| 29 |
*/
|
| 30 |
function image_get_toolkit() {
|
| 31 |
static $toolkit;
|
| 32 |
if (!$toolkit) {
|
| 33 |
$toolkit = variable_get('image_toolkit', 'gd');
|
| 34 |
if ($toolkit != 'gd') {
|
| 35 |
include_once 'includes/image.'.$toolkit.'.inc';
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
return $toolkit;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Invokes the given method using the currently selected toolkit.
|
| 44 |
*
|
| 45 |
* @param $method A string containing the method to invoke.
|
| 46 |
* @param $params An optional array of parameters to pass to the toolkit method
|
| 47 |
*
|
| 48 |
* @return Mixed values (typically Boolean for successful operation)
|
| 49 |
*/
|
| 50 |
function image_toolkit_invoke($method, $params = array()) {
|
| 51 |
$toolkit = image_get_toolkit();
|
| 52 |
|
| 53 |
$function = 'image_' . $toolkit . '_'. $method;
|
| 54 |
if (function_exists($function)) {
|
| 55 |
return call_user_func_array($function, $params);
|
| 56 |
}
|
| 57 |
else {
|
| 58 |
drupal_set_message(t('%method is not supported by %toolkit.', array('%method' => "<em>$method</em>", '%toolkit' => "<em>$toolkit</em>")));
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Get details about an image.
|
| 65 |
*
|
| 66 |
* @return array containing information about the image
|
| 67 |
* 'width': image's width in pixels
|
| 68 |
* 'height': image's height in pixels
|
| 69 |
* 'extension': commonly used extension for the image
|
| 70 |
* 'mime_type': image's MIME type ('image/jpeg', 'image/gif', etc.)
|
| 71 |
*/
|
| 72 |
function image_get_info($file) {
|
| 73 |
if (!file_exists($file)) {
|
| 74 |
return false;
|
| 75 |
}
|
| 76 |
|
| 77 |
$details = false;
|
| 78 |
$data = @getimagesize($file);
|
| 79 |
|
| 80 |
if (is_array($data)) {
|
| 81 |
$extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
|
| 82 |
$extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : '';
|
| 83 |
$details = array('width' => $data[0],
|
| 84 |
'height' => $data[1],
|
| 85 |
'extension' => $extension,
|
| 86 |
'mime_type' => $data['mime']);
|
| 87 |
}
|
| 88 |
|
| 89 |
return $details;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Scales an image to the given width and height while maintaining aspect
|
| 94 |
* ratio.
|
| 95 |
*
|
| 96 |
* @param $source The filepath of the source image
|
| 97 |
* @param $destination The file path of the destination image
|
| 98 |
* @param $width The target width
|
| 99 |
* @param $height The target height
|
| 100 |
*
|
| 101 |
* @return True or false, based on success
|
| 102 |
*/
|
| 103 |
function image_scale($source, $destination, $width, $height) {
|
| 104 |
$info = image_get_info($source);
|
| 105 |
|
| 106 |
// don't scale up
|
| 107 |
if ($width > $info['width'] && $height > $info['height']) {
|
| 108 |
return false;
|
| 109 |
}
|
| 110 |
|
| 111 |
$aspect = $info['height'] / $info['width'];
|
| 112 |
if ($aspect < $height / $width) {
|
| 113 |
$width = (int)min($width, $info['width']);
|
| 114 |
$height = (int)round($width * $aspect);
|
| 115 |
} else {
|
| 116 |
$height = (int)min($height, $info['height']);
|
| 117 |
$width = (int)round($height / $aspect);
|
| 118 |
}
|
| 119 |
|
| 120 |
return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Resize an image to the given dimensions (ignoring aspect ratio).
|
| 125 |
*
|
| 126 |
* @param $source The filepath of the source image.
|
| 127 |
* @param $destination The file path of the destination image.
|
| 128 |
* @param $width The target width.
|
| 129 |
* @param $height The target height.
|
| 130 |
*/
|
| 131 |
function image_resize($source, $destination, $width, $height) {
|
| 132 |
return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Rotate an image by the given number of degrees.
|
| 137 |
*
|
| 138 |
* @param $source The filepath of the source image
|
| 139 |
* @param $destination The file path of the destination image
|
| 140 |
* @param $degrees The number of (clockwise) degrees to rotate the image
|
| 141 |
*/
|
| 142 |
function image_rotate($source, $destination, $degrees) {
|
| 143 |
return image_toolkit_invoke('rotate', array($source, $destination, $degrees));
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Crop an image to the rectangle specified by the given rectangle.
|
| 148 |
*
|
| 149 |
* @param $source The filepath of the source image
|
| 150 |
* @param $destination The file path of the destination image
|
| 151 |
* @param $x The top left co-ordinate of the crop area (x axis value)
|
| 152 |
* @param $y The top left co-ordinate of the crop area (y axis value)
|
| 153 |
* @param $width The target width
|
| 154 |
* @param $height The target height
|
| 155 |
*/
|
| 156 |
function image_crop($source, $destination, $x, $y, $width, $height) {
|
| 157 |
return image_toolkit_invoke('crop', array($source, $destination, $x, $y, $width, $height));
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* GD Toolkit functions
|
| 162 |
*/
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Verify GD settings (that the extension is actually installed).
|
| 166 |
*/
|
| 167 |
function image_gd_settings() {
|
| 168 |
if (!extension_loaded('gd')) {
|
| 169 |
drupal_set_message(t('Unable to load the GD toolkit'), 'error');
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Scale an image to the specified size using GD.
|
| 175 |
*/
|
| 176 |
function image_gd_resize($source, $destination, $width, $height) {
|
| 177 |
if (!file_exists($source)) {
|
| 178 |
return false;
|
| 179 |
}
|
| 180 |
|
| 181 |
$info = image_get_info($source);
|
| 182 |
if (!$info) {
|
| 183 |
return false;
|
| 184 |
}
|
| 185 |
|
| 186 |
$im = image_gd_open($source, $info['extension']);
|
| 187 |
if (!$im) {
|
| 188 |
return false;
|
| 189 |
}
|
| 190 |
|
| 191 |
// GD1 doesn't have true color
|
| 192 |
if (function_exists('imageCreateTrueColor')) {
|
| 193 |
$res = imageCreateTrueColor($width, $height);
|
| 194 |
}
|
| 195 |
else {
|
| 196 |
$res = imageCreate($width, $height);
|
| 197 |
}
|
| 198 |
|
| 199 |
// GD1 doesn't have copyResampled
|
| 200 |
if (function_exists('imageCopyResampled')) {
|
| 201 |
imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
|
| 202 |
}
|
| 203 |
else {
|
| 204 |
imageCopyResized($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
|
| 205 |
}
|
| 206 |
$result = image_gd_close($res, $destination, $info['extension']);
|
| 207 |
|
| 208 |
imageDestroy($res);
|
| 209 |
imageDestroy($im);
|
| 210 |
|
| 211 |
return $result;
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Rotate an image the given number of degrees.
|
| 216 |
*/
|
| 217 |
function image_gd_rotate($source, $destination, $degrees, $bg_color = 0) {
|
| 218 |
if (!function_exists('imageRotate')) {
|
| 219 |
return false;
|
| 220 |
}
|
| 221 |
|
| 222 |
$info = image_get_info($source);
|
| 223 |
if (!$info) {
|
| 224 |
return false;
|
| 225 |
}
|
| 226 |
|
| 227 |
$im = image_gd_open($source, $info['extension']);
|
| 228 |
if (!$im) {
|
| 229 |
return false;
|
| 230 |
}
|
| 231 |
|
| 232 |
$res = imageRotate($im, $degrees, $bg_color);
|
| 233 |
|
| 234 |
$result = image_gd_close($res, $destination, $info['extension']);
|
| 235 |
|
| 236 |
return $result;
|
| 237 |
}
|
| 238 |
|
| 239 |
/**
|
| 240 |
* Crop an image using the GD toolkit.
|
| 241 |
*/
|
| 242 |
function image_gd_crop($source, $destination, $x, $y, $width, $height) {
|
| 243 |
$info = image_get_info($source);
|
| 244 |
if (!$info) {
|
| 245 |
return false;
|
| 246 |
}
|
| 247 |
|
| 248 |
$im = image_gd_open($source, $info['extension']);
|
| 249 |
|
| 250 |
// GD1 doesn't have true color
|
| 251 |
if (function_exists('imageCreateTrueColor')) {
|
| 252 |
$res = imageCreateTrueColor($width, $height);
|
| 253 |
}
|
| 254 |
else {
|
| 255 |
$res = imageCreate($width, $height);
|
| 256 |
}
|
| 257 |
|
| 258 |
imageCopy($im, $res, 0, 0, $x, $y, $width, $height);
|
| 259 |
|
| 260 |
$result = image_gd_close($res, $destination, $info['extension']);
|
| 261 |
|
| 262 |
imageDestroy($res);
|
| 263 |
imageDestroy($im);
|
| 264 |
|
| 265 |
return $result;
|
| 266 |
}
|
| 267 |
|
| 268 |
/**
|
| 269 |
* GD helper function to create an image resource from a file.
|
| 270 |
*/
|
| 271 |
function image_gd_open($file, $extension) {
|
| 272 |
$extension = str_replace('jpg', 'jpeg', $extension);
|
| 273 |
$open_func = 'imageCreateFrom'. $extension;
|
| 274 |
if (!function_exists($open_func)) {
|
| 275 |
return false;
|
| 276 |
}
|
| 277 |
return $open_func($file);
|
| 278 |
}
|
| 279 |
|
| 280 |
/**
|
| 281 |
* GD helper to write an image resource to a destination file.
|
| 282 |
*/
|
| 283 |
function image_gd_close($res, $destination, $extension) {
|
| 284 |
$extension = str_replace('jpg', 'jpeg', $extension);
|
| 285 |
$close_func = 'image'. $extension;
|
| 286 |
if (!function_exists($close_func)) {
|
| 287 |
return false;
|
| 288 |
}
|
| 289 |
return $close_func($res, $destination);
|
| 290 |
}
|
| 291 |
|
| 292 |
?>
|