Created
October 20, 2012 22:49
-
-
Save elchappo/3925107 to your computer and use it in GitHub Desktop.
PHP resizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$resizer = new Resizer(); | |
$resizer->setRequestFileInfo($_GET['path']); | |
$resizer->setRequestHight($_GET['hight']); | |
$resizer->setRequestWidth($_GET['width']); | |
$resizer->setRequestMode($_GET['mode']); | |
$resizer->generateImage(); | |
/** | |
* | |
*/ | |
class Resizer { | |
/* | |
* Original image requested | |
*/ | |
private $requestFileInfo = null; | |
/* | |
* Reguested Image width | |
*/ | |
private $requestWidth = null; | |
/* | |
* Reguested Image Hight | |
*/ | |
private $requestHight = null; | |
/* | |
* Requested operation mode | |
*/ | |
private $requestMode = null; | |
/* | |
* Allowed request extensions | |
*/ | |
private $allowedExtensions = array('jpg', 'gif', 'png'); | |
/** | |
* Allowed request values | |
* @var array | |
*/ | |
private $allowedSizesRange = array( | |
'hight' => array( | |
'min' => 20, | |
'max' => 1000 | |
), | |
'width' => array( | |
'min' => 20, | |
'max' => 1000 | |
) | |
); | |
/** | |
* Secret used to generate image name | |
* @var string | |
*/ | |
private $secret = 'dog%under&%desk'; | |
/** | |
* Response max-age header | |
* @var integer | |
*/ | |
private $maxAge = 0; | |
/** | |
* Response Access-Controll-Origin header | |
* @var string | |
*/ | |
private $accessControlAllowOrigin = '*'; | |
/** | |
* Document root | |
* @var string | |
*/ | |
private $baseDirPath = null; | |
/** | |
* Cache folder path | |
* @var string | |
*/ | |
private $cacheDir = 'cache'; | |
/** | |
* Cache path | |
* @var string | |
*/ | |
private $cacheDirPath = null; | |
/** | |
* Cache file path | |
*/ | |
private $cacheFileName = null; | |
/** | |
* Response stat | |
* @var string | |
*/ | |
private $stat = 'MISS'; | |
/** | |
* Response generation time | |
* @var time s | |
*/ | |
protected $statTime = null; | |
/** | |
* Class construct | |
* Check if file modified since first, and set script paths | |
*/ | |
public function __construct() { | |
$this->statTime = new Timer(); | |
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { | |
header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304); | |
exit; | |
} else { | |
$this->baseDirPath = dirname(__FILE__); | |
$this->cacheDirPath = $this->appendBaseDir($this->cacheDir); | |
} | |
} | |
/** | |
* @todo | |
*/ | |
public function convertImage() { | |
$this->checkInCache(); | |
} | |
/** | |
* This finction resize will resize image | |
* @todo lock file ? | |
*/ | |
public function resizeImage() { | |
$original = $this->getOriginalFilePath(); | |
if (!file_exists($original)) { | |
header("Status: 404 Not Found"); | |
echo "The original file " . $this->getOriginalFilePath() . " doesnot not exist"; | |
exit; | |
} else { | |
try { | |
$image = new Imagick($original); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
//ping the image | |
$image->pingImage($original); | |
//read image | |
$image->readImage($original); | |
if ($this->getRequestMode() == 0) { | |
//preserve aspect ratio | |
$image->thumbnailImage($this->getRequestWidth(), FALSE); | |
} else { | |
$image->thumbnailImage($this->getRequestWidth(), $this->getRequestHight(), FALSE); | |
} | |
//write the thumbnail to disk | |
$image->writeImage($this->cacheFileName); | |
$image->destroy(); | |
} | |
} | |
public function generateImage() { | |
if(!$this->checkInCache()){ | |
$this->stat = 'HIT'; | |
$this->resizeImage(); | |
} | |
header("Status: 200 Found"); | |
header("X-Cache: " . $this->stat); | |
header("X-Time: " . $this->statTime->getElapsedTime()); | |
header("Cache-Control: private, max-age=" . $this->maxAge . ", pre-check=10800"); | |
header("Pragma: private"); | |
header("Expires: " . date(DATE_RFC822, strtotime("+{$this->maxAge} seconds")."s")); | |
header("Content-type: image/" . $this->getExtension()); | |
header("Content-Length: " . filesize($this->cacheFileName)); | |
header("Last-Modified: " . date(DATE_RFC822, filemtime($this->cacheFileName))); | |
header("Access-Control-Allow-Origin: $this->accessControlAllowOrigin"); | |
echo file_get_contents($this->cacheFileName); | |
exit; | |
} | |
private function checkInCache() { | |
if (file_exists($this->generateCacheName())) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
private function validateRange($val, $max, $min) { | |
if ($val < $min || $val > $max) { | |
header("Status: 404 Not Found"); | |
echo "Sorry size not allowed."; | |
exit; | |
} else { | |
return $val; | |
} | |
} | |
private function validateExtension($file) { | |
if (!in_array($file['extension'], $this->allowedExtensions) || strpos($file['filename'], '.') !== FALSE) { | |
header("Status: 404 Not Found"); | |
echo "Sorry Extension not allowed."; | |
exit; | |
} else { | |
return $file; | |
} | |
} | |
private function generateCacheName() { | |
$this->cacheFileName = $this->cacheDirPath . DIRECTORY_SEPARATOR . $this->encodeName(); | |
return $this->cacheFileName; | |
} | |
private function appendBaseDir($string) { | |
return $this->baseDirPath . DIRECTORY_SEPARATOR . $string; | |
} | |
private function encodeName() { | |
return md5($this->secret . $this->getOriginalFilePath() . $this->getRequestWidth() . $this->getRequestHight() . $this->getRequestMode()); | |
} | |
private function getOriginalFilePath() { | |
$fileInfo = $this->getRequestFileInfo(); | |
return $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['basename']; | |
} | |
private function getExtension() { | |
return $this->requestFileInfo['extension']; | |
} | |
private function getRequestFileInfo() { | |
return $this->requestFileInfo; | |
} | |
public function setRequestFileInfo($requestPath) { | |
$this->requestFileInfo = $this->validateExtension(pathinfo($requestPath)); | |
} | |
public function getRequestWidth() { | |
return $this->requestWidth; | |
} | |
public function setRequestWidth($requestWidth) { | |
$this->requestWidth = $this->validateRange((int) $requestWidth, $this->allowedSizesRange['width']['max'], $this->allowedSizesRange['width']['min']); | |
} | |
public function getRequestHight() { | |
return $this->requestHight; | |
} | |
public function setRequestHight($requestHight) { | |
$this->requestHight = $this->validateRange((int) $requestHight, $this->allowedSizesRange['hight']['max'], $this->allowedSizesRange['hight']['min']); | |
} | |
public function getRequestMode() { | |
return $this->requestMode; | |
} | |
public function setRequestMode($requestMode) { | |
$this->requestMode = (int) $requestMode; | |
} | |
} | |
class Timer { | |
protected $_time; | |
public function __construct() { | |
$this->resetTimer(); | |
} | |
public function resetTimer() { | |
$this->_time = microtime(true); | |
} | |
public function getElapsedTime($resetTimer = false) { | |
$difference = sprintf("%1.3f", microtime(true) - $this->_time); | |
if ($resetTimer) { | |
$this->resetTimer(); | |
} | |
return $difference; | |
} | |
public function __toString() { | |
return $this->getElapsedTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment