fix conflict in phpfspot.class.php
[phpfspot.git] / phpfspot_img.php
1 <?php
2
3 /***************************************************************************
4  *
5  * phpfspot, presents your F-Spot photo collection in Web browsers.
6  *
7  * Copyright (c) by Andreas Unterkircher
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  ***************************************************************************/
24
25 require_once "phpfspot.class.php";
26
27 /**
28  * PHPFSPOT_IMG class
29  *
30  * handles phpfspot's photos. It will output either the photo binaries
31  * or can also show error messages as a on-the-fly generated picture.
32  * @package phpfspot
33  */
34 class PHPFSPOT_IMG {
35    
36    private $db;
37    private $parent;
38
39    /**
40     * PHPFSPOT_IMG class constructor
41     */
42    public function __construct()
43    {
44       $this->parent = new PHPFSPOT;
45       $this->db = $this->parent->db;
46
47    } // __construct()
48
49    /**
50     * PHPFSPOT_IMG class destructor
51     */
52    public function __destruct()
53    {
54
55    } // __desctruct()
56
57    /**
58     * sends the specified image to the browser
59     *
60     * this function will send the specified image to 
61     * the client - in the specified width. it also try's
62     * to create on-the-fly missing thumbnails via PHPFSPOT
63     * gen_thumbs function.
64     * @param integer $idx
65     * @param integer $width
66     */
67    public function showImg($idx, $width = 0, $version = NULL)
68    {
69       if($idx == 'rand')
70          $idx = $this->parent->get_random_photo();
71
72       /* display the lastest available version, if a wrong version has been requested */
73       if(!isset($version) || !$this->parent->is_valid_version($idx, $version))
74          $version = $this->parent->get_latest_version($idx);
75
76       $details = $this->parent->get_photo_details($idx, $version);
77
78       if(!$details) {
79          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
80          return;
81       }
82
83       /* no width specified - show photo in its original size */
84       if($width == 0) {
85          $fullpath = $this->parent->translate_path($this->parent->parse_uri($details['uri'], 'fullpath'));
86       }
87       /* show thumbnail */
88       else {
89
90          /* check for an entry if we already handled this photo before. If not,
91             create a thumbnail for it.
92          */
93          if(!$this->parent->getMD5($idx)) {
94             $this->parent->gen_thumb($idx);
95          }
96          /* get the full filesystem path to the thumbnail */
97          $fullpath = $this->parent->get_thumb_path($width, $idx, $version);
98          /* if the thumb file does not exist, create it */
99          if(!file_exists($fullpath)) {
100             $this->parent->gen_thumb($idx);
101          }
102       }
103
104       if(!file_exists($fullpath)) {
105          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
106          return;
107       }
108       if(!is_readable($fullpath)) {
109          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
110          return;
111       } 
112       $mime = $this->parent->get_mime_info($fullpath);
113
114       if(!$this->parent->checkifImageSupported($mime)) {
115          $this->parent->showTextImage("Unsupported Image Type");
116          return;
117       }
118
119       Header("Content-Type: ". $mime);
120       Header("Content-Length: ". filesize($fullpath));
121       Header("Content-Transfer-Encoding: binary\n");
122       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
123       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
124       Header("Accept-Ranges: bytes");
125       Header("Connection: close");
126       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
127       Header("Cache-Control: no-cache");
128       Header("Pragma: no-cache");
129
130       $file = fopen($fullpath, "rb");
131       fpassthru($file);
132       @fclose($file);
133
134    } // showImg()
135
136    /**
137     * sends a random photo of the requested tag to the browser
138     *
139     * this function will send a random photo to the client.
140     * It is selected out by the provided $tagidx. It also try's
141     * to create on-the-fly missing thumbnails via PHPFSPOT
142     * gen_thumbs function.
143     * @param integer $idx
144     */
145    public function showTagImg($tagidx)
146    {
147       $idx = $this->parent->get_random_tag_photo($tagidx);
148       $width = 30;
149
150       $details = $this->parent->get_photo_details($idx);
151
152       if(!$details) {
153          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
154          return;
155       }
156
157       /* if no entry for this photo is yet in the database, create thumb */
158       if(!$this->parent->getMD5($idx)) {
159          $this->parent->gen_thumb($idx);
160       }
161
162       $version = $this->parent­>get_latest_version($idx);
163
164       $fullpath = $this->parent->get_thumb_path($width, $idx, $version);
165       /* if the thumb file does not exist, create it */
166       if(!file_exists($fullpath)) {
167          $this->parent->gen_thumb($idx);
168       }
169
170       if(!file_exists($fullpath)) {
171          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
172          return;
173       }
174       if(!is_readable($fullpath)) {
175          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
176          return;
177       }
178
179       $mime = $this->parent->get_mime_info($fullpath);
180
181       if(!$this->parent->checkifImageSupported($mime)) {
182          $this->parent->showTextImage("Unsupported Image Type");
183          return;
184       }
185
186       Header("Content-Type: ". $mime);
187       Header("Content-Length: ". filesize($fullpath));
188       Header("Content-Transfer-Encoding: binary\n");
189       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
190       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
191       Header("Accept-Ranges: bytes");
192       Header("Connection: close");
193       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
194       Header("Cache-Control: no-cache");
195       Header("Pragma: no-cache");
196
197       $file = fopen($fullpath, "rb");
198       fpassthru($file);
199       @fclose($file);
200
201    } // showTagImg()
202
203 } // PHPFSPOT_IMG()
204
205 if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand')) {
206
207    $img = new PHPFSPOT_IMG;
208
209    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
210       $width = $_GET['width'];
211    else
212       $width = NULL;
213
214    if(isset($_GET['version']) && is_numeric($_GET['version']))
215       $version = $_GET['version'];
216    else
217       $version = NULL;
218
219    $img->showImg($_GET['idx'], $width, $version);
220
221    exit(0);
222 }
223
224 if(isset($_GET['tagidx']) && is_numeric($_GET['tagidx'])) {
225
226    $img = new PHPFSPOT_IMG;
227    $img->showTagImg($_GET['tagidx']);
228
229    exit(0);
230
231 }
232
233 ?>