image_error; } function get_image_message() { return $this->image_message; } function img_size($file, $max_width, $max_height) { $img_infos = getimagesize($file); if($img_infos) { if($img_infos[0] > $max_width || $img_infos[1] > $max_height) { $r = $max_width / $img_infos[0]; if($r * $img_infos[1] > $max_height) $r = $max_height / $img_infos[1]; $img_size = array ( "width" => floor($r * $img_infos[0]), "height" => floor($r * $img_infos[1]) ); } else $img_size = array ( "width" => $img_infos[0], "height" => $img_infos[1] ); return $img_size; } return false; } function unique_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; } function upload_image($image, $dest, $img_file = null) { switch($_FILES[$image]["error"]) { case UPLOAD_ERR_OK : break; default : $this->image_error = "Impossible d'uploader l'image."; } if(!$this->image_error) { $img_file = isset($img_file) ? $img_file : $_FILES[$image]["name"]; $v_name = explode(".", $img_file); $ext = $v_name[count($v_name) - 1]; if ( strcasecmp($ext, "png") == 0 || strcasecmp($ext, "gif") == 0 || strcasecmp($ext, "jpg") == 0 || strcasecmp($ext, "jpeg") == 0 ) { if(file_exists($dest)) { if(!is_dir($dest)) $this->image_error = "le dossier d'upload est un fichier. Impossible d'y uploader l'image"; } else { @mkdir($dest); clearstatcache(); if(!file_exists($dest) && !is_dir($dest)) $this->image_error = "Impossible de creer le dossier d'upload"; } if(!$this->image_error) { if ( @move_uploaded_file ( $_FILES[$image]["tmp_name"], $dest."/".$img_file ) !== false ) return $img_file; else $this->image_error = "Impossible de copier l'image uploadee"; } } else $this->image_message = "Le fichier image doit être au format png, gif ou jpg"; } return false; } function img_thumb($src, $max_width, $max_height, $thumbs_dir = "") { if(strlen($thumbs_dir) > 0) { if(!@is_dir($thumbs_dir)) @mkdir($thumbs_dir); if(!@is_dir($thumbs_dir)) return false; } $thumb = array(); try { $sql = "SELECT *" ." FROM #--thumbs" ." WHERE src=".$this->eq($src) ." AND max_width=".$this->eq($max_width) ." AND max_height=".$this->eq($max_height); $rst = $this->sql->query($sql); if($v_rst = $this->sql->fetch_assoc($rst)) $thumb = $v_rst; $this->sql->free_result($rst); } catch(Exception $_e) { $thumb = false; } if($thumb !== false) { if($thumb) return $thumb; $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; $thumb_file = $thumb_dir.$this->unique_file_name($thumbs_dir.$thumb_dir, $src, "img_"); if($thumb_file === false) return false; if(($thumb = $this->make_thumb($src, $max_width, $max_height, $thumbs_dir, $thumb_file)) !== false) { try { $sql = "INSERT INTO #--thumbs" ."(src, src_width, src_height, max_width, max_height, thumb_file, thumb_width, thumb_height, creation_date) VALUES" ."( ".$this->eq($thumb["src"]) .", ".$this->eq($thumb["src_width"]) .", ".$this->eq($thumb["src_height"]) .", ".$this->eq($max_width) .", ".$this->eq($max_height) .", ".$this->eq($thumb["thumb_file"]) .", ".$this->eq($thumb["thumb_width"]) .", ".$this->eq($thumb["thumb_height"]) .", ".$this->eq($thumb["creation_date"]) .")"; $rst = $this->sql->query($sql); return $thumb; } catch(Exception $_e) {} } } return false; } function make_thumb($src, $max_width, $max_height, $thumbs_dir, $thumb_file, $quality = null) { $dest = $thumbs_dir.$thumb_file; $size = getimagesize($src); 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($width, $height); imagecopyresampled($thumb_img, $src_img, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); 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" => $width, "thumb_height" => $height, "creation_date" => date("Y-m-d H:i:s") ); } } } return false; } } ?>