Ncimage.php 14.3 KB
<?php
/**
 * @package  anc_image
 * @category magento
 * @mailto	code [at] netz.coop
 * @author	netz.coop eG* 
 * @copyright (c) 2014, netz.coop eG
 * 
 *   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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 */ 
class Anc_Image_Helper_Ncimage extends Mage_Core_Helper_Abstract {
	public function printAdminId() {
		$admin_id = Mage::getSingleton('admin/session')->getUser()->getId();
	}

	public function getNcImageMetaDataAsHtml($param_ncimage_id) {
		$Quoteitemoption_Collection = Mage::helper('anc_image/ncmodel')->getNcImageMetaData($param_ncimage_id);
		$ncimage_quoteitemoptions = $Quoteitemoption_Collection->getItems();
		$return_html = 'Dieses Bild haben Sie bis jetzt '.count($ncimage_quoteitemoptions).' mal verwendet!  &#10;';
		foreach($ncimage_quoteitemoptions as $ncimage_quoteitemoption) {
			$return_html .= $ncimage_quoteitemoption->getOrdertime().' &#10;';
		}
		return $return_html;
	}
	
	/**
	 * return the path from the anc_image/ncimage with the $param_ncimage_id
	 * 
	 * @param int $param_ncimage_id
	 * @param string $param_kindOfPath relative || absolute
	 * @param string $param_kindOfFile original || thumbnail
	 * @return string
	 */
	public function getNcImagePath($param_ncimage_id, $param_kindOfPath, $param_kindOfFile, $param_NoPrivacy=false) {
		
		$ncimage = Mage::helper('anc_image/ncmodel')->getNcImage($param_ncimage_id);
		if(is_object($ncimage)) {
			
			$mainpath = $this->getMainPath($ncimage, $param_NoPrivacy);
			if($mainpath) {
				if($param_kindOfFile === 'original') {

				} else if($param_kindOfFile === 'thumbnail') {
					if($this->generateThumbnail($mainpath, $ncimage)) {
					} else {
						$param_kindOfFile = 'original';
					}
					
				} else {
					D::ulli("getNcImagePath($param_ncimage_id, $param_kindOfPath, $param_kindOfFile) FALSE: kindoffile problem");
					return false;
				}
				
				if(is_file($this->getPath('absolute', $this->getAncImagePath($param_kindOfFile).$mainpath, $param_kindOfFile, $ncimage))) {
					return $this->getPath($param_kindOfPath, $this->getAncImagePath($param_kindOfFile).$mainpath, $param_kindOfFile, $ncimage);
				} else {
					D::ulli("getNcImagePath($param_ncimage_id, $param_kindOfPath, $param_kindOfFile) FALSE: datei existiert nicht   +++ ".Mage::helper('anc_lib/ncfilepath')->getThisMagentoInstallationPath().$path.DS.$ncimage->getFile());
					return false;
				}
			} else {
				D::ulli("getNcImagePath($param_ncimage_id, $param_kindOfPath, $param_kindOfFile) FALSE: es gab getMainPath probleme");
			}
			
		} else {
			D::ulli("getNcImagePath($param_ncimage_id, $param_kindOfPath, $param_kindOfFile) FALSE: es gibt kein ncimage");
			return false;
		}
	}	
	
	private function getPath($param_kindOfPath, $param_partOfPath, $param_kindOfFile, Anc_Image_Model_Ncimage $param_ncimage=null) {
		if($param_kindOfPath==='relative') {
			$root = '';
		} else if($param_kindOfPath==='absolute') {
			$root = Mage::helper('anc_lib/ncfilepath')->getThisMagentoInstallationPath();
		}
		if(is_null($param_ncimage)) {
			return $root.$param_partOfPath.DS.$param_kindOfFile.DS;
		} else {
			return $root.$param_partOfPath.DS.$param_kindOfFile.DS.$param_ncimage->getFile();
		}
		
	}
	
	public function generateThumbnail($param_path, Anc_Image_Model_Ncimage $param_ncimage) {

		if(!file_exists($this->getPath('absolute', $this->getAncImagePath('thumbnail').$param_path, 'thumbnail', $param_ncimage))) {
			$originalpath = $this->getPath('absolute', $this->getAncImagePath('original').$param_path, 'original', $param_ncimage);
			$thumbnail_filepath = $this->getPath('absolute', $this->getAncImagePath('thumbnail').$param_path, 'thumbnail', $param_ncimage);
			if(file_exists($originalpath)) {
				return mcImagick::makeThumbnail($originalpath, $thumbnail_filepath);

			} else {
				D::li('es existiert kein Bild!!!!!');
				return false;
			}
		} else {
			return true;
		}
	}
	
	public function getPathForNewNcImage($param_kindOfFile='original') {
		
		$mainpath = $this->getMainPath();
		if($mainpath) {
			$path = Mage::helper('anc_lib/ncfilepath')->getThisMagentoInstallationPath().$this->getAncImagePath().$mainpath.DS.$param_kindOfFile;
			return $path;
		} else {
			return false;
		}

	}

	private $CropfilePath = array();
	
