Skip to content

Instantly share code, notes, and snippets.

@danvine
Created July 11, 2012 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvine/3087544 to your computer and use it in GitHub Desktop.
Save danvine/3087544 to your computer and use it in GitHub Desktop.
Simple php cacheing proxy for url2png.com
<?php
function url2png_v6($url, $args) {
# Get your apikey from http://url2png.com/plans
$URL2PNG_APIKEY = "PXXXX";
$URL2PNG_SECRET = "SXXXX";
# urlencode request target
$options['url'] = urlencode($url);
$options += $args;
# create the query string based on the options
foreach($options as $key => $value) { $_parts[] = "$key=$value"; }
# create a token from the ENTIRE query string
$query_string = implode("&", $_parts);
$TOKEN = md5($query_string . $URL2PNG_SECRET);
return "http://beta.url2png.com/v6/$URL2PNG_APIKEY/$TOKEN/png/?$query_string";
}
$options['force'] = 'false'; # [false,always,timestamp] Default: false
$options['fullpage'] = 'false'; # [true,false] Default: false
$options['thumbnail_max_width'] = 'false'; # scaled image width in pixels; Default no-scaling.
$options['viewport'] = "1280x1024"; # Max 5000x5000; Default 1280x1024
$url = (isset($_GET['url'])) ? $_GET['url'] : "http://www.google.com";
$src = url2png_v6($url, $options);
$thumbnail_id = md5($src);
if(file_exists("/var/www/thumbnails/$thumbnail_id.png") {
header("location: /thumbnails/$thumbnail_id.png");
} else {
$png_data = @file_get_contents($src);
$error = true;
foreach($http_response_header as $header) {
$error = ($header == "X-Status: OK" or $header == "X-Error: false") ? false : $error;
}
if ($error === true) {
// echo "oops!\n\n";
// print_r($http_response_header);
header("location: /thumbnails/oops.png");
} else {
// echo "yay!";
@file_put_contents("/var/www/thumbnails/$thumbnail_id.png", $png_data, LOCK_EX);
header("location: /thumbnails/$thumbnail_id.png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment