View Full Version : How do I do this in PHP?
edge
21st June 2006, 15:07
I would like to use ImageMagick's identify within PHP to identify an image!
Problem (for me as I'm new to PHP), is how do I return the result(s) that are in an Array?
When I run identify /var/www/web1/web/tmp/upload/test.jpg from a SSH session I get this: JPEG 90x69 DirectClass 2kb 0.000u 0:01
All I need the PHP code to returm is the JPEG
This is what I have as PHP code till now.
<?php
exec("identify /var/www/web1/web/tmp/upload/test.jpg");
// the data returned is in an Array.
// How do I show the value(s) from the Array?
?>
sjau
21st June 2006, 15:37
<?php
exec("identify /var/www/web1/web/tmp/upload/test.jpg", $result);
$identify = $result[0];
?>
In your example it seems that JPEG is the first array element so you it is $result[0].
Here's another neat little function that lets you display (multi-)dimensional arrays quite easily:
<php
function displayArray($aArray) {
if (is_array($aArray) && (count($aArray) > 0)) {
print("<table border=1>");
print("<tr><th>Key</th><th>Value</th></tr>");
foreach ($aArray as $aKey => $aValue) {
print("<tr>");
if (!is_array($aValue)) {
if (empty($aValue)) {
print("<td>$aKey</td><td><i>$aValue</i></td>");
} else {
print("<td>$aKey</td><td>$aValue</td>");
}
} else {
print("<td>$aKey(array)</td><td>");
displayArray($aValue);
print("</td>");
}
print("</tr>");
}
print("</table>");
} else {
print("<i>empty or invalid</i>");
}
}
?>
So you could build a script like this:
<?php
function displayArray($aArray) {
if (is_array($aArray) && (count($aArray) > 0)) {
print("<table border=1>");
print("<tr><th>Key</th><th>Value</th></tr>");
foreach ($aArray as $aKey => $aValue) {
print("<tr>");
if (!is_array($aValue)) {
if (empty($aValue)) {
print("<td>$aKey</td><td><i>$aValue</i></td>");
} else {
print("<td>$aKey</td><td>$aValue</td>");
}
} else {
print("<td>$aKey(array)</td><td>");
displayArray($aValue);
print("</td>");
}
print("</tr>");
}
print("</table>");
} else {
print("<i>empty or invalid</i>");
}
}
exec("identify /var/www/web1/web/tmp/upload/test.jpg", $result);
displayArray($result);
?>
edge
21st June 2006, 15:40
Thank you... The code looks nice :-)
I was just reading about the explode option in PHP, so I made this code;
<?php
$str=exec("identify /var/www/web1/web/tmp/upload/test.jpg");
$pieces = explode(" ", $str);
echo $pieces[0]; // 1st value
echo "<br>";
echo $pieces[1]; // 2nd value
?>
sjau
21st June 2006, 15:43
well, if the exec functions returns an array then there is no need for exploding ^^
edge
21st June 2006, 17:41
well, if the exec functions returns an array then there is no need for exploding ^^
But the array returned for: identify /var/www/web1/web/tmp/upload/test.jpg
looks like this: /var/www/web1/web/tmp/upload/test.jpg JPEG 125x164 DirectClass 8kb 0.000u 0:01
I only need the JPEG
With the explode function I'm breaking the array at every 'space' it finds in the array.
I guess I called it an Array, but it's not..?
sjau
21st June 2006, 18:01
well, after you explode at " " then it's the second element $result[1] ^^
falko
22nd June 2006, 16:00
You could as well use PHP's getimagesize function to find out about the type of an image file: http://www.php.net/manual/en/function.getimagesize.php
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.