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 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How can I save this image?

    Hi,

    I am using snapcasa.com to provide me with an image.

    They encourage you to cache the images on your own server so I am trying to do that.

    When I use this code to display the image it works fine:

    PHP Code:
    <img src="http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url\" width='300px' height='200px' alt='Thumbnail for $title_sht'> 
    Now, I want to save this image locally so I tried this:

    PHP Code:
    $image_data file_get_contents("http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url");
    file_put_contents($image_file$image_data); 
    When I do the above I don't get the real image returned but only their
    default "Unregistered Domain" image.

    This maybe an anti-hammer reaction because of the quick succession of my image requests.

    So since I already have the image displayed using the <img src=
    tag, is there a way to save this image locally ?

    If not, maybe I should use the file_get_contents() function to get the image data in the first place ?

    Would appreciate any input on this



    .
    Last edited by jeddi; 03-08-2010 at 07:07 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You make it sound like you're first displaying their image link, as well as re-accessing it for caching.

    Cache their image locally first... then use the cached version for display, not theirs.

    I'd even store a datetime with the image so you know when it was cached, then maybe re-cache it once a week or so. That way you're only hitting their server once a week for each URL you need.

    Some pseudo-code:
    PHP Code:
    if ( ! $site_is_cached || $site_cache_is_over_one_week_old )
    {
        
    $cache_result set_site_image_cache$site_url );
    }

    $site_image get_site_image_cache$site_url );

    echo 
    '<img src="/cached_site_images/'$site_image'" />'
    Last edited by kbluhm; 03-08-2010 at 08:31 PM.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Yes, you are right, I had got the code
    round the wrong way.

    I have now changed it to grab the file first and then
    display it.

    But I still can not get the image using get_file_contents function.

    This is my code:
    PHP Code:
    $url "http://snapcasa.com/get.aspx?code=9360&size=l&url=$Db_url";
    $image_data file_get_contents($url);
    file_put_contents($image_file$image_data);
    echo 
    "<img src=\"$image_file\"  width='300px' height='200px' alt='Please wait while thumbnail loads ...'>
    <br><br>The Paydotcom Product Code: $Db_prod
    <br><br>Image Url: $url
    <br><br>
    <img src=\"$url\"  width='300px' height='200px' alt='Please wait while thumbnail loads ...'>
    <br><br>
    <span style = \"font-size:18px; color:darkblue;\"><b>Customer Payments Schedule:<br>$price</b></span> 
    I think it proves that the url is fine:

    The result can be seen for yourself:

    Test Page

    You will see the file_get_contents function attempt
    in a box with just the ALT showing followed by the immage using the
    same url but in the img tag.

    I hope you can see a fault with my script ;-)

    If not, how else can I save the file ?


    Thanks for your help.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Any more suggestions on this one ...


    I am still stuck



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    It sounds to me like they have some sort of checking on who's grabbing their images, and they haven't added your host to the whitelist.
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Are you using their free or paid service?

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    I am using the free service.

    And I have done what they require,

    I have registered my website domain and
    my IP address with them.

    Maybe the file_get_contents function is
    kind of annonymous compared with the <img src=\ tag ?




    .
    Last edited by jeddi; 03-10-2010 at 04:37 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here is what I'm thinking. Create a folder calls /thumbs/ in a root folder, and in that folder create another called /images/ and make it writable. Then place an index.php file in there that looks like this:
    PHP Code:
    <?php

    if ( ! isset( $_GET['url'] ) )
    {
        exit( 
    'No URL specified.' );
    }

    // Some constants... could easily be variables if you wish
    define'SNAPCASA'  'http://snapcasa.com/get.aspx?code=9360&size=l&url=' );
    define'IMAGE_URL' $_GET['url'] );
    define'IMAGES_DIR'dirname__FILE__ ) . DIRECTORY_SEPARATOR 'images' DIRECTORY_SEPARATOR );
    define'IMAGE_PATH'IMAGES_DIR md5IMAGE_URL ) );

    // Ensure the source image exists and does not need re-caching
    if ( ! is_fileIMAGE_PATH ) || filemtimeIMAGE_PATH ) < strtotime'-1 week' ) )
    {
        
    copyIMAGE_PATHSNAPCASA IMAGE_URL );
    }

    // At this point we have a file, so display it!
    header'Content-type: image/jpeg' );
    header'Content-length: ' filesizeIMAGE_PATH ) );
    header'Content-disposition: attachment; filename="' basenameIMAGE_PATH ) . '.jpg"');
    readfileIMAGE_PATH );
    Then you'd call it like so:
    Code:
    <img src="/thumbs/index.php?url=www.google.com" />
    If copy() doesn't work, you could always go the CURL route and see if that does the trick, but I see no imminent issues with this code.

    This is a very simple example, and I'm unfamiliar with SnapCasa's API. If they throw any error codes at you that would help in displaying errors or default images, then CURL would definitely be the way to go in order to capture any responses.

    If you know what image format you're receiving (jpeg, gif, etc), you'll want to set the respective content type using header( 'Content-type: image/xxx' ) just before readfile(), where xxx is the image format.
    Last edited by kbluhm; 03-10-2010 at 08:09 PM.

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Well ,

    That sure looks interesting.

    Where I am its about midnight, so I off to bed !

    I'll give it a try in the morning
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #10
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are looking for thumbnail generation service, try sitethumbshot.com. It is a good website and very reliable.


  •  

    Posting Permissions

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