Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,929
    Thanks
    15
    Thanked 229 Times in 229 Posts

    Usint Imagemagick identify to build a javascript array with PHP.

    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 Code:

    <?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 $i count($imagelist)-$i++ )
    {
    $data $imagelist[$i];
    print 
    "
    imagelist.push(\""
    .$path.$data."\");
    "
    ;
    }

    ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Should be easier, and much safer to use getimagesize(). It's not the exact same thing, but you should get all relevant info from the function.

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    People are just keen to use shell_exec. Looks much cooler than getimagesize!!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by idalatob View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,929
    Thanks
    15
    Thanked 229 Times in 229 Posts
    Quote Originally Posted by Fou-Lu View Post
    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 Code:
    <?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 Code:


    <?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\" />";

    ?>
    Last edited by DrDOS; 09-17-2010 at 07:16 PM. Reason: Found a good working example of getimagesize.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •