Page 1 of 1

Reading out image size of vector files with ImageMagick

Posted: 2007-01-26T09:40:53-07:00
by macorama
Hello everybody,

I was reading through the forum but I couldn't find any clue how I could get ImageMagick just returning image infos to a php variable.
My aim is finding out the image size of vector files, lets say its 400x600px. Depending on that, my next action would be php calculating the resize values. Then i would call IM again to do the actual resizing.

Something like that:

Code: Select all

$cmd = MAGICK_HOME."convert -identify verbose ".$sourcefile;
exec("$cmd");
# Here I need to evaluate the result from the IM call, I have no idea how to read this and put it into a variable
...
$newheight = #based on the IM result
...
$cmd2 = MAGICK_HOME."convert -sample ".$newheight." ".$sourcefile." ".$targetfile;
exec("$cmd2");
Anybody an idea? Would be great!

Solved

Posted: 2007-01-26T14:03:46-07:00
by macorama
Ok, after playing around a bit, i figured it out.

If you want to get details on your image before you do anything and you do not want to use the php function "getimagesize" (or if you can't because you are processing vector files) and if you don't have MagickWand but just IM you can do the following:

Call the identify command on your image:

Code: Select all

$cmd = MAGICK_HOME."identify -verbose ".$sourcefile;
exec("$cmd 2>&1", &$o, $r);
Format and join the return of the command into a variable:

Code: Select all

$return = (join("\n", $o)."\n</pre>\n");
Ok, i guess that was the most difficult part. The next thing is just having some functions you could easily addopt to any other information you need:

Code: Select all

function GetGeometry($result)
	{
		global $original_width, $original_height;
		
		$start = strpos($result, "Geometry");
		$result = substr($result, $start);
		$length = strpos($result, "\n");
		print $start."->".$length."<br>";
		$result = substr($result, 10, $length-10); 
		$result = explode("x", $result);
		$original_width = $result[0];
		$original_height = $result[1];
	}
I hope this helps someone, at least it did for me. Maybe there is an easier way, let me know!

regards,
macorama

Posted: 2007-01-26T15:25:46-07:00
by Bonzo
Thats interesting macorama although I needed to change the first part of the code to:

Code: Select all

$cmd = "identify -verbose ".$sourcefile; 
exec("$cmd 2>&1", &$o, $r);
Although I can not at the moment think of a use for it in my case :(

Posted: 2007-01-26T16:18:49-07:00
by macorama
Hello Bonzo,

i have to define the home path of IM:

Code: Select all

define('MAGICK_HOME','/usr/bin/');
then I can use the path as a defined parameter:

Code: Select all

$cmd = MAGICK_HOME."identify -verbose ".$sourcefile; 
I guess thats just depending on your system :)

I just found out:
using the "-format" command could shorten that script alot...

Posted: 2007-01-28T21:44:24-07:00
by anthony
Actually it means the web server was not started with a 'PATH' setting that included the IM commands. It may also cause problems for other delegate commands, used for specialised image conversions.