3 class mw_data_thumbs extends mw_data{
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;
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;
16 $thumbs = $this->data_list(
18 "table_name" => "thumbs",
20 array("src", "eq", $src),
21 array("max_width", "eq", $max_width),
22 array("max_height", "eq", $max_height)
31 $thumb = reset($thumbs["list"]);
32 if(file_exists($thumbs_dir.$thumb["thumb_file"])){
38 "table_name" => "thumbs",
40 "index_value" => $thumb["id"]
47 if(($thumb_file = $this->new_thumb_file_name($thumbs_dir.$thumb_dir, $src, "img_")) === false){
50 $thumb_file = $thumb_dir.$thumb_file;
53 $thumb = $this->make_thumb(
69 "table_name" => "thumbs",
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")
96 $background_color = array(255, 255, 255)
98 $dest = $thumbs_dir.$thumb_file;
99 if(!($size = @getimagesize($src))) return false;
101 if($size[0] > $size[1]){
103 $height = ($size[1] * ($width / $size[0]));
106 $height = $max_height;
107 $width = $size[0] * ($height / $size[1]);
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";
115 $create_function = "";
116 $thumb_function = "";
117 if(strcasecmp($ext, "jpg") == 0){
118 $create_function = "imagecreatefromjpeg";
119 $thumb_function = "imagejpeg";
121 elseif(strcasecmp($ext, "gif") == 0){
122 $create_function = "imagecreatefromgif";
123 $thumb_function = "imagegif";
126 elseif(strcasecmp($ext, "png") == 0){
127 $create_function = "imagecreatefrompng";
128 $thumb_function = "imagepng";
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);
138 floor(($max_width - $width) / 2),
139 floor(($max_height - $height) / 2),
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){
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")
167 function new_thumb_file_name($dest, $file_name, $prefix = "img_"){
168 $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : "";
171 if(strpos($file_name, ".") !== false){
172 $v_ext_path = explode(".", $file_name);
173 $ext = ".".$v_ext_path[count($v_ext_path) - 1];
176 while(file_exists($dest.$prefix.$i.$ext)) $i++;
177 return $prefix.$i.$ext;