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