import initial
[mw_thumbs] / app / data / modules / share / mw_data_thumbs.php
1 <?php
2
3   class mw_data_thumbs extends mw_data{
4
5     function img_thumb($src, $max_width, $max_height, $thumbs_dir = "", $background_color = array(255, 255, 255)){
6       if(strlen($thumbs_dir) > 0){
7         if(!@is_dir($thumbs_dir)) @mkdir($thumbs_dir);
8         if(!@is_dir($thumbs_dir)) return false;
9       }
10       $thumbs_dir .= strlen($thumbs_dir) > 0 && substr($thumbs_dir, -1) != "/" ? "/" : "";
11       $thumb_dir = $max_width."x".$max_height."/";
12       if(!@is_dir($thumbs_dir.$thumb_dir)) @mkdir($thumbs_dir.$thumb_dir);
13       if(!@is_dir($thumbs_dir.$thumb_dir)) return false;
14       if(
15         (
16           $thumbs = $this->data_list(
17             array(
18               "table_name" => "thumbs",
19               "filters" => array(
20                 array("src", "eq", $src),
21                 array("max_width", "eq", $max_width),
22                 array("max_height", "eq", $max_height)
23               )
24             )
25           )
26         ) === false
27       ){
28         return false;
29       }
30       if($thumbs["list"]){
31         $thumb = reset($thumbs["list"]);
32         if(file_exists($thumbs_dir.$thumb["thumb_file"])){
33           return $thumb;
34         }
35         if(
36           !$this->data_delete(
37             array(
38               "table_name" => "thumbs",
39               "index_name" => "id",
40               "index_value" => $thumb["id"]
41             )
42           )
43         ){
44           return false;
45         }
46       }
47       if(($thumb_file = $this->new_thumb_file_name($thumbs_dir.$thumb_dir, $src, "img_")) === false){
48         return false;
49       }
50       $thumb_file = $thumb_dir.$thumb_file;
51       if(
52         (
53           $thumb = $this->make_thumb(
54             $src,
55             $max_width,
56             $max_height,
57             $thumbs_dir,
58             $thumb_file,
59             null,
60             $background_color
61           )
62         ) === false
63       ){
64         return false;
65       }
66       if(
67         !$this->data_insert(
68           array(
69             "table_name" => "thumbs",
70             "values" => array(
71               "src" => $thumb["src"],
72               "src_width" => $thumb["src_width"],
73               "src_height" => $thumb["src_height"],
74               "max_width" => $max_width,
75               "max_height" => $max_height,
76               "thumb_file" => $thumb["thumb_file"],
77               "thumb_width" => $thumb["thumb_width"],
78               "thumb_height" => $thumb["thumb_height"],
79               "creation_date" => date("Y-m-d H:i:s")
80             )
81           )
82         )
83       ){
84         return false;
85       }
86       return $thumb;
87     }
88
89     function make_thumb(
90       $src,
91       $max_width,
92       $max_height,
93       $thumbs_dir,
94       $thumb_file,
95       $quality = null,
96       $background_color = array(255, 255, 255)
97     ){
98       $dest = $thumbs_dir.$thumb_file;
99       if(!($size = @getimagesize($src))) return false;
100       if($size[0]){
101         if($size[0] > $size[1]){
102           $width = $max_width;
103           $height = ($size[1] * ($width / $size[0]));
104         }
105         else{
106           $height = $max_height;
107           $width = $size[0] * ($height / $size[1]);
108         }
109         $v_ext_path = explode(".", $src);
110         $ext = $v_ext_path[count($v_ext_path) - 1];
111         if(strcasecmp($ext, "jpg") == 0 || strcasecmp($ext, "jpeg") == 0)  $ext = "jpg";
112         elseif(strcasecmp($ext, "gif") == 0)  $ext = "gif";
113         elseif(strcasecmp($ext, "png") == 0)  $ext = "png";
114         else $ext = "";
115         $create_function = "";
116         $thumb_function = "";
117         if(strcasecmp($ext, "jpg") == 0){
118           $create_function = "imagecreatefromjpeg";
119           $thumb_function = "imagejpeg";
120         }
121         elseif(strcasecmp($ext, "gif") == 0){
122           $create_function = "imagecreatefromgif";
123           $thumb_function = "imagegif";
124           $quality = NULL;
125         }
126         elseif(strcasecmp($ext, "png") == 0){
127           $create_function = "imagecreatefrompng";
128           $thumb_function = "imagepng";
129         }
130         if($create_function){
131           $src_img = $create_function($src);
132           $thumb_img = imagecreatetruecolor($max_width, $max_height);
133           $thumb_bkg = imagecolorallocate($thumb_img, $background_color[0], $background_color[1], $background_color[2]);
134           imagefilledrectangle($thumb_img, 0, 0, $max_width, $max_height, $thumb_bkg);
135           imagecopyresampled(
136             $thumb_img,
137             $src_img,
138             floor(($max_width - $width) / 2),
139             floor(($max_height - $height) / 2),
140             0,
141             0,
142             $width,
143             $height,
144             $size[0],
145             $size[1]
146           );
147           if(isset($quality)) $thumb_img = $thumb_function($thumb_img, $dest, $quality);
148           else $thumb_img = $thumb_function($thumb_img, $dest);
149           if($thumb_img !== false){
150             return array(
151               "src" => $src,
152               "src_width" => $size[0],
153               "src_height" => $size[1],
154               "max_width" => $max_width,
155               "max_height" => $max_height,
156               "thumb_file" => $thumb_file,
157               "thumb_width" => $max_width,
158               "thumb_height" => $max_height,
159               "creation_date" => date("Y-m-d H:i:s")
160             );
161           }
162         }
163       }
164       return false;
165     }
166
167     function new_thumb_file_name($dest, $file_name, $prefix = "img_"){
168       $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : "";
169       if(is_dir($dest)){
170         $ext = "";
171         if(strpos($file_name, ".") !== false){
172           $v_ext_path = explode(".", $file_name);
173           $ext = ".".$v_ext_path[count($v_ext_path) - 1];
174         }
175         $i = 0;
176         while(file_exists($dest.$prefix.$i.$ext)) $i++;
177         return $prefix.$i.$ext;
178       }
179       return false;
180     }
181
182   }
183
184 ?>