--- /dev/null
+<?php
+
+ class mw_data_thumbs extends mw_data{
+
+ function img_thumb($src, $max_width, $max_height, $thumbs_dir = "", $background_color = array(255, 255, 255)){
+ if(strlen($thumbs_dir) > 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
+ )
+ ) === 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;
+ }
+
+ function make_thumb(
+ $src,
+ $max_width,
+ $max_height,
+ $thumbs_dir,
+ $thumb_file,
+ $quality = null,
+ $background_color = array(255, 255, 255)
+ ){
+ $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);
+ imagecopyresampled(
+ $thumb_img,
+ $src_img,
+ floor(($max_width - $width) / 2),
+ floor(($max_height - $height) / 2),
+ 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" => $max_width,
+ "thumb_height" => $max_height,
+ "creation_date" => date("Y-m-d H:i:s")
+ );
+ }
+ }
+ }
+ return false;
+ }
+
+ 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;
+ }
+
+ }
+
+?>
\ No newline at end of file
--- /dev/null
+<?php
+
+ class mw_thumbs extends mw_plugin{
+
+ function title(){
+ return "Thumbs";
+ }
+
+ function description(){
+ return "Un générateur de miniatures";
+ }
+
+ function install($env){
+ if(
+ $env->bdd("sgbd") == "mysql"
+ || $env->bdd("sgbd") == "pdo_mysql"
+ ){
+ return $this->install_mysql($env);
+ }
+ elseif(
+ $env->bdd("sgbd") == "pdo_sqlite"
+ ){
+ return $this->install_sqlite($env);
+ }
+ elseif(
+ $env->bdd("sgbd") == "xml"
+ ){
+ return $this->install_xml($env);
+ }
+ else{
+ return "Mode de stockage pour Mtweb (".$env->bdd("sgbd").") non supporté par mw_thumbs";
+ }
+ return true;
+ }
+
+ function install_mysql($env){
+ $data = $env->data();
+ $sgbd = $data->sgbd();
+ try{
+ $EXISTS = $sgbd->table_exists("#--thumbs");
+ }
+ catch(Exception $e){
+ return "impossible de savoir si la table #--thumbs existe";
+ }
+ if($EXISTS){
+ return "la table #--thumbs existe deja";
+ }
+ try{
+ $sql =
+ "CREATE TABLE #--thumbs("\r
+ ." `id` int(11) NOT NULL AUTO_INCREMENT,"\r
+ ." `src` varchar(255) NOT NULL DEFAULT '',"\r
+ ." `src_width` int(11) NOT NULL DEFAULT '0',"\r
+ ." `src_height` int(11) NOT NULL DEFAULT '0',"\r
+ ." `max_width` int(11) NOT NULL DEFAULT '0',"\r
+ ." `max_height` int(11) NOT NULL DEFAULT '0',"\r
+ ." `thumb_file` varchar(255) NOT NULL,"\r
+ ." `thumb_width` int(11) NOT NULL DEFAULT '0',"\r
+ ." `thumb_height` int(11) NOT NULL DEFAULT '0',"\r
+ ." `creation_date` datetime NOT NULL,"\r
+ ." PRIMARY KEY (`id`)"\r
+ .")";
+ $sgbd->query($sql);
+ }
+ catch(Exception $e){
+ return "imposible de creer la table #--thumbs";
+ }
+ return true;
+ }
+
+ function install_sqlite($env){
+ $data = $env->data();
+ $sgbd = $data->sgbd();
+ try{
+ $EXISTS = $sgbd->table_exists("#--thumbs");
+ }
+ catch(Exception $e){
+ return "impossible de savoir si la table #--thumbs existe";
+ }
+ if($EXISTS){
+ return "la table #--thumbs existe deja";
+ }
+ try{
+ $sql =
+ "CREATE TABLE #--thumbs("\r
+ ." `id` INTEGER PRIMARY KEY AUTOINCREMENT"\r
+ ." `src` TEXT NOT NULL DEFAULT '',"\r
+ ." `src_width` INTEGER NOT NULL DEFAULT '0',"\r
+ ." `src_height` INTEGER NOT NULL DEFAULT '0',"\r
+ ." `max_width` INTEGER NOT NULL DEFAULT '0',"\r
+ ." `max_height` INTEGER NOT NULL DEFAULT '0',"\r
+ ." `thumb_file` TEXT NOT NULL,"\r
+ ." `thumb_width` INTEGER NOT NULL DEFAULT '0',"\r
+ ." `thumb_height` INTEGER NOT NULL DEFAULT '0',"\r
+ ." `creation_date` TEXT NOT NULL"\r
+ .")";
+ $sgbd->query($sql);
+ }
+ catch(Exception $e){
+ return "imposible de creer la table #--thumbs";
+ }
+ return true;
+ }
+
+ function install_xml($env){
+ $data = $env->data();
+ $sgbd = $data->sgbd();
+ $EXISTS = $sgbd->data_exists("thumbs");
+ if(!isset($EXISTS)){
+ return "impossible de savoir si la table #--thumbs existe";
+ }
+ if($EXISTS){
+ return "la table #--thumbs existe deja";
+ }
+ if(!$sgbd->create_data("thumbs")){
+ return "imposible de creer la table #--thumbs";
+ }
+ return true;
+ }
+
+ function uninstall($env){
+ if($env->bdd("sgbd") == "xml") return $this->uninstall_xml($env);
+ else return $this->uninstall_sql($env);
+ }
+
+ function uninstall_xml($env){
+ $data = $env->data();
+ $sgbd = $data->sgbd();
+ if(!$this->disable($env)) return "impossible de desactiver le plugin";
+ $EXISTS = $sgbd->data_exists("thumbs");
+ if(!isset($EXISTS)){
+ return "impossible de savoir si la table #--thumbs existe";
+ }
+ if(!$EXISTS){
+ // return "la table #--thumbs n'existe pas";
+ }
+ elseif(!$sgbd->remove_data("thumbs")){
+ return "imposible de supprimer la table #--thumbs";
+ }
+ return true;
+ }
+
+ function uninstall_sql($env){
+ $data = $env->data();
+ $sgbd = $data->sgbd();
+ if(!$this->disable($env)) return "impossible de desactiver le plugin";
+ try{
+ $EXISTS = $sgbd->table_exists("#--thumbs");
+ }
+ catch(Exception $e){
+ return "impossible de savoir si la table #--thumbs existe";
+ }
+ if(!$EXISTS){
+ // return "la table #--thumbs n'existe pas";
+ }
+ else{
+ try{
+ $sgbd->query("DROP TABLE #--thumbs");
+ }
+ catch(Exception $e){
+ return "imposible de supprimer la table #--thumbs";
+ }
+ }
+ return true;
+ }
+
+ }