added image manipulation class
This commit is contained in:
		
							parent
							
								
									caa96faa5c
								
							
						
					
					
						commit
						6f8a9549f2
					
				| 
						 | 
				
			
			@ -0,0 +1,180 @@
 | 
			
		|||
<?php
 | 
			
		||||
namespace LAM\ImageUtils;
 | 
			
		||||
use Imagick;
 | 
			
		||||
/*
 | 
			
		||||
 | 
			
		||||
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
 | 
			
		||||
Copyright (C) 2018  Roland Gruber
 | 
			
		||||
 | 
			
		||||
This program is free software; you can redistribute it and/or modify
 | 
			
		||||
it under the terms of the GNU General Public License as published by
 | 
			
		||||
the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
(at your option) any later version.
 | 
			
		||||
 | 
			
		||||
This program is distributed in the hope that it will be useful,
 | 
			
		||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
You should have received a copy of the GNU General Public License
 | 
			
		||||
along with this program; if not, write to the Free Software
 | 
			
		||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Image manipulation functions.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Roland Gruber
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Factory to create modificators for images.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Roland Gruber
 | 
			
		||||
 */
 | 
			
		||||
class ImageManipulationFactory {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns an image manipulator based on installed PHP modules.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string $imageData binary string of image data
 | 
			
		||||
	 * @return ImageManipulator manipulator
 | 
			
		||||
	 */
 | 
			
