├── README.markdown └── class.smushit.php /README.markdown: -------------------------------------------------------------------------------- 1 | smushit-php 2 | ========= 3 | 4 | A PHP wrapper around Yahoo!'s [Smush.it image compression web service](http://www.smushit.com/ysmush.it/). 5 | 6 | Given a URL to an image or a local image filename, this class will losslessly compress the image using Yahoo!'s Smush.it service and return the URL to the smushed image along with its meta information. It can optionally overwrite the local image file. 7 | 8 | Much love to the Yahoo! [Exceptional Performance group](http://tech.groups.yahoo.com/group/exceptional-performance/) for a wonderful service :-) 9 | 10 | EXAMPLES 11 | -------- 12 | 13 | $img = new SmushIt('http://some-domain.com/image.jpg'); 14 | print_r($img); // Will output... 15 | // SmushIt Object 16 | // ( 17 | // [filename] => 18 | // [url] => http://some-domain.com/image.jpg 19 | // [compressedUrl] => http://smushit.zenfs.com/results/dcd75d06/smush/image.jpg 20 | // [size] => 42517 21 | // [compressedSize] => 23063 22 | // [savings] => 45.76 23 | // [error] => 24 | // ) 25 | 26 | UPDATES 27 | ------- 28 | 29 | Code is hosted at GitHub: [http://github.com/tylerhall/smushit-php](http://github.com/tylerhall/smushit-php) 30 | 31 | LICENSE 32 | ------- 33 | 34 | The MIT License 35 | 36 | Copyright (c) 2009 Tyler Hall 37 | 38 | Permission is hereby granted, free of charge, to any person obtaining a copy 39 | of this software and associated documentation files (the "Software"), to deal 40 | in the Software without restriction, including without limitation the rights 41 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 42 | copies of the Software, and to permit persons to whom the Software is 43 | furnished to do so, subject to the following conditions: 44 | 45 | The above copyright notice and this permission notice shall be included in 46 | all copies or substantial portions of the Software. 47 | 48 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 49 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 50 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 51 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 52 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 53 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 54 | THE SOFTWARE. -------------------------------------------------------------------------------- /class.smushit.php: -------------------------------------------------------------------------------- 1 | 6 | // http://github.com/tylerhall/smushit-php/tree/master 7 | 8 | class SmushIt 9 | { 10 | const SMUSH_URL = 'http://www.smushit.com/ysmush.it/ws.php?'; 11 | 12 | public $filename; 13 | public $url; 14 | public $compressedUrl; 15 | public $size; 16 | public $compressedSize; 17 | public $savings; 18 | public $error; 19 | 20 | public function __construct($data = null) 21 | { 22 | if(!is_null($data)) 23 | { 24 | if(preg_match('/https?:\/\//', $data) == 1) 25 | $this->smushURL($data); 26 | else 27 | $this->smushFile($data); 28 | } 29 | } 30 | 31 | public function smushURL($url) 32 | { 33 | $this->url = $url; 34 | 35 | $ch = curl_init(); 36 | curl_setopt($ch, CURLOPT_URL, self::SMUSH_URL . 'img=' . $url); 37 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 38 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 39 | $json_str = curl_exec($ch); 40 | curl_close($ch); 41 | 42 | return $this->parseResponse($json_str); 43 | } 44 | 45 | public function smushFile($filename) 46 | { 47 | $this->filename = $filename; 48 | 49 | if(!is_readable($filename)) 50 | { 51 | $this->error = 'Could not read file'; 52 | return false; 53 | } 54 | 55 | $ch = curl_init(); 56 | curl_setopt($ch, CURLOPT_URL, self::SMUSH_URL); 57 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 58 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 59 | curl_setopt($ch, CURLOPT_POST, true); 60 | curl_setopt($ch, CURLOPT_POSTFIELDS, array('files' => '@' . $filename)); 61 | $json_str = curl_exec($ch); 62 | curl_close($ch); 63 | 64 | return $this->parseResponse($json_str); 65 | } 66 | 67 | private function parseResponse($json_str) 68 | { 69 | $this->error = null; 70 | $json = json_decode($json_str); 71 | 72 | if(is_null($json)) 73 | { 74 | $this->error = 'Bad response from Smush.it web service'; 75 | return false; 76 | } 77 | 78 | if(isset($json->error)) 79 | { 80 | $this->error = $json->error; 81 | return false; 82 | } 83 | $this->filename = substr (strrchr ($json->src, '/'), 1 ); 84 | $this->size = $json->src_size; 85 | $this->compressedUrl = rawurldecode ($json->dest); 86 | $this->compressedSize = $json->dest_size; 87 | $this->savings = $json->percent; 88 | return true; 89 | } 90 | } --------------------------------------------------------------------------------