issue43, also return a thumbnail if requested width is below image size
[phpfspot.git] / phpfspot_img.php
1 <?php
2
3 require_once "phpfspot.class.php";
4
5 class PHPFSPOT_IMG {
6    
7    var $db;
8    var $parent;
9
10    public function __construct()
11    {
12       $this->parent = new PHPFSPOT;
13       $this->db = $this->parent->db;
14
15    } // __construct()
16
17    public function __destruct()
18    {
19
20    } // __desctruct()
21
22    public function show($idx, $width = 0)
23    {
24       $details = $this->parent->get_photo_details($idx);
25    
26       if(!$details) {
27          $this->parent->showTextImage("The image you requested is unknown");
28          return;
29       }
30
31       /* no width specified - show photo in its original size */
32       if($width == 0) {
33          $fullpath = $this->parent->translate_path($details['directory_path'])  ."/". $details['name'];
34       }
35       /* show thumbnail */
36       else {
37          /* if no entry for this photo is yet in the database, create thumb */
38          if(!$this->parent->getMD5($idx)) {
39             $this->parent->gen_thumb($idx);
40          }
41          $fullpath = $this->parent->cfg->base_path ."/thumbs/". $width ."_". $this->parent->getMD5($idx);
42          /* if the thumb file does not exist, create it */
43          if(!file_exists($fullpath)) {
44             $this->parent->gen_thumb($idx);
45          }
46       }
47
48       if(!file_exists($fullpath)) {
49          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
50          return;
51       }
52       if(!is_readable($fullpath)) {
53          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
54          return;
55       }
56
57       $tmp = getimagesize($fullpath);
58       $mime = $tmp['mime'];
59
60       if(!$this->parent->checkifImageSupported($mime)) {
61          $this->parent->showTextImage("Unsupported Image Type");
62          return;
63       }
64
65       Header("Content-Type: ". $mime);
66       Header("Content-Length: ". filesize($fullpath));
67       Header("Content-Transfer-Encoding: binary\n");
68       Header("Content-Disposition: inline; filename=\"". $details['name'] ."\"");
69       Header("Accept-Ranges: bytes");
70       Header("Connection: close");
71    
72       $file = fopen($fullpath, "rb");
73       fpassthru($file);
74       @fclose($file);
75
76    } // show()
77
78 }
79
80 if(isset($_GET['idx']) && is_numeric($_GET['idx'])) {
81
82    $img = new PHPFSPOT_IMG;
83
84    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
85       $img->show($_GET['idx'], $_GET['width']);
86    else
87       $img->show($_GET['idx']);
88 }
89
90 ?>