<![CDATA[ PHP Fatal error: Cannot redeclare function() (previously declared in a snippet - My Forums]]> https://forums.modx.com/thread/?thread=98379 <![CDATA[PHP Fatal error: Cannot redeclare function() (previously declared in a snippet]]> https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532004
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.]]>
glynszasz Sep 22, 2015, 06:49 AM https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532004
<![CDATA[Re: PHP Fatal error: Cannot redeclare function() (previously declared in a snippet]]> https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532010 sottwell Sep 22, 2015, 07:28 AM https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532010 <![CDATA[Re: PHP Fatal error: Cannot redeclare function() (previously declared in a snippet]]> https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532008
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;	
	}
}

]]>
glynszasz Sep 22, 2015, 07:25 AM https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532008
<![CDATA[Re: PHP Fatal error: Cannot redeclare function() (previously declared in a snippet (Best Answer)]]> https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532006 using function_exists

http://forums.modx.com/thread/19686/when-developing-snippets-with-functions]]>
sottwell Sep 22, 2015, 07:08 AM https://forums.modx.com/thread/98379/php-fatal-error-cannot-redeclare-function-previously-declared-in-a-snippet#dis-post-532006