123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?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;
- }
|