How to Capture Screenshot of a Website Using PHP

Sometime we need to capture the screenshot of a website. In this tutorial we will learn how to capture screenshot of a website using php. We will user ScreenshotLayer to capture screenshot of a website. In this we will use PHP class “screenshot.class.php”. It will send an web request to ScreenshotLayer API to grab the website screenshot in JPG, GIF or PNG formats.

Get ScreenshotLayer API key:

Now you need the api key to access this class code. Using this api key an HTTP request send to screenshotLayer service to capture the screen. First you have to login to the website and choose the plan as per requirement. after login you will get an API key that you can use further to capture the screenshot of the website.

To get api key SignUp here: https://screenshotlayer.com/product

Use ScreenShotLayer Php Class code:

In the below screenshot.class.php code, Just use you own API-KEY in this file and include this file in your screen capture sample.php file.

<?php 

class screenShot{ 
    
    //********************************************************* 
    // Settings 
    //********************************************************* 
    
    //Your screenshotLayer API key 
    //Available at https://screenshotlayer.com/product 
    private $apiKey = 'YOUR_API_KEY_HERE'; 
    
    //API endpoint 
    //only needs to change if the API changes location 
    private $endPoint = 'http://api.screenshotlayer.com/api/capture'; 
    
    //Secret Keyword defined in your screenshotLayer dashboard 
    //leave blank if you have not activated this feature 
    private $secretKey = ''; 
    
    //API key/value pair params 
    public $params = array(); 
    
    //response capture 
    public $capture; 
    
    //image info 
    public $imageInfo; 
    
    /* 
    method: class construction 
    usage: screenShot([string url]); 
    params: url = URL to web video 
    
    The screenshotLayer API requires a valid webpage URL to capture. 
    
    returns: null 
    */ 
    public function __construct($url='',$format='PNG'){ 
        
        $this->params['url'] = $url; 
        if( !empty($this->secretKey) ){ 
            
            $secret = md5($url.$this->secretKey); 
            $this->params['secret_key'] = $secret; 
            
        } 
        
    } 
    
    /* 
    method: displayImage 
    usage: displayImage(void); 
    params: none 
    
    This method will write an image tag with the correct request url. 
    Note: this method will not verify the request is to a valid url and 
    will return a broken image if not. 
    
    returns: null 
    */ 
    public function displayImage(){ 
        
        $request = $this->buildRequest(); 
        
        echo '<img src="'.$request.'">'; 
        
    } 
    
    /* 
    method: captureScreen 
    usage: captureScreen(void); 
    params: none 
    
    This method will query the api for the binary code of the captured webpage. 
    
    returns: null 
    */ 
    public function capturePage(){ 
        
        $request = $this->buildRequest(); 
        
        $this->capture = file_get_contents($request); 
        
        $this->imageInfo = getimagesizefromstring($this->capture); 
        
        if( empty($this->imageInfo) ){ 
            
            if( empty($this->capture) ){ 
                
                throw new Exception('An unknown error has occured'); 
                
            }else{ 
                
                $response = json_decode($this->capture); 
                
                throw new Exception($response->error->info); 
                
            } 
            
        } 
        
    } 
    
    /* 
    method: downloadCapture 
    usage: downloadCapture([string fileName='']); 
    params: fileName = The name of the file written to disk 
    
    This method will download the captured image to the client. 
    
    returns: null 
    */ 
    public function downloadCapture($fileName=''){ 
        
        $fileName = ( empty($fileName) ) ? 'capture' : $fileName; 
        
        if( empty($this->capture) ){ 
            
            throw new Exception('No image has been captured'); 
            
        } 
        
        header('Content-Type: '.$this->imageInfo['mime']); 
        header('Content-Disposition: attachment; filename="'.$fileName.'"'); 
        header('Content-Transfer-Encoding: binary'); 
        
        echo $this->capture; 
        
    } 
    
    /* 
    method: displayCapture 
    usage: displayCapture(void); 
    params: none 
    
    This method will display the captured image to the browser. 
    
    returns: null 
    */ 
    public function displayCapture(){ 
        
        if( empty($this->capture) ){ 
            
            throw new Exception('No image has been captured'); 
            
        } 
        
        header('Content-Type: '.$this->imageInfo['mime']); 
        
        echo $this->capture; 
        
    } 
    
    /* 
    method: buildRequest 
    usage: buildRequest(void); 
    params: none 
    
    This method will build the api request url. 
    
    returns: api request url 
    */ 
    public function buildRequest(){ 
        
        if( empty($this->params['url']) ){ 
            
            throw new Exception('API requires URL to video'); 
            
        } 
        
        $request = $this->endPoint.'?access_key='.$this->apiKey; 
        
        foreach( $this->params as $key=>$value ){ 
            
            if( $key == 'url' ){ 
                
                $request .= '&url='.urlencode($value); 
                
            }else{ 
                
                $request .= '&'.$key.'='.$value; 
                
            } 
            
        } 
        
        return $request; 
        
    } 
    
    /* 
    method: setParam 
    usage: setParam(string key, string value); 
    params: key = key of the params key/value pair 
             value = value of the params key/value pair 
    
    add or change the params key/value pair specified. 
    
    returns: null 
    */ 
    public function setParam($key,$value){ 
        
        $this->params[$key] = $value; 
        
    } 
} 
?>

Write PHP code to Capture Screen

After that use the below mentioend code to capture the screen. Follow below “sample.php” file code for this. we can create an object of the screenshot class by passign the url of the website, which we want to capture.

<?php 

//include screenShot class 
include('screenshot.class.php'); 

//use own website url 

$screenShot = new screenShot('https://atcodex.com/'); 

//display html image tag for captured webpage 

$screenShot->displayImage(); 


//$screenShot->capturePage(); 

//$screenShot->downloadCapture('test.png'); 

//$screenShot->displayCapture(); 

?>

Run the code and you will the get screenshot of the website. ScreenshotLayer stores your image for 30 days only untill you capture again.

You can also check complete ScreenshotLayer API documentation here: https://screenshotlayer.com/documentation.

There are multple tools and library available out there to capture the screenshot of the website. But personally i like this very easy to integrate on any website.

Recommended Posts For You

You can also visit the following related posts