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)){
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(
73 "table_name" => "thumbs",
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")
100 $background_color = array(255, 255, 255)
102 $dest = $thumbs_dir.$thumb_file;
103 if(!($size = @getimagesize($src))) return false;
105 if($size[0] > $size[1]){
107 $height = ($size[1] * ($width / $size[0]));
110 $height = $max_height;
111 $width = $size[0] * ($height / $size[1]);
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";
119 $create_function = "";
120 $thumb_function = "";
121 if(strcasecmp($ext, "jpg") == 0){
122 $create_function = "imagecreatefromjpeg";
123 $thumb_function = "imagejpeg";
125 elseif(strcasecmp($ext, "gif") == 0){
126 $create_function = "imagecreatefromgif";
127 $thumb_function = "imagegif";
130 elseif(strcasecmp($ext, "png") == 0){
131 $create_function = "imagecreatefrompng";
132 $thumb_function = "imagepng";
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);
142 floor(($max_width - $width) / 2),
143 floor(($max_height - $height) / 2),
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){
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")
171 function new_thumb_file_name($dest, $file_name, $prefix = "img_"){
172 $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : "";
175 if(strpos($file_name, ".") !== false){
176 $v_ext_path = explode(".", $file_name);
177 $ext = ".".$v_ext_path[count($v_ext_path) - 1];
180 while(file_exists($dest.$prefix.$i.$ext)) $i++;
181 return $prefix.$i.$ext;