8379b832c76870f339750c5a354129f591301904
[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  *
33  * @package phpfspot
34  */
35 class PHPFSPOT_IMG {
36    
37    var $db;
38    var $parent;
39
40    /**
41     * PHPFSPOT_IMG class constructor
42     */
43    public function __construct()
44    {
45       $this->parent = new PHPFSPOT;
46       $this->db = $this->parent->db;
47
48    } // __construct()
49
50    /**
51     * PHPFSPOT_IMG class destructor
52     */
53    public function __destruct()
54    {
55
56    } // __desctruct()
57
58    /**
59     * sends the specified image to the browser
60     *
61     * this function will send the specified image to 
62     * the client - in the specified width. it also try's
63     * to create on-the-fly missing thumbnails via PHPFSPOT
64     * gen_thumbs function.
65     */
66    public function show($idx, $width = 0)
67    {
68       if($idx == 'rand')
69          $idx = $this->parent->get_random_photo();
70
71       $details = $this->parent->get_photo_details($idx);
72    
73       if(!$details) {
74          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
75          return;
76       }
77
78       /* no width specified - show photo in its original size */
79       if($width == 0) {
80          $fullpath = $this->parent->translate_path($this->parent->parse_uri($details['uri'], 'fullpath'));
81       }
82       /* show thumbnail */
83       else {
84          /* if no entry for this photo is yet in the database, create thumb */
85          if(!$this->parent->getMD5($idx)) {
86             $this->parent->gen_thumb($idx);
87          }
88          $fullpath = $this->parent->get_thumb_path($width, $idx);
89          /* if the thumb file does not exist, create it */
90          if(!file_exists($fullpath)) {
91             $this->parent->gen_thumb($idx);
92          }
93       }
94
95       if(!file_exists($fullpath)) {
96          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
97          return;
98       }
99       if(!is_readable($fullpath)) {
100          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
101          return;
102       }
103
104       $tmp = getimagesize($fullpath);
105       $mime = $tmp['mime'];
106
107       if(!$this->parent->checkifImageSupported($mime)) {
108          $this->parent->showTextImage("Unsupported Image Type");
109          return;
110       }
111
112       Header("Content-Type: ". $mime);
113       Header("Content-Length: ". filesize($fullpath));
114       Header("Content-Transfer-Encoding: binary\n");
115       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
116       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
117       Header("Accept-Ranges: bytes");
118       Header("Connection: close");
119       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
120       Header("Cache-Control: no-cache");
121       Header("Pragma: no-cache");
122          
123       $file = fopen($fullpath, "rb");
124       fpassthru($file);
125       @fclose($file);
126
127    } // show()
128
129 }
130
131 if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand')) {
132
133    $img = new PHPFSPOT_IMG;
134
135    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
136       $img->show($_GET['idx'], $_GET['width']);
137    else
138       $img->show($_GET['idx']);
139 }
140
141 ?>