Revert "* initial work towards factorizing the controllers (yeah, now it's tsControll...
[vsc.git] / _res / _libs / tsimage2.class.php
blob722c848327bd1e1ad834b58836bee708e5502a82
1 <?php
3 class tsImage2 extends Output {
4 public static $allowedExtensions = array (1 => 'gif', 2 => 'jpg', 3 => 'png');
5 public $path;
6 private $content,
7 $imgAttr,
8 $hashedPath = null;
10 public function __construct ( $incPath ){
11 // ob_start();
12 var_dump($incPath);
13 $this->time['start'] = microtime (true);
14 if ($this->testPath ($incPath)) {
15 $this->imgAttr = getimagesize($this->path);
16 // var_dump($this->imgAttr);
17 $this->getType();
18 } else {
19 // die ('bitch');
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)) {
28 var_dump($path);
29 return $path;
32 return false;
35 public function __destruct (){
36 if ( !empty($this->content) )
37 imagedestroy($this->content);
40 public function testPath ($incPath = null) {
41 if ($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;
49 return true;
52 return false;
55 public function getType(){
56 //$this->imgAttr = getimagesize ($this->path);
57 switch ($this->imgAttr[2]) {
58 case 1 :
59 $this->content = imagecreatefromgif ($this->path);
60 break;
61 case 2 :
62 $this->content = imagecreatefromjpeg ($this->path);
63 break;
64 case 3 :
65 $this->content = imagecreatefrompng ($this->path);
66 imageAlphaBlending($this->content, true);
67 imageSaveAlpha($this->content, true);
68 break;
69 default :
70 break;
74 public function outputHash(){
75 $retVal = false;
77 if (tsImage2::$allowedExtensions[$this->imgAttr[2]] == 'jpg')
78 $type = 'jpeg';
79 else
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);
90 return $retVal;
92 public function resize($w, $h = null, $method = 'scale'){
93 if (empty($this->content))
94 return false;
96 // check cache
97 if (!empty($w))
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);
106 // } else {
107 // $this->outputHash();
108 // }
109 } else {
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]);
118 $this->outputHash();
120 if (!empty($retVal)) {
121 return true;
122 } else {
123 $this->content = $temp;
124 return false;
128 public function dispatch (){
129 ob_end_clean();
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')
133 $type = 'jpeg';
134 else
135 $type = tsImage2::$allowedExtensions[$this->imgAttr[2]];
138 $functionName = 'image'.$type;
139 header ('Content-type: '.$this->imgAttr['mime']);
141 if (!empty($w))
142 $this->hashedPath = S_C_TEMPL_DIR.md5($this->path.$w.$h);
144 if (!is_file($this->hashedPath)) {
145 ob_start();
146 $retVal = $functionName($this->content);
147 $imageData = ob_get_contents();
148 echo $imageDataLength = ob_get_length();
149 ob_end_clean();
151 header ('Content-Length: ' . $imageDataLength);
152 echo $imageData.'Not Hashed';
153 } else {
154 header ('Content-Length: ' . filesize($this->hashedPath));
155 $retVal = readfile($this->hashedPath);
156 echo 'Hashed';
158 $this->time['end'] = microtime (true);
159 return $retVal;
162 public function debug(){
163 ob_end_clean();
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";
167 echo '<pre>'."\n";
168 var_dump($this);
169 echo '</pre>'."\n";