www.webdeveloper.com
Results 1 to 7 of 7

Thread: Batch image processing

  1. #1
    Join Date
    Jul 2007
    Posts
    245

    Batch image processing

    I need to create an automated process that generates the thumbnails of all the images contained in a directory (those that have no thumbnail yet). The problem is the size of these images, and their number is quite considerable. These images have between 1-5 MB.

    I said my coworkers the easiest way for this would be to batch process the images with Photoshop, but they said they prefer something which is not subjected to use Photoshop everytime a new image is added to the directory.

    I tried another way, which wasn't smart: Collecting the info of the files in an array, then looping thorugh it to make AJAX calls in order to process each one in the background. However, as you might guess, this caused the browser to freeze, and not all the images were processed, due to a timeout, I guess. I must say that I used the GD library and as far as I know, ImageMagick is quite faster.

    My question is: is there an efficient way to do this with PHP+javascript asynchronously? Something like queueing the images. And much better, with some sort of notifier for each processed image.
    I also saw once another batch processing with page refresh for each image. This could be interesting too by default.

    If you know some good tutorial for this it would be enough.
    Last edited by Jazztronik; 05-11-2012 at 04:30 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    I don't think that AJAX calls would be a problem in and of themself, but if you call a PHP script via AJAX and, say, the PHP script crashes -- such as due to memory issues due to a large number of pixels in an image -- it would never send a HTTP response, so the JavaScript on the browser would just keep waiting for a response.

    If the IMagic extension is available to you, I believe it's less of a memory problem (whatever memory it uses is not charged against PHP's max memory setting). Otherwise, you can use getimagesize() to find the width and height of an image, multiply them and then multiply that product times 4 (32bits/pixel) to get an idea how much memory will be needed. At that point you can either go ahead and process the image if you think you have enough memory, or reject it (and send an error response so you know you have to handle it manually), or perhaps alter your memory limit accordingly (e.g. via ini_set()).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2012
    Posts
    6
    hey buddy,

    you should have created a thumbnail size image before you uploaded the image (in case your image if from a form).

    use simpleImage class to resize your image.
    source: http://www.white-hat-web-design.co.u...ages-with-php/


    to create thumbnail size for your image in you directory,

    fetch your files then loop the resizing code per image.

    you can use opendir() function to do this.
    http://php.net/manual/en/function.opendir.php

    example;

    while(*condition fetching data form directory*){

    //do the resizing here

    }

  4. #4
    Join Date
    Jul 2007
    Posts
    245
    Hello jr and NogDog,

    jr.wramirez: Thanks for the tip about the SimpleImage class. Looks pretty handy. Anyway, that's not what is causing me problems. I'm using CodeIgniter and it comes with a nice image manipulation class. There is no need to upload anything in this case: the big images are dumped with an FTP client (Filezilla). They must be there in order to allow the users to download them.

    NogDog: one question that still confuses me: does PHP do multitask? I mean when Javascript (JQuery in particular) loops though the array containing the files info and makes the AJAX calls, those calls are supposed to be made in a very short time.

    Does PHP queue all these requests so as to process one by one, or start processsing them until it runs out of memory? Or in other words, does PHP process a given number of requests at the same time?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    If you send several separate AJAX requests, as far as the web server is concerned, those are all separate HTTP requests that are completely unrelated to each other, so each fires off a separate invocation of your PHP script. Those requests would not effect each other as far as the PHP max memory limit, as that is on a per-request basis -- but it could impact total server memory if they are all running at once on the server, I suppose. (I'm not enough of a server/sysadmin sort to know if this would be a real problem or not.)

    I would be inclined to send the AJAX requests sequentially rather than concurrently: send request 1, wait for the response from the server (whether success or failure), and have the AJAX callback function then trigger the next AJAX request in the queue, etc... At the very least, such an approach might make it easier to debug where the actual problem is, rather than trying to figure out which request out of n concurrent requests is the culprit.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    May 2012
    Posts
    8
    Most elegant way to do this is to loop dir and use PHP GD library (http://php.net/manual/en/book.image.php)

  7. #7
    Join Date
    Jul 2007
    Posts
    245
    Thanks NogDog! you've been very helpful. I'll do it that way.

    signorm68: thanks for trying to help but please read the thread first (at least the 1st post). Seems that you've only read the title.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles