PHP external command processing

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".

PHP external command processing

Post by ridera »

I had a bit of learning to do to get IM working well with PHP's external command feature. I couldn't find any good documentaion that was of significant help.

It appears that the developers prefer designers use MagickWand and that is proabably good. However, my site is on a shared server and it doesn't have MagickWand and they won't install it just for me.

Here is some code I think will be of help to designers unfamiliar with using PHP and IM's command lines. Those familiar with PHP should be able to follow the logic and my reasoning.

Code: Select all

$IMinput_fpfile= '';				         //if none OR
$IMinput_fpfile= getcwd() . '/whatever';      //make certain files are full path
$IMout_fpfile= getcwd() . '/fancy_title.png'; 	//your output file


touch($IMout_fpfile);				//this checks that IM can write its file
chmod($IMout_fpfile, 0647);		    //makes certain IM can write its file

$IMcmd_debug= ($IMdebug)? '-debug exception' : '';

//make certain your server's path to "convert" or "identify" etc. are correct	

$IMinput= (empty($IMinput_fpfile))? '' : $IMinput_fpfile;

//A typical command	
$command="convert $IMinput $IMcmd_debug -background lightblue -fill blue -font times -pointsize 72 label:Anthony $IMout_fpfile";

//here is the command line	
exec("$command", $IMarray, $IMcode);


//if there is an an input file, set to 1
	if(0){$details=exec("$identify -verbose $IMinput_fpfile", $code); var_dump($details, $code);} 

	exec("convert -version", $array); echo($array[0]);
	echo "<br><br>" . $command . "<br><br>";		

	echo "<pre>" . print_r($IMarray, TRUE) ."</pre>";
	echo "<br>Error code: " . $IMcode;
}//end if

//Leave in operational code to catch failures	
$filesize= filesize($IMout_fpfile);

if($filesize == 0 OR $IMcode !=0){

	echo "<span style=\"color:red\"> Fatal error, image processor failed to create image file. Contect tech support. IM error code=$IMcode</span>";
}//end if
Hope this helps folks getting started with IM and PHP.

If anyone has any additional help for newbies; please feel free to add it