#1  
Old 21st June 2006, 15:07
edge edge is offline
Moderator
 
Join Date: Dec 2005
Location: The Netherlands
Posts: 1,721
Thanks: 148
Thanked 98 Times in 91 Posts
Default 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?

?>
Reply With Quote
  #2  
Old 21st June 2006, 15:37
sjau sjau is offline
Pseudo Lawyer
 
Join Date: Apr 2006
Location: Switzerland
Posts: 857
Thanks: 3
Thanked 25 Times in 23 Posts
Default

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);

?>
Reply With Quote
  #3  
Old 21st June 2006, 15:40
edge edge is offline
Moderator
 
Join Date: Dec 2005
Location: The Netherlands
Posts: 1,721
Thanks: 148
Thanked 98 Times in 91 Posts
Default

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

?>
Reply With Quote
  #4  
Old 21st June 2006, 15:43
sjau sjau is offline
Pseudo Lawyer
 
Join Date: Apr 2006
Location: Switzerland
Posts: 857
Thanks: 3
Thanked 25 Times in 23 Posts
Default

well, if the exec functions returns an array then there is no need for exploding ^^
Reply With Quote
  #5  
Old 21st June 2006, 17:41
edge edge is offline
Moderator
 
Join Date: Dec 2005
Location: The Netherlands
Posts: 1,721
Thanks: 148
Thanked 98 Times in 91 Posts
Default

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..?
Reply With Quote
  #6  
Old 21st June 2006, 18:01
sjau sjau is offline
Pseudo Lawyer
 
Join Date: Apr 2006
Location: Switzerland
Posts: 857
Thanks: 3
Thanked 25 Times in 23 Posts
Default

well, after you explode at " " then it's the second element $result[1] ^^
Reply With Quote
  #7  
Old 22nd June 2006, 16:00
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 31,853
Thanks: 781
Thanked 1,558 Times in 1,477 Posts
Default

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
__________________
Falko
--
Follow me on:
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
"unknown filter" growing apache log. wwparrish Installation/Configuration 6 1st September 2006 19:40
perfect setup suse 10 - phpmyadmin & mysql question reddog Server Operation 7 17th June 2006 13:59
2 domains, 1 site wadims Installation/Configuration 13 31st May 2006 01:21
Downgrade php5 to php4.4.2 llizards Installation/Configuration 4 14th March 2006 00:58
all my site go to /var/www/ Absolusteph Installation/Configuration 14 11th March 2006 22:27


All times are GMT +2. The time now is 03:24.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Sponsored Links: Unified Communications: Thoughts, Strategies and Predictions
Join the discussion.
www.seamlessenterprise.com

IP Convergence
Integrate your wireless and wireline networks.
Learn how from the experts at Sprint.
www.seamlessenterprise.com

Wireless & Wireline Integration
Thoughts, strategies and solutions: join the discussion
www.seamlessenterprise.com

Unified Communications 2009
Join the Discussion. Now.
www.seamlessenterprise.com

Red Hat Virtual Experience - a free virtual event. Dec. 9th