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