update docs for 1.7 release
[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          if(!$this->parent->is_valid_width($width)) {
91             $this->parent->showTextImage("Requested width ". $width ."px is not valid!");
92             return;
93          }
94          /* check for an entry if we already handled this photo before. If not,
95             create a thumbnail for it.
96          */
97          if(!$this->parent->getMD5($idx)) {
98             $this->parent->gen_thumb($idx);
99          }
100          /* get the full filesystem path to the thumbnail */
101          $fullpath = $this->parent->get_thumb_path($width, $idx, $version);
102          /* if the thumb file does not exist, create it */
103          if(!file_exists($fullpath)) {
104             $this->parent->gen_thumb($idx);
105          }
106       }
107
108       if(!file_exists($fullpath)) {
109          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
110          return;
111       }
112       if(!is_readable($fullpath)) {
113          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
114          return;
115       } 
116       $mime = $this->parent->get_mime_info($fullpath);
117
118       if(!$this->parent->checkifImageSupported($mime)) {
119          $this->parent->showTextImage("Unsupported Image Type");
120          return;
121       }
122
123       Header("Content-Type: ". $mime);
124       Header("Content-Length: ". filesize($fullpath));
125       Header("Content-Transfer-Encoding: binary\n");
126       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
127       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
128       Header("Accept-Ranges: bytes");
129       Header("Connection: close");
130       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
131       Header("Cache-Control: no-cache");
132       Header("Pragma: no-cache");
133
134       $file = fopen($fullpath, "rb");
135       fpassthru($file);
136       @fclose($file);
137
138    } // showImg()
139
140    /**
141     * sends a random photo of the requested tag to the browser
142     *
143     * this function will send a random photo to the client.
144     * It is selected out by the provided $tagidx. It also try's
145     * to create on-the-fly missing thumbnails via PHPFSPOT
146     * gen_thumbs function.
147     * @param integer $idx
148     */
149    public function showTagImg($tagidx)
150    {
151       $idx = $this->parent->get_random_tag_photo($tagidx);
152       $width = 30;
153
154       $details = $this->parent->get_photo_details($idx);
155
156       if(!$details) {
157          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
158          return;
159       }
160
161       /* if no entry for this photo is yet in the database, create thumb */
162       if(!$this->parent->getMD5($idx)) {
163          $this->parent->gen_thumb($idx);
164       }
165
166       $version = $this->parent->get_latest_version($idx);
167
168       $fullpath = $this->parent->get_thumb_path($width, $idx, $version);
169       /* if the thumb file does not exist, create it */
170       if(!file_exists($fullpath)) {
171          $this->parent->gen_thumb($idx);
172       }
173
174       if(!file_exists($fullpath)) {
175          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
176          return;
177       }
178       if(!is_readable($fullpath)) {
179          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
180          return;
181       }
182
183       $mime = $this->parent->get_mime_info($fullpath);
184
185       if(!$this->parent->checkifImageSupported($mime)) {
186          $this->parent->showTextImage("Unsupported Image Type");
187          return;
188       }
189
190       Header("Content-Type: ". $mime);
191       Header("Content-Length: ". filesize($fullpath));
192       Header("Content-Transfer-Encoding: binary\n");
193       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
194       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
195       Header("Accept-Ranges: bytes");
196       Header("Connection: close");
197       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
198       Header("Cache-Control: no-cache");
199       Header("Pragma: no-cache");
200
201       $file = fopen($fullpath, "rb");
202       fpassthru($file);
203       @fclose($file);
204
205    } // showTagImg()
206
207 } // PHPFSPOT_IMG()
208
209 if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand')) {
210
211    $img = new PHPFSPOT_IMG;
212
213    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
214       $width = $_GET['width'];
215    else
216       $width = NULL;
217
218    if(isset($_GET['version']) && is_numeric($_GET['version']))
219       $version = $_GET['version'];
220    else
221       $version = NULL;
222
223    $img->showImg($_GET['idx'], $width, $version);
224
225    exit(0);
226 }
227
228 if(isset($_GET['tagidx']) && is_numeric($_GET['tagidx'])) {
229
230    $img = new PHPFSPOT_IMG;
231    $img->showTagImg($_GET['tagidx']);
232
233    exit(0);
234
235 }
236
237 ?>