15f0799d119054ab48c7bfa0f0a99e0ac5d475ba
[mw_thumbs] / app / data / modules / share / mw_data_thumbs.php
1 <?php
2
3   class mw_data_thumbs extends mw_data{
4
5     function mw_thumbs_activated(){
6       return true;
7     }
8
9     function img_thumb($src, $max_width, $max_height, $thumbs_dir = "", $background_color = array(255, 255, 255)){
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           )
66         ) === false
67       ){
68         return false;
69       }
70       if(
71         !$this->data_insert(
72           array(
73             "table_name" => "thumbs",
74             "values" => array(
75               "src" => $thumb["src"],
76               "src_width" => $thumb["src_width"],
77               "src_height" => $thumb["src_height"],
78               "max_width" => $max_width,
79               "max_height" => $max_height,
80               "thumb_file" => $thumb["thumb_file"],
81               "thumb_width" => $thumb["thumb_width"],
82               "thumb_height" => $thumb["thumb_height"],
83               "creation_date" => date("Y-m-d H:i:s")
84             )
85           )
86         )
87       ){
88         return false;
89       }
90       return $thumb;
91     }
92
93     function make_thumb(
94       $src,
95       $max_width,
96       $max_height,
97       $thumbs_dir,
98       $thumb_file,
99       $quality = null,
100       $background_color = array(255, 255, 255)
101     ){
102       $dest = $thumbs_dir.$thumb_file;
103       if(!($size = @getimagesize($src))) return false;
104       if($size[0]){
105         if($size[0] > $size[1]){
106           $width = $max_width;
107           $height = ($size[1] * ($width / $size[0]));
108         }
109         else{
110           $height = $max_height;
111           $width = $size[0] * ($height / $size[1]);
112         }
113         $v_ext_path = explode(".", $src);
114         $ext = $v_ext_path[count($v_ext_path) - 1];
115         if(strcasecmp($ext, "jpg") == 0 || strcasecmp($ext, "jpeg") == 0)  $ext = "jpg";
116         elseif(strcasecmp($ext, "gif") == 0)  $ext = "gif";
117         elseif(strcasecmp($ext, "png") == 0)  $ext = "png";
118         else $ext = "";
119         $create_function = "";
120         $thumb_function = "";
121         if(strcasecmp($ext, "jpg") == 0){
122           $create_function = "imagecreatefromjpeg";
123           $thumb_function = "imagejpeg";
124         }
125         elseif(strcasecmp($ext, "gif") == 0){
126           $create_function = "imagecreatefromgif";
127           $thumb_function = "imagegif";
128           $quality = NULL;
129         }
130         elseif(strcasecmp($ext, "png") == 0){
131           $create_function = "imagecreatefrompng";
132           $thumb_function = "imagepng";
133         }
134         if($create_function){
135           $src_img = $create_function($src);
136           $thumb_img = imagecreatetruecolor($max_width, $max_height);
137           $thumb_bkg = imagecolorallocate($thumb_img, $background_color[0], $background_color[1], $background_color[2]);
138           imagefilledrectangle($thumb_img, 0, 0, $max_width, $max_height, $thumb_bkg);
139           imagecopyresampled(
140             $thumb_img,
141             $src_img,
142             floor(($max_width - $width) / 2),
143             floor(($max_height - $height) / 2),
144             0,
145             0,
146             $width,
147             $height,
148             $size[0],
149             $size[1]
150           );
151           if(isset($quality)) $thumb_img = $thumb_function($thumb_img, $dest, $quality);
152           else $thumb_img = $thumb_function($thumb_img, $dest);
153           if($thumb_img !== false){
154             return array(
155               "src" => $src,
156               "src_width" => $size[0],
157               "src_height" => $size[1],
158               "max_width" => $max_width,
159               "max_height" => $max_height,
160               "thumb_file" => $thumb_file,
161               "thumb_width" => $max_width,
162               "thumb_height" => $max_height,
163               "creation_date" => date("Y-m-d H:i:s")
164             );
165           }
166         }
167       }
168       return false;
169     }
170
171     function new_thumb_file_name($dest, $file_name, $prefix = "img_"){
172       $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : "";
173       if(is_dir($dest)){
174         $ext = "";
175         if(strpos($file_name, ".") !== false){
176           $v_ext_path = explode(".", $file_name);
177           $ext = ".".$v_ext_path[count($v_ext_path) - 1];
178         }
179         $i = 0;
180         while(file_exists($dest.$prefix.$i.$ext)) $i++;
181         return $prefix.$i.$ext;
182       }
183       return false;
184     }
185
186   }
187
188 ?>