issue75, support for database access via PDO sqlite
[phpfspot.git] / phpfspot_img.php
1 <?php
2
3 /***************************************************************************
4  *
5  * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
6  * All rights reserved
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  ***************************************************************************/
23
24 require_once "phpfspot.class.php";
25
26 class PHPFSPOT_IMG {
27    
28    var $db;
29    var $parent;
30
31    /**
32     * PHPFSPOT_IMG class constructor
33     */
34    public function __construct()
35    {
36       $this->parent = new PHPFSPOT;
37       $this->db = $this->parent->db;
38
39    } // __construct()
40
41    /**
42     * PHPFSPOT_IMG class destructor
43     */
44    public function __destruct()
45    {
46
47    } // __desctruct()
48
49    /**
50     * sends the specified image to the browser
51     *
52     * this function will send the specified image to 
53     * the client - in the specified width. it also try's
54     * to create on-the-fly missing thumbnails via PHPFSPOT
55     * gen_thumbs function.
56     */
57    public function show($idx, $width = 0)
58    {
59       if($idx == 'rand')
60          $idx = $this->parent->get_random_photo();
61
62       $details = $this->parent->get_photo_details($idx);
63    
64       if(!$details) {
65          $this->parent->showTextImage("The image (". $idx .") you requested is unknown");
66          return;
67       }
68
69       /* no width specified - show photo in its original size */
70       if($width == 0) {
71          $fullpath = $this->parent->translate_path($details['directory_path'])  ."/". $details['name'];
72       }
73       /* show thumbnail */
74       else {
75          /* if no entry for this photo is yet in the database, create thumb */
76          if(!$this->parent->getMD5($idx)) {
77             $this->parent->gen_thumb($idx);
78          }
79          $fullpath = $this->parent->get_thumb_path($width, $idx);
80          /* if the thumb file does not exist, create it */
81          if(!file_exists($fullpath)) {
82             $this->parent->gen_thumb($idx);
83          }
84       }
85
86       if(!file_exists($fullpath)) {
87          $this->parent->showTextImage("File ". basename($fullpath) ." does not exist");
88          return;
89       }
90       if(!is_readable($fullpath)) {
91          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
92          return;
93       }
94
95       $tmp = getimagesize($fullpath);
96       $mime = $tmp['mime'];
97
98       if(!$this->parent->checkifImageSupported($mime)) {
99          $this->parent->showTextImage("Unsupported Image Type");
100          return;
101       }
102
103       Header("Content-Type: ". $mime);
104       Header("Content-Length: ". filesize($fullpath));
105       Header("Content-Transfer-Encoding: binary\n");
106       Header("Content-Disposition: inline; filename=\"". $details['name'] ."\"");
107       Header("Accept-Ranges: bytes");
108       Header("Connection: close");
109       Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
110       Header("Cache-Control: no-cache");
111       Header("Pragma: no-cache");
112          
113       $file = fopen($fullpath, "rb");
114       fpassthru($file);
115       @fclose($file);
116
117    } // show()
118
119 }
120
121 if(isset($_GET['idx']) && (is_numeric($_GET['idx']) || $_GET['idx'] == 'rand')) {
122
123    $img = new PHPFSPOT_IMG;
124
125    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
126       $img->show($_GET['idx'], $_GET['width']);
127    else
128       $img->show($_GET['idx']);
129 }
130
131 ?>