
21st June 2006, 15:07
|
|
Moderator
|
|
Join Date: Dec 2005
Location: The Netherlands
Posts: 1,721
Thanks: 148
Thanked 98 Times in 91 Posts
|
|
How do I do this in PHP?
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.
Code:
<?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?
?>
|

21st June 2006, 15:37
|
|
Pseudo Lawyer
|
|
Join Date: Apr 2006
Location: Switzerland
Posts: 857
Thanks: 3
Thanked 25 Times in 23 Posts
|
|
Quote:
<?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:
Quote:
<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:
Quote:
<?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);
?>
|
|

21st June 2006, 15:40
|
|
Moderator
|
|
Join Date: Dec 2005
Location: The Netherlands
Posts: 1,721
Thanks: 148
Thanked 98 Times in 91 Posts
|
|
Thank you... The code looks nice :-)
I was just reading about the explode option in PHP, so I made this code;
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
?>
|

21st June 2006, 15:43
|
|
Pseudo Lawyer
|
|
Join Date: Apr 2006
Location: Switzerland
Posts: 857
Thanks: 3
Thanked 25 Times in 23 Posts
|
|
well, if the exec functions returns an array then there is no need for exploding ^^
|

21st June 2006, 17:41
|
|
Moderator
|
|
Join Date: Dec 2005
Location: The Netherlands
Posts: 1,721
Thanks: 148
Thanked 98 Times in 91 Posts
|
|
Quote:
|
Originally Posted by sjau
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..?
|

21st June 2006, 18:01
|
|
Pseudo Lawyer
|
|
Join Date: Apr 2006
Location: Switzerland
Posts: 857
Thanks: 3
Thanked 25 Times in 23 Posts
|
|
well, after you explode at " " then it's the second element $result[1] ^^
|

22nd June 2006, 16:00
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 31,853
Thanks: 781
Thanked 1,558 Times in 1,477 Posts
|
|
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
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:24.
|
Recent comments
18 hours 51 min ago
1 day 7 min ago
1 day 18 min ago
1 day 26 min ago
1 day 1 hour ago
1 day 3 hours ago
1 day 6 hours ago
1 day 6 hours ago
1 day 6 hours ago
1 day 7 hours ago