0){ if(!@is_dir($thumbs_dir)) @mkdir($thumbs_dir); if(!@is_dir($thumbs_dir)) return false; } $thumbs_dir .= strlen($thumbs_dir) > 0 && substr($thumbs_dir, -1) != "/" ? "/" : ""; $thumb_dir = $max_width."x".$max_height."/"; if(!@is_dir($thumbs_dir.$thumb_dir)) @mkdir($thumbs_dir.$thumb_dir); if(!@is_dir($thumbs_dir.$thumb_dir)) return false; if( ( $thumbs = $this->data_list( array( "table_name" => "thumbs", "filters" => array( array("src", "eq", $src), array("max_width", "eq", $max_width), array("max_height", "eq", $max_height) ) ) ) ) === false ){ return false; } if($thumbs["list"]){ $thumb = reset($thumbs["list"]); if(file_exists($thumbs_dir.$thumb["thumb_file"])){ return $thumb; } if( !$this->data_delete( array( "table_name" => "thumbs", "index_name" => "id", "index_value" => $thumb["id"] ) ) ){ return false; } } if(($thumb_file = $this->new_thumb_file_name($thumbs_dir.$thumb_dir, $src, "img_")) === false){ return false; } $thumb_file = $thumb_dir.$thumb_file; if( ( $thumb = $this->make_thumb( $src, $max_width, $max_height, $thumbs_dir, $thumb_file, null, $background_color, $CROP ) ) === false ){ return false; } if( !$this->data_insert( array( "table_name" => "thumbs", "values" => array( "src" => $thumb["src"], "src_width" => $thumb["src_width"], "src_height" => $thumb["src_height"], "max_width" => $max_width, "max_height" => $max_height, "thumb_file" => $thumb["thumb_file"], "thumb_width" => $thumb["thumb_width"], "thumb_height" => $thumb["thumb_height"], "creation_date" => date("Y-m-d H:i:s") ) ) ) ){ return false; } return $thumb; } public function make_thumb( $src, $max_width, $max_height, $thumbs_dir, $thumb_file, $quality = null, $background_color = array(255, 255, 255), $CROP = false ){ $dest = $thumbs_dir.$thumb_file; if(!($size = @getimagesize($src))) return false; if($size[0]){ if($size[0] > $size[1]){ $width = $max_width; $height = ($size[1] * ($width / $size[0])); } else{ $height = $max_height; $width = $size[0] * ($height / $size[1]); } $v_ext_path = explode(".", $src); $ext = $v_ext_path[count($v_ext_path) - 1]; if(strcasecmp($ext, "jpg") == 0 || strcasecmp($ext, "jpeg") == 0) $ext = "jpg"; elseif(strcasecmp($ext, "gif") == 0) $ext = "gif"; elseif(strcasecmp($ext, "png") == 0) $ext = "png"; else $ext = ""; $create_function = ""; $thumb_function = ""; if(strcasecmp($ext, "jpg") == 0){ $create_function = "imagecreatefromjpeg"; $thumb_function = "imagejpeg"; } elseif(strcasecmp($ext, "gif") == 0){ $create_function = "imagecreatefromgif"; $thumb_function = "imagegif"; $quality = NULL; } elseif(strcasecmp($ext, "png") == 0){ $create_function = "imagecreatefrompng"; $thumb_function = "imagepng"; } if($create_function){ $src_img = $create_function($src); $thumb_img = imagecreatetruecolor($max_width, $max_height); $thumb_bkg = imagecolorallocate($thumb_img, $background_color[0], $background_color[1], $background_color[2]); imagefilledrectangle($thumb_img, 0, 0, $max_width, $max_height, $thumb_bkg); if($CROP){ if($size[0] < $max_width || $size[1] < $max_height){ $CROP = false; } if($width == $max_width && $height == $max_height){ $CROP = false; } } if($CROP){ $dst_x = 0; $dst_y = 0; $dst_w = $max_width; $dst_h = $max_height; if($width != $max_width){ $r = $max_width / $size[0]; $thumb_cut_height = (($size[1] * $r) - $max_height) / 2; $img_cut_height = $size[1] * ($thumb_cut_height / $max_height); $src_x = 0; $src_y = floor($img_cut_height); $src_w = $size[0]; $src_h = floor($size[1] - (2 * $img_cut_height)); } else{ $r = $max_height / $size[1]; $thumb_cut_width = (($size[0] * $r) - $max_width) / 2; $img_cut_width = $size[0] * ($thumb_cut_width / $max_width); $src_x = floor($img_cut_width); $src_y = 0; $src_w = floor($size[0] - (2 * $img_cut_width)); $src_h = $size[1]; } } else{ $dst_x = floor(($max_width - $width) / 2); $dst_y = floor(($max_height - $height) / 2); $src_x = 0; $src_y = 0; $dst_w = $width; $dst_h = $height; $src_w = $size[0]; $src_h = $size[1]; } imagecopyresampled( $thumb_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); if(isset($quality)) $thumb_img = $thumb_function($thumb_img, $dest, $quality); else $thumb_img = $thumb_function($thumb_img, $dest); if($thumb_img !== false){ return array( "src" => $src, "src_width" => $size[0], "src_height" => $size[1], "max_width" => $max_width, "max_height" => $max_height, "thumb_file" => $thumb_file, "thumb_width" => $max_width, "thumb_height" => $max_height, "creation_date" => date("Y-m-d H:i:s") ); } } } return false; } public function new_thumb_file_name($dest, $file_name, $prefix = "img_"){ $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : ""; if(is_dir($dest)){ $ext = ""; if(strpos($file_name, ".") !== false){ $v_ext_path = explode(".", $file_name); $ext = ".".$v_ext_path[count($v_ext_path) - 1]; } $i = 0; while(file_exists($dest.$prefix.$i.$ext)) $i++; return $prefix.$i.$ext; } return false; } }