draw single photo with AJAX refresh
[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       $src_img = @imagecreatefromjpeg($image);
56         
57       if($src_img)
58       {
59          /* grabs the height and width */
60          $new_w = imagesx($src_img);
61          $new_h = imagesy($src_img);
62                 
63          // If requested width is more then the actual image width,
64          // do not generate a thumbnail
65          
66          if($width >= $new_w) {
67             imagedestroy($src_img);
68             return;
69          }
70                 
71          /* calculates aspect ratio */
72          $aspect_ratio = $new_h / $new_w;
73                 
74          /* sets new size */
75          $new_w = $width;
76          $new_h = abs($new_w * $aspect_ratio);
77                 
78          /* creates new image of that size */
79          $dst_img = imagecreatetruecolor($new_w,$new_h);
80
81          imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
82                 
83          /* copies resized portion of original image into new image */
84          imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
85                 
86          /* write down new generated file */
87          
88          if(!file_exists(dirname($image) ."/thumbs"))
89             mkdir(dirname($image) ."/thumbs");
90
91          $newfile = dirname($image) ."/thumbs/". $width ."_". basename($image);
92          imagejpeg($dst_img, $newfile, 75);
93                 
94          /* free your mind */
95          imagedestroy($dst_img);
96          imagedestroy($src_img);
97       }
98
99    } // resize_image()
100 }
101
102 if(isset($_GET['idx']) && is_numeric($_GET['idx'])) {
103
104    if(isset($_GET['width']) && is_numeric($_GET['width'])) 
105       $width = $_GET['width'];
106    else
107       $width = "";
108
109    $img = new PHPFSPOT_IMG;
110    $img->show($_GET['idx'], $width);
111 }
112
113 ?>