bd811e85ce2d26e6738ed3dec1e9206065631a29
[phpfspot.git] / phpfspot_img.php
1 <?php
2
3 /***************************************************************************
4  *
5  * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
6  * All rights reserved
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  ***************************************************************************/
23
24 require_once "phpfspot.class.php";
25
26 class PHPFSPOT_IMG {
27    
28    var $db;
29    var $parent;
30
31    public function __construct()
32    {
33       $this->parent = new PHPFSPOT;
34       $this->db = $this->parent->db;
35
36    } // __construct()
37
38    public function __destruct()
39    {
40
41    } // __desctruct()
42
43    public function show($idx, $width = 0)
44    {
45       $details = $this->parent->get_photo_details($idx);
46    
47       if(!$details) {
48          $this->parent->showTextImage("The image you requested is unknown");
49          return;
50       }
51
52       /* no width specified - show photo in its original size */
53       if($width == 0) {
54          $fullpath = $this->parent->translate_path($details['directory_path'])  ."/". $details['name'];
55       }
56       /* show thumbnail */
57       else {
58          /* if no entry for this photo is yet in the database, create thumb */
59          if(!$this->parent->getMD5($idx)) {
60             $this->parent->gen_thumb($idx);
61          }
62          $fullpath = $this->parent->cfg->base_path ."/thumbs/". $width ."_". $this->parent->getMD5($idx);
63          /* if the thumb file does not exist, create it */
64          if(!file_exists($fullpath)) {
65             $this->parent->gen_thumb($idx);
66          }
67       }
68
69       if(!file_exists($fullpath)) {
70          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
71          return;
72       }
73       if(!is_readable($fullpath)) {
74          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
75          return;
76       }
77
78       $tmp = getimagesize($fullpath);
79       $mime = $tmp['mime'];
80
81       if(!$this->parent->checkifImageSupported($mime)) {
82          $this->parent->showTextImage("Unsupported Image Type");
83          return;
84       }
85
86       Header("Content-Type: ". $mime);
87       Header("Content-Length: ". filesize($fullpath));
88       Header("Content-Transfer-Encoding: binary\n");
89       Header("Content-Disposition: inline; filename=\"". $details['name'] ."\"");
90       Header("Accept-Ranges: bytes");
91       Header("Connection: close");
92    
93       $file = fopen($fullpath, "rb");
94       fpassthru($file);
95       @fclose($file);
96
97    } // show()
98
99 }
100
101 if(isset($_GET['idx']) && is_numeric($_GET['idx'])) {
102
103    $img = new PHPFSPOT_IMG;
104
105    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
106       $img->show($_GET['idx'], $_GET['width']);
107    else
108       $img->show($_GET['idx']);
109 }
110
111 ?>