Created
May 6, 2009 22:41
-
-
Save ioseb/107795 to your computer and use it in GitHub Desktop.
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 | |
class DynamicImage { | |
/** | |
* @var HttpRequest | |
*/ | |
private $req; | |
/** | |
* @var File | |
*/ | |
private $file; | |
private $status; | |
private $width; | |
private $height; | |
function DynamicImage() { | |
$this->req = &HttpRequest::getInstance(); | |
$this->file = null; | |
$this->status = false; | |
$this->width = 0; | |
$this->height = 0; | |
$this->init(); | |
} | |
function init() { | |
$req = &$this->req; | |
if ($path = $req->getParameter('im', null, PHPType::STR())) { | |
$file = new File($path); | |
if ($file->exists()) { | |
$this->status = true; | |
$this->file = &$file; | |
} | |
$this->width = $req->getParameter('w', 100, PHPType::INT()); | |
$this->height = $req->getParameter('h', 100, PHPType::INT()); | |
} | |
} | |
function write() { | |
if ($this->file && $this->status) { | |
$info = getimagesize($this->file->getAbsolutePath()); | |
$extHandler = $this->getImageHandler($info['mime']); | |
$imageFunction = 'imagecreatefrom' . $extHandler; | |
$imageHandler = 'image' . $extHandler; | |
$image = $imageFunction($this->file->getAbsolutePath()); | |
if (function_exists('imagecreatetruecolor')){ | |
$thumb = imagecreatetruecolor($this->width, $this->height); | |
} else { | |
$thumb = imagecreate($this->width, $this->height); | |
} | |
imagecopyresized($thumb, $image, 0, 0, 0, 0, $this->width, $this->height, ImageSX($image), ImageSY($image)); | |
//$image_handler($thumb, '', 100); | |
header('Content-type:image/' . $extHandler); | |
header('Cache-Control: no-store, no-cache, must-revalidate'); | |
header('Cache-Control: post-check=0, pre-check=0', false); | |
$imageHandler($thumb); | |
imagedestroy($thumb); | |
} | |
} | |
function getImageHandler($mime) { | |
$info['png'] = array('image/png', 'image/x-png'); | |
$info['jpeg'] = array('image/pjpeg', 'image/jpeg'); | |
$info['gif'] = array('image/gif'); | |
foreach($info as $key=>$value){ | |
if (in_array($mime, $info[$key])){ | |
return $key; | |
} | |
} | |
} | |
} | |
//usage | |
$di = new DynamicImage(); | |
$di->write(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment