31831b88158d61e96731c69e845d5fa1a0e60dbd
[ouya-imagestore.git] / src / imagestore / Controller / Image.php
1 <?php
2 namespace imagestore;
3
4 class Controller_Image
5 {
6     public function handle($uri)
7     {
8         if (!isset($_GET['path'])) {
9             return $this->error('400 Bad request', 'Path missing');
10         }
11
12         $path = $GLOBALS['imagestore']['basedir'] . $_GET['path'];
13         if (!file_exists($path)) {
14             return $this->error('404 Not Found', 'File not found');
15         }
16         
17         if (!isset($_GET['w'])) {
18             header('Content-type: image/jpeg');
19             header('Content-length: ' . filesize($path));
20             return readfile($path);
21         }
22         
23         //resize
24         $img = imagecreatefromjpeg($path);
25
26         $newWidth = (int) $_GET['w'];
27         //if (isset($_GET['h'])) {
28         //    $newHeight = (int) $_GET['h'];
29         //} else {
30         $newHeight = intval($newWidth / imagesx($img) * imagesy($img));
31         //}
32         
33         $thumb = imagecreatetruecolor($newWidth, $newHeight);
34         // Resize
35         imagecopyresampled(
36             $thumb, $img, 0, 0, 0, 0,
37             $newWidth, $newHeight, imagesx($img), imagesy($img)
38         );
39         imagedestroy($img);
40
41         header('Content-type: image/jpeg');
42         imagejpeg($thumb);
43         imagedestroy($thumb);
44     }
45
46     protected function error($error, $message)
47     {
48         header('HTTP/1.0 ' . $error);
49         header('Content-type: text/plain; charset=utf-8');
50         echo $message;
51     }
52 }
53 ?>