⚠️ Urgent! Active Attacks on MODX Revolution Sites Below Revolution 2.6.5
Subscribe: RSS
  • I have written a snippet that contains a number of functions in it. When I place this snippet multiple times within a template I get the following error:

    PHP Fatal error:  Cannot redeclare displayMygif() (previously declared in ..modsnippet/25.include.cache.php:98) in ...modsnippet/25.include.cache.php on line 104


    displayMygif() is a function I have created.

    How do I overcome this issue? Do I need to declare my functions some other way?

    Should I put all of my functions into a separate file and do a include_once on that. How do I do this within a snippet?

    Each time I call the snippet I use the following:

    [[!buildImage?
            &part=`toc`
            &layoutType=`[[*ImageLayout:getTVLabel]]`
            &imageNameStart=`is[[*IssueNumber]]-id[[*id]]-`
            &cog1=`[[*cog1]]`
            &cog2=`[[*cog2]]`
            &photo=`[[*photo]]`
        ]]


    cog1, cog2 and photo are image links. ImageLayout is a ToggleTVSet TV.

    This is all running within:
    I am using:
    MODX 2.40
    Image + 2.3.4
    pThumb 2.3.3
    PHP 5.4.37 on a Apache server

    My snippet is as follows:
    //setup a few base variables
    	$imagePath = 'images/icons/';
    	$exportPath = 'images/newsletter/';
    	$layout = $output = $thePart = $thelayoutType = $theImageName = $theCog1=$theCog2=$thePhoto='';
    	
    	//lets get some snippet properties
    	$thePart = $modx->getOption('part', $scriptProperties, '', true);
    	$thelayoutType = $thePart.getLayoutName($modx->getOption('layoutType', $scriptProperties, '', true));
    	$theImageName = $modx->getOption('imageNameStart', $scriptProperties, '', true).$thelayoutType;
    	$theCog1 = $imagePath.$thelayoutType.'_'.$modx->getOption('cog1', $scriptProperties, '', true).'.gif';
    	$theCog2 = $imagePath.$thelayoutType.'_'.$modx->getOption('cog2', $scriptProperties, '', true).'.gif';
    	$thePhoto = '/home/osldisraqadmin/public_html'.$modx->getOption('photo', $scriptProperties, '', true);
    
    displayImage ( $thelayoutType, $theImageName, $theCog1, $theCog2, $thePhoto, $exportPath);
    
    function getLayoutName($txt) {
      # based on code from
      # http://txt2re.com/index-php.php3?s=2%20cogs%201%20photo%20(Lay4)&-8&11
    
    	$re1='.*?';	# Non-greedy match on filler
    	$re2='(Lay)';	# Word 1
    	$re3='(\\d+)';	# Integer Number 1
    
    	if ($c=preg_match_all ("/".$re1.$re2.$re3."/is", $txt, $matches))
    		{
    		  $word1=$matches[1][0];
    		  $int1=$matches[2][0];
    		  return $word1.$int1;
    		}
    }
    
    function displayMygif($theImageContainer, $imageName) {
    	header ('Content-Type: image/gif');
    	$fullImagePath = 'images/newsletter/'.$imageName.'.gif';
    	imagegif($theImageContainer, $fullImagePath);
    	imagedestroy($theImageContainer);
    	return;	
    }
    
    function displayMyJPeg($theImageContainer, $imageName) {
    	header ('Content-Type: image/jpg');
    	$fullImagePath = 'images/newsletter/'.$imageName.'.jpg';
    	imagejpeg($theImageContainer, $fullImagePath, 85);
    	imagedestroy($theImageContainer);
    	return;	
    }
    
    function resizeMyPhoto ($photo, $newWidth, $newHeight) {
    	$newPhotoContainer = imagecreatetruecolor($newWidth, $newHeight);
    	//need the path of $photo
    	$newPhotoSize = imagecopyresampled($newPhotoContainer, $photo, 0, 0, 0, 0, 131, 98, imagesx($photo), imagesy($photo));
    	return $newPhotoContainer;
    }
    
    //this is the main display function
    function displayImage ( $layout, $imageName, $cog1, $cog2='false', $photo='false', $exportPath) {
            global $modx;
            global $exportPath;
    	//note that 
    	
    	
    	switch ($layout) {
    
    		case 'tocLay1':
    			$mycog1 = imagecreatefromgif($cog1);
    			$imageContainer = imagecreatetruecolor(180, 98);
    			imagecopy($imageContainer, $mycog1, 0, 0, 0, 0, 180, 98);
    			displayMygif($imageContainer, $imageName);
    			$endPath = $exportPath.$imageName.'.gif';
    			$output = $modx->getChunk('imgOutputTOC', array('imagePath'=>$endPath));
    			imagedestroy($mycog1);
    			echo $output;
    			break;
    
    		case 'tocLay2':
    			$mycog1 = imagecreatefromgif($cog1);
    			$mycog2 = imagecreatefromgif($cog2);
    			$imageContainer = imagecreatetruecolor(180, 98);
    			imagecopy($imageContainer, $mycog1, 0, 0, 0, 0, 90, 98);
    			imagecopy($imageContainer, $mycog2, 90, 0, 0, 0, 90, 98);
    			displayMygif($imageContainer, $imageName);
    			$endPath = $exportPath.$imageName.'.gif';
    			$output = $modx->getChunk('imgOutputTOC', array('imagePath'=>$endPath));
    			imagedestroy($mycog1);
    			imagedestroy($mycog2);
    			echo $output;
    			break;	
    		
    		default:
    			echo 'nothing to layout';
    			break;
    		
    	}
    	//end layout switch
    
    	return $output;
    }
    //end displayImage function
    
    return $output;
    


    Many thanks in advance for your help.

    This question has been answered by sottwell. See the first response.

    [ed. note: glynszasz last edited this post 3 years, 2 months ago.]
      Glyn Szasz
      Sydney, Australia

      Happy to help (when can) and happy to learn

      If you are a Sydney based MODX developer would love to hear from you. Please contact me.
    • discuss.answer
      Whenever you create a function in a snippet or a plugin you should enclose it in a conditional
      using function_exists

      http://forums.modx.com/thread/19686/when-developing-snippets-with-functions
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
      • Sue, you are godsend. I was about to post a followup with the exact same solution as you suggest. Many thanks.

        I just did a little more research and found the exact answer you mentioned. Fully tested now and it worked. Thank you so much for your help. It is greatly appreciated.

        For completness I have now declared my functions as:

        if(!function_exists(displayMygif)){
        	function displayMygif($theImageContainer, $imageName) {
        		header ('Content-Type: image/gif');
        		$fullImagePath = '/home/osldisraqadmin/public_html/xresource/images/newsletter/'.$imageName.'.gif';
        		imagegif($theImageContainer, $fullImagePath);
        		imagedestroy($theImageContainer);
        		return;	
        	}
        }
        

          Glyn Szasz
          Sydney, Australia

          Happy to help (when can) and happy to learn

          If you are a Sydney based MODX developer would love to hear from you. Please contact me.
        • Kind of alarming to see that post I made almost 10 years ago. How time flies!
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org