Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

Reading the imagemagick image data into a php variable


function GetGeometry( $result ){ 
global $original_width, $original_height; 
$start = strpos( $result, "Geometry" ); 
$result = substr( $result, $start ); 
$length = strpos( $result, "\n" ); 
// Uncomment the line below to find some information required later in the code
//print $start." -> ".$length."<br>"; 
$result = substr( $result, 10, $length-10 ); 
$result = explode( "x", $result ); 
$original_width = $result[0]; 
$original_height = $result[1]; 
} 
// Reads all the image data into an array.
// The width and height are seperated out using the function above 
$sourcefile = "sunflower.jpg"; 
$cmd = "identify -verbose ".$sourcefile; 
exec( "$cmd 2>&1", &$o, $r ); 
$return = ( "\n<pre>\n".join( "\n", $o )."\n</pre>\n" ); 
$answer = GetGeometry( $return ); 
echo 'Original width = '.$original_width.'<br>"; 
echo 'Original height = '.$original_height.'<br>"; 

Output:


Original width = 400
Original height = 300+0+0




Getting the info without the formatting function above


$sourcefile = "sunflower.jpg"; 
$cmd = "identify -verbose ".$sourcefile; 
exec( "$cmd 2>&1", &$o, $r ); 
$return = ( "\n<pre>\n".join( "\n", $o )."\n</pre>\n" ); 
$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; 

Output:

Resolution = 72x72




Using the built in ImageMagick format function to select the info required; the problem is it displays the information even if you do not want it to.


$sourcefile = "sunflower.jpg";
echo "File size = ";
$answer = system("identify -format \"%b\" $sourcefile"); 

Output:

File size = 38439



$sourcefile = "sunflower.jpg"; 
print "Date and time taken = ";
$answer_exif = system("identify -format \"%[EXIF:DateTime]\" $sourcefile");

Output:

Date and time taken = 2006:07:29 16:10:30


Another method where the width and height are read into a variable WITOUT displaying.


$Image_dimensions = exec("convert sunflower.jpg -format '%wx%h' info:- ");




Yet another method this time the information is read into a variable and not displayed unless asked for.


exec("identify -format \"%[EXIF:DateTime]\" sunflower.jpg", $a);
echo "Date and time taken = ".$a[0];

Output:

Date and time taken = 2006:07:29 16:10:30