Location: PHPKode > projects > HikaShop > back/helpers/image.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class hikashopImageHelper{
	function hikashopImageHelper(){
		$config =& hikashop::config();
		$uploadFolder = ltrim(JPath::clean(html_entity_decode($config->get('uploadfolder'))),DS);
		$uploadFolder = rtrim($uploadFolder,DS).DS;
		$this->uploadFolder_url = str_replace(DS,'/',$uploadFolder);
		$this->uploadFolder = JPATH_ROOT.DS.$uploadFolder;
		$app =& JFactory::getApplication();
		if($app->isAdmin()){
			$this->uploadFolder_url = '../'.$this->uploadFolder_url;
		}else{
			$this->uploadFolder_url = JURI::base(true).'/'.$this->uploadFolder_url;
		}
		$this->thumbnail = $config->get('thumbnail',1);
		$this->thumbnail_x=$config->get('thumbnail_x',100);
		$this->thumbnail_y=$config->get('thumbnail_y',100);
		$this->main_thumbnail_x=$this->thumbnail_x;
		$this->main_thumbnail_y=$this->thumbnail_y;
		$this->main_uploadFolder_url = $this->uploadFolder_url;
		$this->main_uploadFolder = $this->uploadFolder;
	}
	function display($path,$addpopup=true,$alt="",$options='',$optionslink=''){
		$html = '';
		if(!$this->_checkImage($this->uploadFolder.$path)){
			$config =& hikashop::config();
			$path = $config->get('default_image');
			if($path == 'barcode.png'){
				$this->uploadFolder_url=HIKASHOP_IMAGES;
				$this->uploadFolder=HIKASHOP_MEDIA.'images'.DS;
			}
			if(!$this->_checkImage($this->uploadFolder.$path)){
				$this->uploadFolder_url = $this->main_uploadFolder_url;
				$this->uploadFolder = $this->main_uploadFolder;
				return $html;
			}
		}
		list($this->width, $this->height) = getimagesize($this->uploadFolder.$path); 
		$html = $this->displayThumbnail($path,$alt,is_string($addpopup),$options);
		if($addpopup){
			$config =& hikashop::config();
			$popup_x=$config->get('max_x_popup',760);
			$popup_y=$config->get('max_y_popup',480);
			$this->width+=20;
			$this->height+=30;
			if($this->width>$popup_x) $this->width = $popup_x;
			if($this->height>$popup_y) $this->height = $popup_y;
			if(is_string($addpopup)){
				static $first=true;
				if($first){
					$js = '
					function hikashopChangeImage(id,url,x,y,obj){
						image=document.getElementById(id);
						if(image){
							image.src=url;
							image.width=x;
							image.height=y;
						}
						image_link = document.getElementById(id+\'_link\');
						if(image_link){
							image_link.href=obj.href;
							image_link.rel=obj.rel;
						}
						var myEls = getElementsByClass(\'hikashop_child_image\');
						for ( i=0;i<myEls.length;i++ ) {
							myEls[i].style.border=\'0px\';
						}
						obj.childNodes[0].style.border=\'1px solid\';
						return false;
					}
					function getElementsByClass(searchClass,node,tag) {
						var classElements = new Array();
						if ( node == null )
							node = document;
						if ( tag == null )
							tag = \'*\';
						var els = node.getElementsByTagName(tag);
						var elsLen = els.length;
						var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
						for (i = 0, j = 0; i < elsLen; i++) {
							if ( pattern.test(els[i].className) ) {
								classElements[j] = els[i];
								j++;
							}
						}
						return classElements;
					}
					window.addEvent(\'domready\', function() {
						image_link = document.getElementById(\'hikashop_image_small_link_first\');
						if(image_link){
							image_link.childNodes[0].style.border=\'1px solid\';
						}
					});
					';
					$doc =& JFactory::getDocument();
					$doc->addScriptDeclaration($js);
					$first=false;
					$optionslink.=' id="hikashop_image_small_link_first" ';
					JHTML::_('behavior.modal');
				}
				$html = '<a class="hikashop_image_small_link" rel="{handler: \'iframe\', size: {x: '.$this->width.', y: '.$this->height.'}}" href="'.$this->uploadFolder_url.$path.'" onclick="SqueezeBox.fromElement(this,{parse: \'rel\'});return false;" target="_blank" onmouseover="return hikashopChangeImage(\''.$addpopup.'\',\''.$this->uploadFolder_url_thumb.'\',\''.$this->thumbnail_x.'\',\''.$this->thumbnail_y.'\',this);" '.$optionslink.'>'.$html.'</a>';
			}else{
				JHTML::_('behavior.modal');
				$html = '<a rel="{handler: \'iframe\', size: {x: '.$this->width.', y: '.$this->height.'}}" target="_blank" href="'.$this->uploadFolder_url.$path.'" onclick="SqueezeBox.fromElement(this,{parse: \'rel\'});return false;" '.$optionslink.'>'.$html.'</a>';
			}
		}
		$this->uploadFolder_url = $this->main_uploadFolder_url;
		$this->uploadFolder = $this->main_uploadFolder;
		return $html;
	}
	function _checkImage($path){
		if(!empty($path)){
			jimport('joomla.filesystem.file');
			if(JFile::exists($path)){
				return true;
			}
		}
		return false;
	}



	
	function getPath($file_path,$url=true){
		if($url){
			return $this->uploadFolder_url.$file_path;
		}
		return $this->uploadFolder.$file_path;
	}
	function displayThumbnail($path,$alt='',$reduceSize=false,$options=''){
		$new = $this->scaleImage($this->width, $this->height,$this->main_thumbnail_x,$this->main_thumbnail_y);
		if($new!==false){
			$this->thumbnail_x=$new[0];
			$this->thumbnail_y=$new[1];
		}else{
			$this->thumbnail_x = $this->width;
			$this->thumbnail_y = $this->height;
		} 
		if(!$reduceSize){
			$options.=' height="'.$this->thumbnail_y.'" width="'.$this->thumbnail_x.'" ';
		}
		if($this->thumbnail){
			jimport('joomla.filesystem.file');
			if(JFile::exists($this->uploadFolder.'thumbnail'.DS.$path)){
				$this->uploadFolder_url_thumb=$this->uploadFolder_url.'thumbnail/'.$path;
				return '<img src="'.$this->uploadFolder_url_thumb.'" alt="'.$alt.'" '.$options.' />';
			}
		}
		$this->uploadFolder_url_thumb=$this->uploadFolder_url.$path;
		return '<img src="'.$this->uploadFolder_url_thumb.'" alt="'.$alt.'" '.$options.' />';
	}
	function generateThumbnail($file_path){
		if($this->thumbnail){
			$ok = false;
			if (function_exists('gd_info')) {
				$gd = gd_info();
				if (isset ($gd["GD Version"])) {
					$ok = true;
					list($this->width, $this->height) = getimagesize($this->uploadFolder.$file_path);
					$config =& hikashop::config();
					$thumbnail_x=$config->get('thumbnail_x',100);
					$thumbnail_y=$config->get('thumbnail_y',100);
					$new = $this->scaleImage($this->width, $this->height,$thumbnail_x,$thumbnail_y);
					if($new!==false){
						$this->_resizeImage($file_path,$new[0],$new[1]);
					}
				}
			}
			if(!$ok){
				$app =& JFactory::getApplication();
				if($app->isAdmin()){
					$app->enqueueMessage('The PHP GD extension could not be found. Thus, it is impossible to generate thumbnails in PHP from your images. If you want HikaShop to generate thumbnails you need to install GD or ask your hosting company to do so. Otherwise, you can deactivate thumbnails creation in the configuration of HikaShop and this message won\'t be displayed');
				}
			}
		}
	}
	function resizeImage($file_path){
		$config =& hikashop::config();
		$image_x=$config->get('image_x',0);
		$image_y=$config->get('image_y',0);
		$watermark_name = $config->get('watermark','');
		if(($image_x && $image_y) || !empty($watermark_name)){
			$ok = false;
			if (function_exists('gd_info')) {
				$gd = gd_info();
				if (isset ($gd["GD Version"])) {
					$ok = true;
					$new = getimagesize($this->uploadFolder.$file_path);
					if($image_x && $image_y){
						$this->width=$new[0];
						$this->height=$new[1];
						$new = $this->scaleImage($this->width, $this->height,$image_x,$image_y);
						if($new===false){
							$new = array($this->width,$this->height);
						}
					}
					$this->_resizeImage($file_path,$new[0],$new[1],$this->uploadFolder,'image');
				}
			}
			if(!$ok){
				$app =& JFactory::getApplication();
				if($app->isAdmin()){
					$app->enqueueMessage('The PHP GD extension could not be found. Thus, it is impossible to process your images in PHP. If you want HikaShop to process your images, you need to install GD or ask your hosting company to do so. Otherwise, you can deactivate thumbnails creation, remove your watermark image if any, and clear the image max width and height in the configuration of HikaShop and this message won\'t be displayed');
				}
			}
		}
	}
	function _resizeImage($file_path,$newWidth,$newHeight,$dstFolder='',$type='thumbnail'){
		$image = $this->uploadFolder.$file_path;
		if(empty($dstFolder)){
			$dstFolder = $this->uploadFolder_url.'thumbnail/';
		}
		$watermark_path = '';
		if($type=='image'){
			if(hikashop::level(2)){
				$config =& hikashop::config();
				$watermark_name = $config->get('watermark','');
				if(!empty($watermark_name)){
					$watermark_path = $this->main_uploadFolder.$watermark_name;
					if(!$this->_checkImage($watermark_path)){
						$watermark_path = '';
					}else{
						$wm_extension = strtolower(substr($watermark_path,strrpos($watermark_path,'.')+1));
						$watermark = $this->_getImage($watermark_path,$wm_extension);
					}
				}
			}
		}
		$extension = strtolower(substr($file_path,strrpos($file_path,'.')+1));
		$img = $this->_getImage($image,$extension);
		if($newWidth!=$this->width || $newHeight!=$this->height){
			$thumb = ImageCreateTrueColor($newWidth, $newHeight);
			if(in_array($extension,array('gif','png'))){
				imagealphablending($thumb, false);
				imagesavealpha($thumb,true);
			}
			if(function_exists("imageAntiAlias")) {
				imageAntiAlias($thumb,true);
			}
			if(function_exists("imagecopyresampled")){
				ImageCopyResampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight,$this->width, $this->height);
			}else{
				ImageCopyResized($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight,$this->width, $this->height);
			}
		}else{
			$thumb =& $img;
		}
		if(!empty($watermark_path)){
			list($wm_width,$wm_height) = getimagesize($watermark_path);
			$padding = 3;
			$dest_x = $newWidth - $wm_width - $padding;  
			$dest_y = $newHeight - $wm_height - $padding;
			$this->imagecopymerge_alpha($thumb, $watermark, $dest_x, $dest_y, 0, 0, $wm_width, $wm_height, (int)$config->get('opacity',0));
			imagedestroy($watermark);
		}
		$dest = $dstFolder.$file_path;
		ob_start();
		switch($extension){
			case 'gif':
				$status = imagegif($thumb);
				break;
			case 'jpg':
			case 'jpeg':
				$status = imagejpeg($thumb,null,100);
				break;
			case 'png':
				$status = imagepng($thumb,null,0);
				break;
		}
		$imageContent = ob_get_clean();
		$status = $status && JFile::write($dest,$imageContent);



		imagedestroy($img);
		@imagedestroy($thumb);
	}
	function _getImage($image,$extension){
		switch($extension){
			case 'gif':
				return ImageCreateFromGIF($image);
				break;
			case 'jpg':
			case 'jpeg':
				return ImageCreateFromJPEG($image);
				break;
			case 'png':
				return ImageCreateFromPNG($image);
				break;
		}
	}
	function scaleImage($x,$y,$cx,$cy) {	    
	    if ($x>=$cx || $y>=$cx) {
	        if ($x>0) $rx=$cx/$x;
	        if ($y>0) $ry=$cy/$y;
	        if ($rx>$ry) {
	            $r=$ry;
	        } else {
	            $r=$rx;
	        }
	        $x=intval($x*$r);
	        $y=intval($y*$r);
	        return array($x,$y);
	    }
	    return false;
	}
	function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
	    if(!isset($pct)){ 
	        return false; 
	    } 
	    $pct /= 100; 
	    $w = imagesx( $src_im ); 
	    $h = imagesy( $src_im ); 
	    imagealphablending( $src_im, false ); 
	    $minalpha = 127; 
	    for( $x = 0; $x < $w; $x++ ) 
	    for( $y = 0; $y < $h; $y++ ){ 
	        $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF; 
	        if( $alpha < $minalpha ){ 
	            $minalpha = $alpha; 
	        } 
	    } 
	    for( $x = 0; $x < $w; $x++ ){ 
	        for( $y = 0; $y < $h; $y++ ){ 
	            $colorxy = imagecolorat( $src_im, $x, $y ); 
	            $alpha = ( $colorxy >> 24 ) & 0xFF; 
	            if( $minalpha !== 127 ){ 
	                $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha ); 
	            } else { 
	                $alpha += 127 * $pct; 
	            } 
	            $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha ); 
	            if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){ 
	                return false; 
	            } 
	        } 
	    } 
	    imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
	}
}
Return current item: HikaShop