3 class tsImage2
extends Output
{
4 public static $allowedExtensions = array (1 => 'gif', 2 => 'jpg', 3 => 'png');
10 public function __construct ( $incPath ){
13 $this->time
['start'] = microtime (true);
14 if ($this->testPath ($incPath)) {
15 $this->imgAttr
= getimagesize($this->path
);
16 // var_dump($this->imgAttr);
23 static public function getPath ($id) {
24 $basePath = IMAGE_PATH
.$id;
25 foreach ( tsImage2
::$allowedExtensions as $ext) {
26 $path = $basePath.'.'.$ext;
27 if (is_file ($path)) {
35 public function __destruct (){
36 if ( !empty($this->content
) )
37 imagedestroy($this->content
);
40 public function testPath ($incPath = null) {
42 $incPath = $this->path
;
43 $ext = end(explode('.', $incPath));
45 if (in_array ($ext, tsImage2
::$allowedExtensions)){
46 $realPath = realpath ($incPath);
47 if (file_exists($realPath)){
48 $this->path
= $realPath;
55 public function getType(){
56 //$this->imgAttr = getimagesize ($this->path);
57 switch ($this->imgAttr
[2]) {
59 $this->content
= imagecreatefromgif ($this->path
);
62 $this->content
= imagecreatefromjpeg ($this->path
);
65 $this->content
= imagecreatefrompng ($this->path
);
66 imageAlphaBlending($this->content
, true);
67 imageSaveAlpha($this->content
, true);
74 public function outputHash(){
77 if (tsImage2
::$allowedExtensions[$this->imgAttr
[2]] == 'jpg')
80 $type = tsImage2
::$allowedExtensions[$this->imgAttr
[2]];
82 $functionName = 'image'.$type;
84 if (!is_file($this->hashedPath
) && !empty($this->content
)) {
85 header ('Content-type: image/'.$type);
86 $retVal = $functionName($this->content
, $this->hashedPath
);
92 public function resize($w, $h = null, $method = 'scale'){
93 if (empty($this->content
))
98 $this->hashedPath
= S_C_TEMPL_DIR
.md5($this->path
.$w.$h);
100 // die(print_r($this->hashedPath));
101 if (is_file($this->hashedPath
)) {
102 $imgAttr = getimagesize($this->hashedPath
);
103 // if ($w == $imgAttr[0] && $h == $imgAttr[1] && !empty($allowedExtensions[$imgAttr[2]])) {
104 $functionName = 'imagecreatefrom'.(tsImage2
::$allowedExtensions[$imgAttr[2]] == 'jpg' ?
'jpeg' : tsImage2
::$allowedExtensions[$imgAttr[2]]);
105 $this->content
= $functionName($this->hashedPath
);
107 // $this->outputHash();
110 $temp = $this->content
;
112 if (empty($h)) // only width... we will make it proportional
113 $h = round($this->imgAttr
[1]/($this->imgAttr
[0]/$w));
115 $this->content
= imagecreatetruecolor ($w, $h);
116 $retVal = imagecopyresampled ($this->content
, $temp, 0, 0, 0, 0, $w, $h, $this->imgAttr
[0], $this->imgAttr
[1]);
120 if (!empty($retVal)) {
123 $this->content
= $temp;
128 public function dispatch (){
130 if (!is_array($this->imgAttr
))
131 trigger_error($this->path
.' is not an image', E_USER_ERROR
);
132 if (tsImage2
::$allowedExtensions[$this->imgAttr
[2]] == 'jpg')
135 $type = tsImage2
::$allowedExtensions[$this->imgAttr
[2]];
138 $functionName = 'image'.$type;
139 header ('Content-type: '.$this->imgAttr
['mime']);
142 $this->hashedPath
= S_C_TEMPL_DIR
.md5($this->path
.$w.$h);
144 if (!is_file($this->hashedPath
)) {
146 $retVal = $functionName($this->content
);
147 $imageData = ob_get_contents();
148 echo $imageDataLength = ob_get_length();
151 header ('Content-Length: ' . $imageDataLength);
152 echo $imageData.'Not Hashed';
154 header ('Content-Length: ' . filesize($this->hashedPath
));
155 $retVal = readfile($this->hashedPath
);
158 $this->time
['end'] = microtime (true);
162 public function debug(){
164 $this->time
['end'] = microtime (true);
165 $this->time
['ms'] = number_format(($this->time
['end'] - $this->time
['start']) * 1000 , 2, ',' , '.');
166 echo ('Content-type: '.$this->imgAttr
['mime'])."\n";