root / phpfspot_img.php

View | Annotate | Download (7.3 KB)

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