Image Resolution

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
zombie_nation

Image Resolution

Post by zombie_nation »

Hi,

First time posting.
Just getting my teeth into imagemagick but having some difficulty.
I have a client site that needs to upload images and produce print quality images with annotations as a standard width / height.
I have grasped the basics of imagemagick in terms of annotating and saving new images but I was hoping for a way to getting the image resolution (dpi) of the uploaded image before I do any annotating.

I am using PHP to do all the work and using exec() to run the command lines I build.
Is there a way to get the resolution and compare that to a pre-defined resolution to see if it advisable to carry on?

Would it be better to use MagickWand for PHP?

Thanks,
Z.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Image Resolution

Post by Bonzo »

Not the best way to do this; I did have a better way but can not remember how to do it now !

Code: Select all

<?php
function GetGeometry($result)
   {
      global $original_width, $original_height;
      
      $start = strpos($result, "Resolution");
      $result = substr($result, $start);
      $length = strpos($result, "\n");
      $result = substr($result, 10, $length-10); 
      $result = explode("x", $result);
      $original_width = $result[0];
      $original_height = $result[1]; 
   }
   
   $sourcefile = "_imagemagick/sunflower.jpg";
   
   $cmd = "identify -verbose ".$sourcefile; 
	exec("$cmd 2>&1", &$o, $r);

	$result = (join("\n", $o)."\n</pre>\n");

	//echo "<pre>".$result;

	GetGeometry($result);

	echo "Width = ".$original_width;
	echo "<br>Height = ".$original_height."<br>";
?>
You should be able to modify this for your needs.

EDIT: Condensed version

Code: Select all

<?
$start = strpos( $return, "Resolution" );
$result = substr( $return, $start );
$length = strpos( $result, "\n" );
$result = substr( $result, 12, $length-12 ); 
$result = explode( "x", $result );
$Resolution = $result[0]."x".$result[1];
echo "Resolution = ".$Resolution;
?>
zombie_nation

Re: Image Resolution

Post by zombie_nation »

thanks for that. I never realised there was a verbose command. Really impressed.

Z.
Post Reply