class_mcImagick.inc.php 2.62 KB
<?php
/** 
 * @copyright (c) 2014, netz.coop eG
 */

class mcImagick {
	
	private static $files = array();
	
	public static function getData($param_file_path) {
		
		if(file_exists($param_file_path)) {
			$ncimage_Imagick = new Imagick($param_file_path);

			$ncimage_data = array(
				'height' => $ncimage_Imagick->getImageHeight(),
				'width' => $ncimage_Imagick->getImageWidth(),
				'properties' => $ncimage_Imagick->getImageProperties(),
				'size' => $ncimage_Imagick->getImageSize(),
				'type' => $ncimage_Imagick->getImageType(),
			);		

			return $ncimage_data;			
		}
		

	}
	/**
	* Calculate new image dimensions to new constraints
	*
	* @param Original X size in pixels
	* @param Original Y size in pixels
	* @return New X maximum size in pixels
	* @return New Y maximum size in pixels
	 * http://php.net/manual/de/imagick.thumbnailimage.php
	*/
	private static function scaleImage($x,$y,$cx,$cy) {
		//Set the default NEW values to be the old, in case it doesn't even need scaling
		list($nx,$ny)=array($x,$y);

		//If image is generally smaller, don't even bother
		if ($x>=$cx || $y>=$cx) {

			//Work out ratios
			if ($x>0) $rx=$cx/$x;
			if ($y>0) $ry=$cy/$y;

			//Use the lowest ratio, to ensure we don't go over the wanted image size
			if ($rx>$ry) {
				$r=$ry;
			} else {
				$r=$rx;
			}

			//Calculate the new size based on the chosen ratio
			$nx=intval($x*$r);
			$ny=intval($y*$r);
		}   

		//Return the results
		return array($nx,$ny);
	}	
	public static function makeThumbnail($param_original_filepath, $param_thumbnail_filepath, $param_maximumWidth=100, $param_maximumHeight=100) {

		if(file_exists($param_original_filepath)) {
			//Read original image and create Imagick object
			try { 
				$thumb=new Imagick($param_original_filepath);
			} catch (Exception $e) {
				D::li('Exception abgefangen: '.  $e->getMessage());
				return false;
			}				

			try {
				//Work out new dimensions
				list($newX,$newY)=mcImagick::scaleImage(
						$thumb->getImageWidth(),
						$thumb->getImageHeight(),
						$param_maximumWidth,
						$param_maximumHeight);

				//Scale the image
				$thumb->thumbnailImage($newX,$newY);				
			} catch (Exception $e) {
				D::li('Exception abgefangen: '.  $e->getMessage());
				return false;
			}				

			$create = mcDir::create(dirname($param_thumbnail_filepath));

			//Write the new image to a file
			try {
				$thumb->writeImage($param_thumbnail_filepath);			
			} catch (Exception $e) {
				D::li('Exception abgefangen: '.  $e->getMessage());
				return false;
			}
			
			if(file_exists($param_thumbnail_filepath)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}