	/**
	 * wenn es die optionalen individuelle optionen gibt, und diese im frontend durch js:crop gesetzt wurden, gibt es ein gecroptest bild zurück
	 * 
	 * 
	 * @param int $param_item_id - AncImage item id
	 * @return string
	 */
	public function getCropfilePath($param_item_id) {

		if(!array_key_exists($param_item_id, $this->CropfilePath) || !$this->CropfilePath[$param_item_id]) {
			$ItemProductOptionByOptionSku = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPAncImage);
			if ($ItemProductOptionByOptionSku) {
				$imgUrl = Mage::helper('anc_image/ncimage')->getNcImagePath($ItemProductOptionByOptionSku, 'absolute', 'original');

				$ncimage = Mage::helper('anc_image/ncmodel')->getNcImage($ItemProductOptionByOptionSku);
				if(is_object($ncimage)) {
					$mainpath = $this->getMainPath($ncimage, false);
					$crop_filepath = $this->getPath('absolute', $this->getAncImagePath('crop').$mainpath, 'crop', null);


					$Cropdata = $this->getCropdata($param_item_id);

					if(is_array($Cropdata) && !empty($Cropdata)) {
						$returnpath = $this->crop($imgUrl, $crop_filepath, $Cropdata, $ncimage->getFile());
					} else {
						$returnpath = $imgUrl;
					}
					$this->CropfilePath[$param_item_id] = $returnpath;								
				} else {
					return false;
				}
			}
		}
		return $this->CropfilePath[$param_item_id];
	}
	
	public function getCropdata($param_item_id) {
		$return = array();
		$return['cropH'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_cropH);
		$return['cropW'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_cropW);
		$return['imgH'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_imgH);
		$return['imgW'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_imgW);
		$return['imgInitH'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_imgInitH);
		$return['imgInitW'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_imgInitW);
		$return['imgX1'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_imgX1);
		$return['imgY1'] = Mage::helper('anc_lib/quoteitem')->getItemProductOptionByOptionSku($param_item_id, Mage::helper('anc_image/ncconstant')->ancSPImage_KEY_imgY1);
		if($return['imgInitW'] || $return['imgInitH'] || $return['imgW'] || $return['imgH'] || $return['imgY1'] || $return['imgX1'] || $return['cropW'] || $return['cropH']) {
			return $return;
		} else {
			return false;
		}
		
	}
	
	/**
	 * cropt das bild (schneiden, vergrößern etc) in ein temporäres verzeichnis
	 * 
	 * !!! THIS IS JUST AN EXAMPLE !!!, PLEASE USE ImageMagick or some other quality image processing libraries
	 * 
	 * @param string $imgUrl
	 * @param string $targetpath
	 * @param array $Cropdata = array(
	 *		imgInitW  	// your image original width (the one we recieved after upload)
	 *		imgInitH 	// your image original height (the one we recieved after upload)
	 *		imgW 		// your new scaled image width
	 *		imgH 		// your new scaled image height
	 *		imgX1 		// top left corner of the cropped image in relation to scaled image
	 *		imgY1 		// top left corner of the cropped image in relation to scaled image
	 *		cropW 		// cropped image width
	 *		cropH 		// cropped image height
	 * )
	 * @param string $filenamepart=''
	 * @return string
	 */
	private function crop($imgUrl, $targetpath, $Cropdata, $filenamepart='') {
		mcDir::create($targetpath);
		$jpeg_quality = 100;
		

		$what = getimagesize($imgUrl);
		switch(strtolower($what['mime']))
		{
			case 'image/png':
//				$img_r = imagecreatefrompng($imgUrl);
//				$source_image = imagecreatefrompng($imgUrl);
				$type = '.png';
				break;
			case 'image/jpeg':
//				$img_r = imagecreatefromjpeg($imgUrl);
//				$source_image = imagecreatefromjpeg($imgUrl);
				$type = '.jpeg';
				break;
			case 'image/gif':
//				$img_r = imagecreatefromgif($imgUrl);
//				$source_image = imagecreatefromgif($imgUrl);
				$type = '.gif';
				break;
			default: die('image type not supported');
		}
			/**
			 * @crop jetzt mit imagemagick
			 */
			$output_filename = $targetpath."/".$filenamepart."cropped_".rand().$type;
			/**
			 *  *		imgInitW  	// your image original width (the one we recieved after upload)
	 *		imgInitH 	// your image original height (the one we recieved after upload)
	 *		imgW 		// your new scaled image width
	 *		imgH 		// your new scaled image height
	 *		imgX1 		// top left corner of the cropped image in relation to scaled image
	 *		imgY1 		// top left corner of the cropped image in relation to scaled image
	 *		cropW 		// cropped image width
	 *		cropH 		// cropped image height
			 */
//			D::s($Cropdata,'$Cropdata',5,1,1);
			/**
			 *  Skalier das Bild
			 */
			$this->scalewithimagemagick($imgUrl,$output_filename,$Cropdata['imgW'], $Cropdata['imgH'],true );
			/**
			 * Beschneide das Bild
			 */
			$this->cropwithimagemagick($output_filename,$output_filename,$Cropdata['cropW'], $Cropdata['cropH'], $Cropdata['imgX1'], $Cropdata['imgY1'] );
			return $output_filename ;
//		
//			$resizedImage = imagecreatetruecolor($Cropdata['imgW'], $Cropdata['imgH']);
//			imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $Cropdata['imgW'], 
//						$Cropdata['imgH'], $Cropdata['imgInitW'], $Cropdata['imgInitH']);	
//
//			$dest_image = imagecreatetruecolor($Cropdata['cropW'], $Cropdata['cropH']);
//			imagecopyresampled($dest_image, $resizedImage, 0, 0, $Cropdata['imgX1'], $Cropdata['imgY1'], $Cropdata['cropW'], 
//						$Cropdata['cropH'], $Cropdata['cropW'], $Cropdata['cropH']);	
//
//
//			imagejpeg($dest_image, $output_filename.$type, $jpeg_quality);
//
//			return $output_filename.$type ;
	}	
	
	
	private function scalewithimagemagick($paramInfile,$param_outfile,$param_scale_new_x,$param_scale_new_y,$param_bestfit=true){
		$image = new Imagick($paramInfile);
		$image->scaleImage($param_scale_new_x,$param_scale_new_y,$param_bestfit );
		$image->writeImage($param_outfile);
	}
	
	
	private function cropwithimagemagick($paramInfile,$param_outfile,$param_crop_image_size_x,$param_crop_image_size_y,$param_crop_image_topleft_x,$param_crop_topleft_size_y){
		$image = new Imagick($paramInfile);
		$image->cropImage($param_crop_image_size_x,$param_crop_image_size_y,$param_crop_image_topleft_x,$param_crop_topleft_size_y);
		$image->writeImage($param_outfile);
	}
	
	
	/**
	 * gibt den pfad für das 
	 * 
	 * @return string
	 */
	private function getAncImagePath($param_kindOfFile) {
		
		if($param_kindOfFile==='thumbnail' || $param_kindOfFile==='crop') {
			$tmp = true;
		} else {
			$tmp = false;
		}
		
		return Mage::helper('anc_lib/ncfilepath')->getModuleFilepath('anc_image',$tmp);
	}

	/**
	 * gibt den wesentlichen Part zurück vom Pfad zur NcImage datei ... ob benutzer, öffentlich oder so 
	 * 
	 * @param Anc_Image_Model_Ncimage $param_ncimage
	 * @return string|boolean
	 */
	private function getMainPath(Anc_Image_Model_Ncimage $param_ncimage=null, $param_NoPrivacy=false) {

		$admin_logged_in = Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();

		if(Mage::app()->getStore()->isAdmin()){
			$vartest='true';
		}
		
		if(is_object($param_ncimage)
				&& (
						(
								$param_ncimage->getCustomerId() == 0 
							&&	$param_ncimage->getAdminUserId() > 0
						)
					)
				)
		{
			$path .= DS.'admin'.DS.$param_ncimage->getAdminUserId(); 
		}
		
		
		
		// wenns ein objekt gibt, dann überprüf auch ob du darfst
		else if(is_object($param_ncimage) 
			&&	(
					$param_NoPrivacy 
						|| 
					(
						($param_ncimage->getCustomerId()==0 || $param_ncimage->getCustomerId()===Mage::getSingleton('customer/session')->getCustomer()->getId()) 
							|| 
						Mage::app()->getStore()->isAdmin()
					)				
			)
		) {
			$path .= DS.'customer'.DS.$param_ncimage->getCustomerId(); 
			
		// ansonsten bei keinem objekt einfach 
		} else if(!is_object($param_ncimage)) {
			
			# Ensure we are in the admin session namespace for checking the admin user..
			
			if(Mage::getSingleton('customer/session')->isLoggedIn()) {
				$path .= DS.'customer'.DS.Mage::getSingleton('customer/session')->getCustomer()->getId(); 
			} else if($admin_logged_in){
				$path .= DS.'admin'.DS.Mage::getSingleton('admin/session')->getUser()->getId();; 
			}else{
				$path .= DS.'customer'.DS.'0'; 
			}
			
			
		} else {
			return false;
		}
		return $path;
	}


	public function getFontName($param_customerid,$param_char='A'){
//		$customer = Mage::getSingleton('customer/session')->getCustomer()->getId();
		$obj_ncimage =   Mage::helper('anc_image/ncmodel')->getNcImagesForCustomer($param_customerid);
		$array_ncimage = $obj_ncimage->getData();
		$fontname=false;
//		D::s($array_ncimage,'$array_ncimage',5,1);
		if(is_array($array_ncimage)&& $array_ncimage[0]){
			foreach ($array_ncimage as $key){
//				D::ulli('Outside if'.$key.' $key; '. $array_ncimage[$key]['font'],1,1);
//				D::s($key,'$array_ncimage',5,1,1);
				if($key['font']&& $key['issignature']==$param_char){
//					D::ulli('Inside if'. $key['font'],1,1);
					$fontname=  $key['font'];
				}

			}
		}
		if($fontname){
			$fontname=str_replace('.ttf', '', $fontname);
			
		}
		
		return $fontname;
		
//		return 'test';
		
	}
	
}