Different sizes download button

Hi,
I have a requirement from a client, he needs two buttons in the page for two different sizes of the current image, those two buttons would allow the user to download such images, different from the original image size
I'm using a custom template, in there, I used getCustomImageURL() for both links; the returned value from getCustomImageURL is sent to a custom php script which sends the image to the browser.
It works great, and the user can download the two different sizes; BUT ONLY if those two sizes had been viewed before, that is, if they are in the cache already. If the image was viewed at the custom size, I get the URL to the cache file, but if not, I get the URL to the image processor
How can I make getCustomImageURL return the URL only after the image processor has finished with it (in cache) ?

Using ZPhoto 1.4

Thanks for any pointer to a solution

Nelson.

Comments

  • acrylian Administrator, Developer
    Make sure the images are already cached when accessed. Use the cache_manager plugin to do that.

    Note that 1.4 is nowhere near the current release version so you should respectively might need to upgrade.
  • You could have your script run a background request for the images via an "ajax" request when the page loads if the image is not already in the cache.

    You can find an example of this in the development build `html_metatags.php` script. Do a search on the variable `$htmlmetatags_need_cache` for the details.
  • acrylian Administrator, Developer
    True, that is the more sophisticated way...;-)
  • Thank you acrylian for the prompt response, yes, I'm a little behind in the updates, currently on v1.4.1.6
    Don't know if I asked the wrong question, but the problem I'm facing is none of the images are in the cache, since they are custom sized
    how can I get a custom size image on the fly? is your answer the solution? using the plugin to force-generate those custom sized images?
    Thanks
  • sbillard, thank you very much for the idea; you are right, AJAX was the step I was missing in my current process
    Thank you again

    Nelson.
  • For future reference ...
    At the end I made an AJAX call to generate the images in cache, but had to build the URL to the cached images (used in the anchor tag), because I found no way to run the getCustomImageURL function after the AJAX call completed (Don't know yet how to call a template function from JavaScript).

    I did it like this:

    `
    //get the URL to the resized image
    $HR_size=2000;
    $HR=getCustomImageURL($HR_size);

    //then I made the AJAX call

    function requestHR(){
    $.ajax({
    cache:false,
    type:"POST",
    url:'<?php echo $HR ?>'
    });
    }
    $(document).ready(function() {
    requestHR();
    });



    //then I construct the URL to the cached image:
    <?php
    $cache_path=implode ('cache', explode ('albums', $_zp_current_image->webpath));
    $hrPath=implode('_' . $HR_size . '.', explode('.', $cache_path));
    ?>

    //I use that path in the link
    ">high res

    The code for downloading the photo is pretty simple:

    <?php
    $dir = SERVERPATH . $_GET['path_to_image'];
    header('Pragma: public');
    header('Cache-Control: public, no-cache');
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . filesize($dir));
    header('Content-Disposition: attachment; filename="' . basename($dir) . '"');
    header('Content-Transfer-Encoding: binary');

    readfile($dir);
    ?>
    `
    Hope this helps somebody else, or anybody with a better solution should correct my mistakes

    Nelson.
  • acrylian Administrator, Developer
    Since JS and PHP are different you cannot call a PHP function directly via JS.
    You probably could use another ajax call to get the new cached url.

    Generally you can for example call "custom theme pages" (those have the url `/page/` where <pagename> the file name minus .php extension. That could contain a function that then returns the new url via ajax as well.

    Regarding the download, we also have a downloadlist plugin that may be of help for the download (it does bascially what you do but it also has download statistics available).
  • In the example I referenced you will see PHP code testing for the uri being to the image processor and saving it into an array. Then at the end of the script, if it did find some images needing caching it generates some javascript code to do the caching. Among the code is a definition of a js array containing the links needing to be fetched and a loop doing an ajax request for each of the elements in that array.
Sign In or Register to comment.