Search code examples
phpscreenshot

To show default image if screenshot of webpage captured using third party fails


What i am trying to do is to capture screenshot of URL submitted by user and using http://open.thumbshots.org/image.aspx?url=http://google.com . The problem is, if for any reason screenshot don't get captured(as user can enter invalid url), what i get is default image of open.thumbshots.org saying "thumbshots.com Image coming soon" . How can i show my default image instead of that image in case of failure?

$url = $_POST['user_url'];

and html

 <img src="http://open.thumbshots.org/image.aspx?url=<?php echo $url;?>" width = "180" height = "150" />

Here i need to decide before showing image, if returned image is default image of thumbshots, display my default image


Solution

  • I am not entirely familiar with this particular service, and you haven't given us any code you are using to go on, but my guess is you have a few options:

    1. check the "expires" and/or "cache-control" headers it seems like the default image is set to expire after 60 seconds for the default image, and 604800 seconds (or some larger value) fo a valid image.
    2. keep a copy of the default image locally, and compare it to the received image if the image you keep locally is is the same as the returned, you know you have the default image
    3. Do (1) and (2)
    4. Use another service, or do it yourself. For example, CutyCapt (see also here)

    Personally, I really like CutyCapt because I can guarantee it's performance on my own servers and it's pretty easy to use. But I don't know what your setup is like or what you've tried already, so it may not be possible for you.

    If you are set on using thumbshots.org, I would suggest checking the headers, as that seems pretty consistently correct (although I've admittedly spent very little time checking).