...

View Full Version : Usint Imagemagick identify to build a javascript array with PHP.



DrDOS
09-16-2010, 05:04 PM
This little script uses PHP to build a javascript array with width and height information. It can be greatly extended, as in the -format mode identify can write entire blocks of code, including HTML and CSS.




<?php

// Builds an array using the identify application of ImageMagick, pushes it into a javascript array.
// Place this code within the script tags, at the top.
// $path is the path to the containing directory including trailing slash.
print "var imagelist = Array();";

$output = shell_exec("identify -format '%f|%w|%h|'^ ".$path."*.[jJ][pP][gG] ".$path."*.[jJ][pP][eE][gG]");
$imagelist = explode("^","$output");

for ( $i = 0 ; $i < count($imagelist)-1 ; $i++ )
{
$data = $imagelist[$i];
print "
imagelist.push(\"".$path.$data."\");
";
}

?>

Inigoesdr
09-17-2010, 07:17 AM
Should be easier, and much safer to use getimagesize() (http://php.net/getimagesize). It's not the exact same thing, but you should get all relevant info from the function.

idalatob
09-17-2010, 11:37 AM
People are just keen to use shell_exec. Looks much cooler than getimagesize!!

Fou-Lu
09-17-2010, 04:52 PM
People are just keen to use shell_exec. Looks much cooler than getimagesize!!

While this may be true (who doesn't like CLI handling?!), many hosts actually disable the usage of shell commands from PHP for (good) security reasons. While writing things to support CLI calls are much much much faster (oh, and definitely cooler looking ;)), they are unfortunately limited in the number of users that can use them. This is just something to keep in mind especially if you are writing broad target code. The broader your target platforms and users are, the more PHP dependent your code must be.
This is what I keep in mind if I needed to write a C module for PHP instead. If its just for myself, then no big deal. But if its to support other users, how many of them can alter their modules list or have dl privilege in PHP? Instead, I'd probably write a more primitive PHP solution using only core (or standard extended features like SQL handling) which is slower, but will get the job done on all clients with whatever version of PHP its written for.

DrDOS
09-17-2010, 05:48 PM
While this may be true (who doesn't like CLI handling?!), many hosts actually disable the usage of shell commands from PHP for (good) security reasons. While writing things to support CLI calls are much much much faster (oh, and definitely cooler looking ;)), they are unfortunately limited in the number of users that can use them. This is just something to keep in mind especially if you are writing broad target code. The broader your target platforms and users are, the more PHP dependent your code must be.
This is what I keep in mind if I needed to write a C module for PHP instead. If its just for myself, then no big deal. But if its to support other users, how many of them can alter their modules list or have dl privilege in PHP? Instead, I'd probably write a more primitive PHP solution using only core (or standard extended features like SQL handling) which is slower, but will get the job done on all clients with whatever version of PHP its written for.

Good things to consider. Right now the getimagesize function seems to be somewhat broken in the current version of PHP. It returns the entire file as a string, and is very slow.



<?php
$size = getimagesize($filename);
$fp = fopen($filename, "rb");
if ($size && $fp) {
header("Content-type: {$size['mime']}");
fpassthru($fp);
print $size // or $fp;
exit;
} else {
// error
}
?>


OK, for whatever reason the above example doesn't work, it came right from the PHP pages, but here's one that does, also from there, slightly modified by me.





<?php

$filename = "0D336060d01.jpg";

list($arg0, $arg1, $arg2, $arg3) = getimagesize($filename);

print $arg0." ".$arg1." ".$arg2." ".$arg3;

echo "<br><img src=\"".$filename."\" $arg3 alt=\"getimagesize() example\" />";

?>

Fou-Lu
09-18-2010, 02:22 AM
No no, that has nothing to do with getimagesize. The print $size; is not a part of the original example; the example itself IS an image (as in, every program that reads the mime will treat it as an image), so you cannot print text out to the screen as well.

The reason why its data is because of the fopen and fpassthru. The first example you have reads an image and then serves it. It doesn't specify what the $filename is, but if it were from say $_GET (which without any proper checks is very dangerous >.<), you could use it as an image: <img src="thatFirstExamplePage.php?filename=someimage.jpg" alt="" /> for example. The second example you have just grabs the image information and adds it to the embedded link; a completely different purpose than the first example. Both the API example and yours work perfect for what they need to do, but they are completely different.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum