Skip to content

Instantly share code, notes, and snippets.

@ThePixelDeveloper
Created December 31, 2009 23:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThePixelDeveloper/266955 to your computer and use it in GitHub Desktop.
Save ThePixelDeveloper/266955 to your computer and use it in GitHub Desktop.
<?php
/**
* Description
* Smush.it uses optimization techniques specific to image format to remove
* unnecessary bytes from image files. It is a "lossless" tool, which means it
* optimizes the images without changing their look or visual quality. After
* Smush.it runs on a web page it reports how many bytes would be saved by
* optimizing the page's images and provides a downloadable zip file with the
* minimized image files.
*
* @license GPL
* @author Mathew L Davies <thepixeldeveloper@googlemail.com>
* @todo Add local file support.
*/
class smushit {
/**
* User Agent string
*/
const user_agent = 'Smush.it PHP Class (+http://mathew-davies.co.uk)';
/**
* Smush.it request URL
*/
const url = 'http://www.smushit.com/ysmush.it/ws.php';
/**
* cURL handle
*/
private $curl = NULL;
/**
* Make sure any prerequisite are installed.
*/
public function __construct() {
if ( ! extension_loaded( 'json' ) ) {
throw new RuntimeException('The json extension was not found');
}
if ( ! extension_loaded( 'curl' ) ) {
throw new RuntimeException('The cURL extension was not found.');
}
// cURL handler
$this->curl = curl_init();
// Return HTTP response
curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, TRUE );
}
/**
* Compress image using smush.it. Image must be available online
*
* @param string url to image.
* @throws Smush_exception
* @return object
*
* src = source location of input image
* src_size = size of the source image in bytes
* dest = temporary location of the compressed image
* dest_size = size of compressed image in bytes
* percent = how much smaller the compressed image is
*/
public function compress($image) {
// Set appropriate URL.
curl_setopt( $this->curl, CURLOPT_URL, self::url.'?'.http_build_query( array('img' => $image ) ) );
// Set user agent
curl_setopt( $this->curl, CURLOPT_USERAGENT, self::user_agent );
// Execute the HTTP request
$request = curl_exec($this->curl);
// JSON response
$result = json_decode($request);
if ( isset ( $result->error ) ) {
throw new Smush_exception($result->error, $image);
}
$result->dest = urldecode($result->dest);
// Return response data
return $result;
}
}
/**
* Custom exception handler
*/
class Smush_exception extends Exception {
/**
* Path to image
* @var string $image
*/
private $image = '';
/**
* Overload the exception construct so we can provide an image name
* @param string $message
* @param string $image
*/
public function __construct($message, $image) {
$this->image = $image;
parent::__construct($message);
}
// Return image path.
final function getImage() {
return $this->image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment