URL2PNG Library for Codeigniter

Creating screenshots on the fly is fly. Here is an example in action.

Here is a simple plugin for using the URL2PNG service in Codeigniter.

First create a config file at config/url2png.php:

$config['url2png_apikey'] = 'put your api key here'; 
$config['url2png_secret'] = 'put your secret key here';
$config['url2png_size'] = '300x300'; // w x h in pixels 

Then create a library file at libraries/Url2png.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Url2png API integration.
 *
 * @package    CodeigniterUrl2png
 * @author     Ronny EW
 */
class Url2png {

    var $url2pngurl; // the api url
    var $apikey; // from url2png
    var $secretkey; // from url2png
    var $size; // something like 300x300
  
    
    /**
     * Connect to Url2PNG and display or save an image.
     * 
     */
    public function Url2png() {
	// Get CI Instance
	$this->CI = &get_instance();
		
	$this->CI->load->config('url2png');
		
        $this->apikey = $this->CI->config->item('url2png_apikey');
        $this->secretkey = $this->CI->config->item('url2png_secret');
        $this->size = $this->CI->config->item('url2png_size');
        $this->url2pngurl = "http://api.url2png.com/v3/";
		
	unset($this->CI);
    }

	/*
	* Get the actual url to call from url2png
	* @param url
	* @param size (e.g. 300x300)
	*/
	function geturl($url = "",$size = "")
	{
		if (trim($size) == "") { $size = $this->size; }
		if (trim($url) == "") { return false; }
		
		$url = str_replace('%','%25',$url);
		$url = str_replace(' ','%20',$url);
		$url = str_replace('&','%26',$url);
		$url = str_replace('#','%23',$url);
		$url = str_replace('?','%3F',$url);
	
		$token = md5($this->secretkey."+$url");
		return $this->url2pngurl.$this->apikey."/".$token."/".$size."/".$url;
	}
	 
	/*
	* Display an image with image tags
	* @param url
	* @param size (e.g. 300x300)
	*/
	function display_image($url = "",$size = "")
	{
		$src = $this->geturl($url,$size);
		$img_tag = ($src) ? sprintf('<img src="%s" title="%s">', $src, $url) : false;
		return $img_tag;
	}
	
	/*
	* Generate an image and save it to disk
	* @param url
	* @param fullpath (e.g. /join/images/12345.png)
	* @param size (e.g. 300x300)
	*/
	function save_image($url = "", $size = "", $fullpath = ""){
		
		// get the image path
		$img = $this->geturl($url,$size);
		if ($img)
		{
			$ch = curl_init ($img);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
			$rawdata=curl_exec($ch);
			curl_close ($ch);
			if(file_exists($fullpath)){
				unlink($fullpath);
			}
			$fp = fopen($fullpath,'w+');
			fwrite($fp, $rawdata);
			fclose($fp);
			
			if(file_exists($fullpath)){
				return $fullpath;
			}
		}
		return false;
	}

}

?>

Then add this code to your controller construct:

$this->load->library('Url2png');

Then use this method in the controller to generate the HTML required to display a thumbnail:

$this->url2png->display_image($url,$size);
Where:
$url is a valid URL
$size is the image dimensions (e.g. 300x300)

E.G. $this->url2png->display_image("http://www.google.com","300x300");

Use this method to return the URL (without HTML):

$this->url2png->geturl($url,$size);

Use this method to save it to your server drive:

$this->url2png->save_image($url, $size, $fullpath);
Where:
$fullpath is the path and name of file you want to save it as (e.g. /images/1234.png)

4 Replies to “URL2PNG Library for Codeigniter”

Comments are closed.