From 5300ffadf1c7faf3f505a585f2223fb6448b86bf Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 3 Aug 2013 20:34:59 -0400 Subject: Better image processing. Add support for GraphicsMagick (a fork of ImageMagick) and `exiftool` (for stripping EXIF metadata quickly). --- post.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'post.php') diff --git a/post.php b/post.php index 65e85bcc..905110d4 100644 --- a/post.php +++ b/post.php @@ -447,14 +447,17 @@ if (isset($_POST['delete'])) { } - if ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg') { + if ($config['convert_auto_orient'] && ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg')) { // The following code corrects the image orientation. // Currently only works with the 'convert' option selected but it could easily be expanded to work with the rest if you can be bothered. if (!($config['redraw_image'] || ($config['strip_exif'] && ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg')))) { - if ($config['thumb_method'] == 'convert' || $config['thumb_method'] == 'convert+gifsicle') { + if (in_array($config['thumb_method'], array('convert', 'convert+gifsicle', 'gm', 'gm+gifsicle'))) { $exif = exif_read_data($upload); + $gm = in_array($config['thumb_method'], array('gm', 'gm+gifsicle')); if (isset($exif['Orientation']) && $exif['Orientation'] != 1) { - shell_exec('convert ' . escapeshellarg($upload) . ' -auto-orient ' . escapeshellarg($upload)); + if($error = shell_exec_error(($gm ? 'gm ' : '') . 'convert ' . + escapeshellarg($upload) . ' -auto-orient ' . escapeshellarg($upload))) + error('Could not auto-orient image!', null, $error); } } } @@ -462,7 +465,6 @@ if (isset($_POST['delete'])) { // create image object $image = new Image($upload, $post['extension']); - if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) { $image->delete(); error($config['error']['maxsize']); @@ -503,8 +505,13 @@ if (isset($_POST['delete'])) { } if ($config['redraw_image'] || ($config['strip_exif'] && ($post['extension'] == 'jpg' || $post['extension'] == 'jpeg'))) { - $image->to($post['file']); - $dont_copy_file = true; + if (!$config['redraw_image'] && $config['strip_with_exiftool']) { + if($error = shell_exec_error('exiftool -q -all= ' . escapeshellarg($upload))) + error('Could not strip EXIF metadata!', null, $error); + } else { + $image->to($post['file']); + $dont_copy_file = true; + } } $image->destroy(); } else { @@ -518,7 +525,7 @@ if (isset($_POST['delete'])) { } if (!isset($dont_copy_file) || !$dont_copy_file) { - if (!@move_uploaded_file($_FILES['file']['tmp_name'], $post['file'])) + if (!@move_uploaded_file($upload, $post['file'])) error($config['error']['nomove']); } } -- cgit v1.3 From b67fc7d54e3fbde2f9e117a961998c5f8440679a Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Sat, 3 Aug 2013 20:50:37 -0400 Subject: Only use `identify` if we have to --- inc/image.php | 15 ++++++++++++--- post.php | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'post.php') diff --git a/inc/image.php b/inc/image.php index 1dac4724..92e74277 100644 --- a/inc/image.php +++ b/inc/image.php @@ -11,7 +11,7 @@ if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) { class Image { public $src, $format, $image, $size; - public function __construct($src, $format = false) { + public function __construct($src, $format = false, $size = false) { global $config; $this->src = $src; @@ -28,7 +28,7 @@ class Image { } } - $this->image = new $classname($this); + $this->image = new $classname($this, $size); if (!$this->image->valid()) { $this->delete(); @@ -122,10 +122,15 @@ class ImageBase extends ImageGD { return (bool)$this->image; } - public function __construct($img) { + public function __construct($img, $size = false) { if (method_exists($this, 'init')) $this->init(); + if ($size && $size[0] > 0 && $size[1] > 0) { + $this->width = $size[0]; + $this->height = $size[1]; + } + if ($img !== false) { $this->src = $img->src; $this->from(); @@ -244,6 +249,10 @@ class ImageConvert extends ImageBase { $this->temp = false; } public function from() { + if ($this->width > 0 && $this->height > 0) { + $this->image = true; + return; + } $size = shell_exec_error(($this->gm ? 'gm ' : '') . 'identify -format "%w %h" ' . escapeshellarg($this->src . '[0]')); if (preg_match('/^(\d+) (\d+)$/', $size, $m)) { $this->width = $m[1]; diff --git a/post.php b/post.php index 905110d4..434bc7e2 100644 --- a/post.php +++ b/post.php @@ -464,7 +464,7 @@ if (isset($_POST['delete'])) { } // create image object - $image = new Image($upload, $post['extension']); + $image = new Image($upload, $post['extension'], $size); if ($image->size->width > $config['max_width'] || $image->size->height > $config['max_height']) { $image->delete(); error($config['error']['maxsize']); -- cgit v1.3