		||||
	public static function getImageManipulator($imageData) {
 | 
			
		||||
		if (extension_loaded('imagick')) {
 | 
			
		||||
			return new ImageManipulatorImagick($imageData);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Modifies images.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Roland Gruber
 | 
			
		||||
 */
 | 
			
		||||
interface ImageManipulator {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the height of the image.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return int height
 | 
			
		||||
	 */
 | 
			
		||||
	public function getHeight();
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the width of the image.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return int width
 | 
			
		||||
	 */
 | 
			
		||||
	public function getWidth();
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Resizes the image to the given maximum dimensions.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param int $width width
 | 
			
		||||
	 * @param int $height height
 | 
			
		||||
	 */
 | 
			
		||||
	public function thumbnail($width, $height);
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Converts the image to JPEG format.
 | 
			
		||||
	 */
 | 
			
		||||
	public function convertToJpeg();
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Crops the image.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param int $x starting point in original image
 | 
			
		||||
	 * @param int $y starting point in original image
 | 
			
		||||
	 * @param int $width width of target size
 | 
			
		||||
	 * @param int $height height of target size
 | 
			
		||||
	 */
 | 
			
		||||
	public function crop($x, $y, $width, $height);
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns the image as binary string.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return string image data
 | 
			
		||||
	 */
 | 
			
		||||
	public function getImageData();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Manipulates images using Imagick library.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Roland Gruber
 | 
			
		||||
 */
 | 
			
		||||
class ImageManipulatorImagick implements ImageManipulator {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Image
 | 
			
		||||
	 *
 | 
			
		||||
	 * @var Imagick image
 | 
			
		||||
	 */
 | 
			
		||||
	private $image;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Constructor.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param string $imageData original image as binary string
 | 
			
		||||
	 */
 | 
			
		||||
	public function __construct($imageData) {
 | 
			
		||||
		$this->image = new Imagick();
 | 
			
		||||
		$this->image->readimageblob($imageData);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * {@inheritDoc}
 | 
			
		||||
	 * @see \LAM\ImageUtils\ImageManipulator::getHeight()
 | 
			
		||||
	 */
 | 
			
		||||
	public function getHeight() {
 | 
			
		||||
		$this->image->getimageheight();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * {@inheritDoc}
 | 
			
		||||
	 * @see \LAM\ImageUtils\ImageManipulator::getWidth()
 | 
			
		||||
	 */
 | 
			
		||||
	public function getWidth() {
 | 
			
		||||
		$this->image->getimagewidth();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * {@inheritDoc}
 | 
			
		||||
	 * @see \LAM\ImageUtils\ImageManipulator::getAsJpeg()
 | 
			
		||||
	 */
 | 
			
		||||
	public function convertToJpeg() {
 | 
			
		||||
		$this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
 | 
			
		||||
		$this->image->setImageFormat('jpeg');
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * {@inheritDoc}
 | 
			
		||||
	 * @see \LAM\ImageUtils\ImageManipulator::crop()
 | 
			
		||||
	 */
 | 
			
		||||
	public function crop($x, $y, $width, $height) {
 | 
			
		||||
		$this->image->cropimage($width, $height, $x, $y);
 | 
			
		||||
	}
 | 
			
		||||
	/**
 | 
			
		||||
	 * {@inheritDoc}
 | 
			
		||||
	 * @see \LAM\ImageUtils\ImageManipulator::resize()
 | 
			
		||||
	 */
 | 
			
		||||
	public function thumbnail($width, $height) {
 | 
			
		||||
		$this->image->thumbnailimage($width, $height, true);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * {@inheritDoc}
 | 
			
		||||
	 * @see \LAM\ImageUtils\ImageManipulator::getImageData()
 | 
			
		||||
	 */
 | 
			
		||||
	public function getImageData() {
 | 
			
		||||
		return $this->image->getimageblob();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
<?php
 | 
			
		||||
use \LAM\TYPES\TypeManager;
 | 
			
		||||
use LAM\ImageUtils\ImageManipulationFactory;
 | 
			
		||||
/*
 | 
			
		||||
 | 
			
		||||
  This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
 | 
			
		||||
| 
						 | 
				
			
			@ -1644,11 +1645,11 @@ class inetOrgPerson extends baseModule implements passwordService {
 | 
			
		|||
		}
 | 
			
		||||
		if (isset($_POST['form_subpage_' . get_class($this) . '_attributes_crop'])) {
 | 
			
		||||
			$messages = array();
 | 
			
		||||
			$image = new Imagick();
 | 
			
		||||
			try {
 | 
			
		||||
				$image->readImageBlob($this->attributes['jpegPhoto'][0]);
 | 
			
		||||
				$image->cropimage($_POST['croppingDataWidth'], $_POST['croppingDataHeight'], $_POST['croppingDataX'], $_POST['croppingDataY']);
 | 
			
		||||
				$this->attributes['jpegPhoto'][0] = $image->getimageblob();
 | 
			
		||||
				include_once dirname(__FILE__) . '/../imageutils.inc';
 | 
			
		||||
				$imageManipulator = ImageManipulationFactory::getImageManipulator($this->attributes['jpegPhoto'][0]);
 | 
			
		||||
				$imageManipulator->crop($_POST['croppingDataX'], $_POST['croppingDataY'], $_POST['croppingDataWidth'], $_POST['croppingDataHeight']);
 | 
			
		||||
				$this->attributes['jpegPhoto'][0] = $imageManipulator->getImageData();
 | 
			
		||||
			}
 | 
			
		||||
			catch (Exception $e) {
 | 
			
		||||
				$msg =  $this->messages['file'][2];
 | 
			
		||||
| 
						 | 
				
			
			@ -1679,18 +1680,17 @@ class inetOrgPerson extends baseModule implements passwordService {
 | 
			
		|||
			}
 | 
			
		||||
			fclose($handle);
 | 
			
		||||
			// convert to JPG
 | 
			
		||||
			$image = new Imagick();
 | 
			
		||||
			try {
 | 
			
		||||
				$image->readImageBlob($data);
 | 
			
		||||
				include_once dirname(__FILE__) . '/../imageutils.inc';
 | 
			
		||||
				$imageManipulator = ImageManipulationFactory::getImageManipulator($data);
 | 
			
		||||
				// resize if maximum values specified
 | 
			
		||||
				if (!empty($this->moduleSettings['inetOrgPerson_jpegPhoto_maxWidth'][0]) || !empty($this->moduleSettings['inetOrgPerson_jpegPhoto_maxHeight'][0])) {
 | 
			
		||||
					$maxWidth = empty($this->moduleSettings['inetOrgPerson_jpegPhoto_maxWidth'][0]) ? $image->getimagewidth() : $this->moduleSettings['inetOrgPerson_jpegPhoto_maxWidth'][0];
 | 
			
		||||
					$maxHeight = empty($this->moduleSettings['inetOrgPerson_jpegPhoto_maxHeight'][0]) ? $image->getimageheight() : $this->moduleSettings['inetOrgPerson_jpegPhoto_maxHeight'][0];
 | 
			
		||||
					$image->thumbnailimage($maxWidth, $maxHeight, true);
 | 
			
		||||
					$maxWidth = empty($this->moduleSettings['inetOrgPerson_jpegPhoto_maxWidth'][0]) ? $imageManipulator->getWidth() : $this->moduleSettings['inetOrgPerson_jpegPhoto_maxWidth'][0];
 | 
			
		||||
					$maxHeight = empty($this->moduleSettings['inetOrgPerson_jpegPhoto_maxHeight'][0]) ? $imageManipulator->getHeight() : $this->moduleSettings['inetOrgPerson_jpegPhoto_maxHeight'][0];
 | 
			
		||||
					$imageManipulator->thumbnail($maxWidth, $maxHeight);
 | 
			
		||||
				}
 | 
			
		||||
				$image->setImageCompression(Imagick::COMPRESSION_JPEG);
 | 
			
		||||
				$image->setImageFormat('jpeg');
 | 
			
		||||
				$data = $image->getimageblob();
 | 
			
		||||
				$imageManipulator->convertToJpeg();
 | 
			
		||||
				$data = $imageManipulator->getImageData();
 | 
			
		||||
			}
 | 
			
		||||
			catch (Exception $e) {
 | 
			
		||||
				$msg =  $this->messages['file'][2];
 | 
			
		||||
| 
						 | 
				
			
			@ -3392,10 +3392,10 @@ class inetOrgPerson extends baseModule implements passwordService {
 | 
			
		|||
			elseif (!empty($data) && (empty($attributes['jpegPhoto'][0]) || ($data != $attributes['jpegPhoto'][0]))) {
 | 
			
		||||
				$moduleSettings = $this->selfServiceSettings->moduleSettings;
 | 
			
		||||
				try {
 | 
			
		||||
					$image = new Imagick();
 | 
			
		||||
					$image->readimageblob($data);
 | 
			
		||||
					$image->cropimage($_POST['croppingDataWidth'], $_POST['croppingDataHeight'], $_POST['croppingDataX'], $_POST['croppingDataY']);
 | 
			
		||||
					$data = $image->getimageblob();
 | 
			
		||||
					include_once dirname(__FILE__) . '/../imageutils.inc';
 | 
			
		||||
					$imageManipulator = ImageManipulationFactory::getImageManipulator($data);
 | 
			
		||||
					$imageManipulator->crop($_POST['croppingDataX'], $_POST['croppingDataY'], $_POST['croppingDataWidth'], $_POST['croppingDataHeight']);
 | 
			
		||||
					$data = $imageManipulator->getImageData();
 | 
			
		||||
					$data = inetOrgPerson::resizeAndConvertImage($data, $moduleSettings);
 | 
			
		||||
					if (!empty($moduleSettings['inetOrgPerson_jpegPhoto_maxSize'][0]) && ($moduleSettings['inetOrgPerson_jpegPhoto_maxSize'][0] < (strlen($data) / 1024))) {
 | 
			
		||||
						$msg =  $this->messages['file'][3];
 | 
			
		||||
| 
						 | 
				
			
			@ -3527,18 +3527,16 @@ class inetOrgPerson extends baseModule implements passwordService {
 | 
			
		|||
	 * @return array binary image data
 | 
			
		||||
	 */
 | 
			
		||||
	private static function resizeAndConvertImage($data, $settings) {
 | 
			
		||||
		// convert to JPG if imagick extension is available
 | 
			
		||||
		$image = new Imagick();
 | 
			
		||||
		$image->readImageBlob($data);
 | 
			
		||||
		include_once dirname(__FILE__) . '/../imageutils.inc';
 | 
			
		||||
		$imageManipulator = ImageManipulationFactory::getImageManipulator($data);
 | 
			
		||||
		// resize if maximum values specified
 | 
			
		||||
		if (!empty($settings['inetOrgPerson_jpegPhoto_maxWidth'][0]) || !empty($settings['inetOrgPerson_jpegPhoto_maxHeight'][0])) {
 | 
			
		||||
			$maxWidth = empty($settings['inetOrgPerson_jpegPhoto_maxWidth'][0]) ? $image->getimagewidth() : $settings['inetOrgPerson_jpegPhoto_maxWidth'][0];
 | 
			
		||||
			$maxHeight = empty($settings['inetOrgPerson_jpegPhoto_maxHeight'][0]) ? $image->getimageheight() : $settings['inetOrgPerson_jpegPhoto_maxHeight'][0];
 | 
			
		||||
			$image->thumbnailimage($maxWidth, $maxHeight, true);
 | 
			
		||||
			$maxWidth = empty($settings['inetOrgPerson_jpegPhoto_maxWidth'][0]) ? $imageManipulator->getWidth() : $settings['inetOrgPerson_jpegPhoto_maxWidth'][0];
 | 
			
		||||
			$maxHeight = empty($settings['inetOrgPerson_jpegPhoto_maxHeight'][0]) ? $imageManipulator->getHeight() : $settings['inetOrgPerson_jpegPhoto_maxHeight'][0];
 | 
			
		||||
			$imageManipulator->thumbnail($maxWidth, $maxHeight);
 | 
			
		||||
		}
 | 
			
		||||
		$image->setImageCompression(Imagick::COMPRESSION_JPEG);
 | 
			
		||||
		$image->setImageFormat('jpeg');
 | 
			
		||||
		$data = $image->getimageblob();
 | 
			
		||||
		$imageManipulator->convertToJpeg();
 | 
			
		||||
		$data = $imageManipulator->getImageData();
 | 
			
		||||
		return $data;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -3618,12 +3616,11 @@ class inetOrgPerson extends baseModule implements passwordService {
 | 
			
		|||
			$handle = fopen($_FILES['qqfile']['tmp_name'], "r");
 | 
			
		||||
			$data = fread($handle, 100000000);
 | 
			
		||||
			fclose($handle);
 | 
			
		||||
			$image = new Imagick();
 | 
			
		||||
			try {
 | 
			
		||||
				$image->readImageBlob($data);
 | 
			
		||||
				$image->setImageCompression(Imagick::COMPRESSION_JPEG);
 | 
			
		||||
				$image->setImageFormat('jpeg');
 | 
			
		||||
				$data = $image->getimageblob();
 | 
			
		||||
				include_once dirname(__FILE__) . '/../imageutils.inc';
 | 
			
		||||
				$imageManipulator = ImageManipulationFactory::getImageManipulator($data);
 | 
			
		||||
				$imageManipulator->convertToJpeg();
 | 
			
		||||
				$data = $imageManipulator->getImageData();
 | 
			
		||||
			}
 | 
			
		||||
			catch (Exception $e) {
 | 
			
		||||
				$result = array('success' => false, 'error' => htmlspecialchars($e->getMessage()));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue