How do I do this in PHP?

Discussion in 'Programming/Scripts' started by edge, Jun 21, 2006.

  1. edge

    edge Active Member Moderator

    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.

    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?
  2. sjau

    sjau Local Meanie Moderator

    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:

    So you could build a script like this:

  3. edge

    edge Active Member Moderator

    Thank you... The code looks nice :)

    I was just reading about the explode option in PHP, so I made this code;

    $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
  4. sjau

    sjau Local Meanie Moderator

    well, if the exec functions returns an array then there is no need for exploding ^^
  5. edge

    edge Active Member Moderator

    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..?
  6. sjau

    sjau Local Meanie Moderator

    well, after you explode at " " then it's the second element $result[1] ^^
  7. falko

    falko Super Moderator Howtoforge Staff

Share This Page