don't recreate already existing thumbnails
[phpfspot.git] / phpfspot_img.php
1 <?php
2
3 require_once "phpfspot_db.php";
4 require_once "phpfspot.class.php";
5
6 class PHPFSPOT_IMG {
7    
8    var $db;
9    var $parent;
10
11    public function __construct()
12    {
13       $this->parent = new PHPFSPOT;
14       $this->db = $this->parent->db;
15
16    } // __construct()
17
18    public function __destruct()
19    {
20
21    } // __desctruct()
22
23    public function show($idx, $width = "")
24    {
25       $details = $this->parent->get_photo_details($idx);
26
27                 foreach(Array($this->parent->cfg->thumb_width, $this->parent->cfg->photo_width) as $resolution)
28                   $this->resize_image($this->parent->translate_path($details['directory_path'])  ."/". $details['name'], $resolution);
29
30       if($width == "")
31          $fullpath = $this->parent->translate_path($details['directory_path'])  ."/". $details['name'];
32       else
33          $fullpath = $this->parent->translate_path($details['directory_path'])  ."/thumbs/". $width ."_". $details['name'];
34
35
36       $tmp = getimagesize($fullpath);
37       $mime = $tmp['mime'];
38
39       Header("Content-Type: ". $mime);
40       Header("Content-Length: ". filesize($fullpath));
41       Header("Content-Transfer-Encoding: binary\n");              
42       $user_agent = strtolower ($_SERVER["HTTP_USER_AGENT"]);     
43       Header("Content-Disposition: inline; filename=\"". $details['name'] ."\"");
44       Header("Accept-Ranges: bytes");                             
45       Header("Connection: close");                                
46    
47       $file = fopen($fullpath, "rb");
48       fpassthru($file);
49       @fclose($file);
50
51    } // show()
52
53    public function resize_image($image, $width)
54    {
55       // if thumbnail already exists, don't recreate it
56       if(file_exists(dirname($image) ."/thumbs/". $width ."_". basename($image)))
57          return;
58
59       $src_img = @imagecreatefromjpeg($image);
60         
61       if($src_img)
62       {
63          /* grabs the height and width */
64          $new_w = imagesx($src_img);
65          $new_h = imagesy($src_img);
66                 
67          // If requested width is more then the actual image width,
68          // do not generate a thumbnail
69          
70          if($width >= $new_w) {
71             imagedestroy($src_img);
72             return;
73          }
74                 
75          /* calculates aspect ratio */
76          $aspect_ratio = $new_h / $new_w;
77                 
78          /* sets new size */
79          $new_w = $width;
80          $new_h = abs($new_w * $aspect_ratio);
81                 
82          /* creates new image of that size */
83          $dst_img = imagecreatetruecolor($new_w,$new_h);
84
85          imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
86                 
87          /* copies resized portion of original image into new image */
88          imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
89                 
90          /* write down new generated file */
91          
92          if(!file_exists(dirname($image) ."/thumbs"))
93             mkdir(dirname($image) ."/thumbs");
94
95          $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
96          imagejpeg($dst_img, $newfile, 75);
97                 
98          /* free your mind */
99          imagedestroy($dst_img);
100          imagedestroy($src_img);
101       }
102
103    } // resize_image()
104 }
105
106 if(isset($_GET['idx']) && is_numeric($_GET['idx'])) {
107
108    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
109       $width = $_GET['width'];
110    else
111       $width = "";
112
113    $img = new PHPFSPOT_IMG;
114    $img->show($_GET['idx'], $width);
115 }
116
117 ?>