Reading out image size of vector files with ImageMagick

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?".
Post Reply
macorama

Reading out image size of vector files with ImageMagick

Post 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!
macorama

Solved

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post 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 :(
macorama

Post 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...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply