3 class mw_data_thumbs extends mw_data{
5 function mw_thumbs_activated(){
9 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;
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;
20 $thumbs = $this->data_list(
22 "table_name" => "thumbs",
24 array("src", "eq", $src),
25 array("max_width", "eq", $max_width),
26 array("max_height", "eq", $max_height)
35 $thumb = reset($thumbs["list"]);
36 if(file_exists($thumbs_dir.$thumb["thumb_file"])){
42 "table_name" => "thumbs",
44 "index_value" => $thumb["id"]
51 if(($thumb_file = $this->new_thumb_file_name($thumbs_dir.$thumb_dir, $src, "img_")) === false){
54 $thumb_file = $thumb_dir.$thumb_file;
57 $thumb = $this->make_thumb(
74 "table_name" => "thumbs",
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")
101 $background_color = array(255, 255, 255),
104 $dest = $thumbs_dir.$thumb_file;
105 if(!($size = @getimagesize($src))) return false;
107 if($size[0] > $size[1]){
109 $height = ($size[1] * ($width / $size[0]));
112 $height = $max_height;
113 $width = $size[0] * ($height / $size[1]);
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";
121 $create_function = "";
122 $thumb_function = "";
123 if(strcasecmp($ext, "jpg") == 0){
124 $create_function = "imagecreatefromjpeg";
125 $thumb_function = "imagejpeg";
127 elseif(strcasecmp($ext, "gif") == 0){
128 $create_function = "imagecreatefromgif";
129 $thumb_function = "imagegif";
132 elseif(strcasecmp($ext, "png") == 0){
133 $create_function = "imagecreatefrompng";
134 $thumb_function = "imagepng";
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);
142 if($size[0] < $max_width || $size[1] < $max_height){
145 if($width == $max_width && $height == $max_height){
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);
159 $src_y = floor($img_cut_height);
161 $src_h = floor($size[1] - (2 * $img_cut_height));
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);
169 $src_w = floor($size[0] - (2 * $img_cut_width));
174 $dst_x = floor(($max_width - $width) / 2);
175 $dst_y = floor(($max_height - $height) / 2);
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){
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")
215 function new_thumb_file_name($dest, $file_name, $prefix = "img_"){
216 $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : "";
219 if(strpos($file_name, ".") !== false){
220 $v_ext_path = explode(".", $file_name);
221 $ext = ".".$v_ext_path[count($v_ext_path) - 1];
224 while(file_exists($dest.$prefix.$i.$ext)) $i++;
225 return $prefix.$i.$ext;