Error handling

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
f3k

Post by f3k » 2006-05-05T12:21:22-07:00

for the fatal error problem: you have to change php's error handler.

example:

Code: Select all

function readImage($wand, $filename) {

	// read current error reporting
	$current_error_reporting = ini_get("error_reporting");
	// diable error reporting
	error_reporting(0);
	// change error handler function
	set_error_handler("magickErrorHandler");

	try {
		$result = MagickReadImage($wand, $filename);
	} catch (Exception $e) {
		// restore error handler and throw exception
		error_reporting($current_error_reporting);
		restore_error_handler();
		throw $e;
	}
	// restore error handling and return result
	error_reporting($current_error_reporting);
	restore_error_handler();
	return $result;
}

function magickErrorHandler($errno, $errmsg, $filename, $linenum, $vars) {
	if (strpos($errmsg, "no decode delegate for this image"))
		$msg = "unknown format";
	else
		$msg = $errmsg;
	throw new Exception($msg, $errno);
}
the readImage() function then throws an exception instead of dieing:

Code: Select all

try{
	$srcMagickWand = NewMagickWand();
	if (!readImage($srcMagickWand , "/tmp/bleh.txt"))
		die("something else went wrong.\n");
} catch (Exception $e) {
	print "fatal error: ".$e->getMessage()."\n";
}


cheers

f3k

Post by f3k » 2006-05-05T12:26:58-07:00

now it would be interesting to know which functions of php's api are likely to fatally die apart from MagickReadImage().

any word from the devs? :)

Post Reply