View Full Version : How can I save this image?

03-08-2010, 08:05 AM

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:

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

$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 :)


03-08-2010, 02:21 PM
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:

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, '" />';

03-09-2010, 05:49 PM
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:

$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
<img src=\"$url\" width='300px' height='200px' alt='Please wait while thumbnail loads ...'>
<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 (http://www.expert-world.com/sys/pdc_detail.php?pd=2340&)

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.

03-10-2010, 09:56 AM
Any more suggestions on this one ...

I am still stuck :(


03-10-2010, 02:33 PM
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.

03-10-2010, 02:38 PM
Are you using their free or paid service?

03-10-2010, 05:34 PM
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 ?


03-10-2010, 09:01 PM
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:


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 . md5( IMAGE_URL ) );

// Ensure the source image exists and does not need re-caching
if ( ! is_file( IMAGE_PATH ) || filemtime( IMAGE_PATH ) < strtotime( '-1 week' ) )

// At this point we have a file, so display it!
header( 'Content-type: image/jpeg' );
header( 'Content-length: ' . filesize( IMAGE_PATH ) );
header( 'Content-disposition: attachment; filename="' . basename( IMAGE_PATH ) . '.jpg"');
readfile( IMAGE_PATH );

Then you'd call it like so:

<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.

03-10-2010, 10:52 PM
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 :)

08-31-2010, 08:59 PM
If you are looking for thumbnail generation service, try sitethumbshot.com. It is a good website and very reliable.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum