translate_path() only needs one parameters. remove unused second one
[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    private $db;
38    private $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     * @param integer $idx
66     * @param integer $width
67     */
68    public function show($idx, $width = 0)
69    {
70       if($idx == 'rand')
71          $idx = $this->parent->get_random_photo();
72
73       $details = $this->parent->get_photo_details($idx);
74    
75       if(!$details) {
76          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
77          return;
78       }
79
80       /* no width specified - show photo in its original size */
81       if($width == 0) {
82          $fullpath = $this->parent->translate_path($this->parent->parse_uri($details['uri'], 'fullpath'));
83       }
84       /* show thumbnail */
85       else {
86          /* if no entry for this photo is yet in the database, create thumb */
87          if(!$this->parent->getMD5($idx)) {
88             $this->parent->gen_thumb($idx);
89          }
90          $fullpath = $this->parent->get_thumb_path($width, $idx);
91          /* if the thumb file does not exist, create it */
92          if(!file_exists($fullpath)) {
93             $this->parent->gen_thumb($idx);
94          }
95       }
96
97       if(!file_exists($fullpath)) {
98          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
99          return;
100       }
101       if(!is_readable($fullpath)) {
102          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
103          return;
104       }
105
106       $tmp = getimagesize($fullpath);
107       $mime = $tmp['mime'];
108
109       if(!$this->parent->checkifImageSupported($mime)) {
110          $this->parent->showTextImage("Unsupported Image Type");
111          return;
112       }
113
114       Header("Content-Type: ". $mime);
115       Header("Content-Length: ". filesize($fullpath));
116       Header("Content-Transfer-Encoding: binary\n");
117       Header("Content-Disposition: inline; filename=\"". $this->parent->parse_uri($details['uri'], 'filename') ."\"");
118       Header("Content-Description: ". $this->parent->parse_uri($details['uri'], 'filename'));
119       Header("Accept-Ranges: bytes");
120       Header("Connection: close");
121       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
122       Header("Cache-Control: no-cache");
123       Header("Pragma: no-cache");
124          
125       $file = fopen($fullpath, "rb");
126       fpassthru($file);
127       @fclose($file);
128
129    } // show()
130
131 } // PHPFSPOT_IMG()
132
133 if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand')) {
134
135    $img = new PHPFSPOT_IMG;
136
137    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
138       $img->show($_GET['idx'], $_GET['width']);
139    else
140       $img->show($_GET['idx']);
141 }
142
143 ?>