| 1 |
<?php
|
| 2 |
// $Id: image.imagemagick.inc,v 1.1 2004/12/16 04:18:46 weitzman Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Return information about the imagemagick toolkit
|
| 6 |
*/
|
| 7 |
function image_imagemagick_info() {
|
| 8 |
return array('name' => 'imagemagick', 'title' => 'ImageMagick Toolkit.');
|
| 9 |
|
| 10 |
}
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Validate and return toolkit specific settings
|
| 14 |
*/
|
| 15 |
function image_imagemagick_settings() {
|
| 16 |
$convert_file = variable_get('image_imagemagick_convert', '/usr/bin/convert');
|
| 17 |
|
| 18 |
if (!file_exists($convert_file)) {
|
| 19 |
form_set_error('image_imagemagick_convert', t('%file does not exist or is not executable.', array('%file' => "<em>$convert_file</em>")));
|
| 20 |
}
|
| 21 |
|
| 22 |
return form_textfield(t('Location of the "convert" binary'), 'image_imagemagick_convert', $convert_file, 64, 64);
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Resize an image to the given width and height
|
| 27 |
*/
|
| 28 |
function image_imagemagick_resize($source, $dest, $width, $height) {
|
| 29 |
$filter = ' -scale '. $width . 'x' . $height . ' -filter QUADRATIC';
|
| 30 |
return _image_imagemagick_convert($source, $dest, $filter);
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Rotate an image
|
| 35 |
*/
|
| 36 |
function image_imagemagick_rotate($source, $dest, $degrees) {
|
| 37 |
$filter = ' -rotate ' . escapeshellarg($degrees) . ' -background #000000';
|
| 38 |
return _image_imagemagick_convert($source, $dest, $filter);
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Crop an image to the specified dimensions
|
| 43 |
*/
|
| 44 |
function image_imagemagick_crop($source, $dest, $x, $y, $width, $height) {
|
| 45 |
$filter = ' -crop ' . $width . 'x' . $height . '+' . $x . '+' . $y;
|
| 46 |
return _image_imagemagick_convert($source, $dest, $filter);
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Helper function: Escape filename for Unix or Windows shell.
|
| 51 |
*
|
| 52 |
* @param $filename filename to escape -- may contain spaces or other problematic characters
|
| 53 |
*
|
| 54 |
* @return escaped version of $filename which should pass through shell
|
| 55 |
*/
|
| 56 |
function _image_escape_shell($filename) {
|
| 57 |
if (strstr($_SERVER['SERVER_SOFTWARE'], 'Win32') || strstr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
|
| 58 |
return '"' . addslashes($filename) . '"';
|
| 59 |
} else {
|
| 60 |
return escapeshellarg($filename);
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Calls the convert executable with the specified filter
|
| 66 |
*/
|
| 67 |
function _image_imagemagick_convert($source, $dest, $filter) {
|
| 68 |
$convert_path = variable_get('image_imagemagick_convert', '/usr/bin/convert');
|
| 69 |
if (!file_exists($convert_path)) {
|
| 70 |
return false;
|
| 71 |
}
|
| 72 |
|
| 73 |
$filter = preg_replace("/[^A-Za-z0-9\.\-\+\040]/", '', $filter);
|
| 74 |
$source = _image_escape_shell($source);
|
| 75 |
$dest = _image_escape_shell($dest);
|
| 76 |
$err = exec("$convert_path $filter $source $dest");
|
| 77 |
if ($err) {
|
| 78 |
return false;
|
| 79 |
}
|
| 80 |
if (!file_exists(trim($dest, "'"))) {
|
| 81 |
return false;
|
| 82 |
}
|
| 83 |
|
| 84 |
return true;
|
| 85 |
}
|
| 86 |
?>
|