Skip to content

Instantly share code, notes, and snippets.

@xrogaan
Created May 7, 2011 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xrogaan/960810 to your computer and use it in GitHub Desktop.
Save xrogaan/960810 to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
define('VERSION','1.0.2');
/* Copyright
** +------------------------------------------------------------
** | Copyright (C) 2005 by Belliere Ludovic
** | xrogaan@intuxication.org
** |
** | This program is free software; you can redistribute it and/or modify
** | it under the terms of the GNU General Public License as published by
** | the Free Software Foundation; either version 2 of the License, or
** | (at your option) any later version.
** |
** | This program is distributed in the hope that it will be useful,
** | but WITHOUT ANY WARRANTY; without even the implied warranty of
** | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** | GNU General Public License for more details.
** |
** | You should have received a copy of the GNU General Public License
** | along with this program; if not, write to the
** | Free Software Foundation, Inc.,
** | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
** +------------------------------------------------------------
** | Titre : Maemise - goodies
** | Version : 1.0.2
** | Notes : Voir fichier README
** |
** +------------------------------------------------------------
*/
/* Configuration
** +------------------------------------------------------------
** | Informations sur les répertoires
** +------------------------------------------------------------
*/
// Dossier a parsé.
// - NOTE : N'a pas été testé avec un dossier ne figurant pas sur la même base que ce fichier
$STR_DIR_GOODIE = './goodie/';
$STR_NAME = 'Goodie'; // Nom de la page
$STR_NOT_LISTING = array('.','..','','.htaccess','.htpasswd'); // fichier a ignorer
/* Configuration
** +------------------------------------------------------------
** | Informations sur les fichiers images
** +------------------------------------------------------------
*/
// Valeur pour alt= dans la balise img
$STR_TEXT_REPLACEMENT = 'Maemise - goodie.';
// Adresse web de base, $STR_DIR_GOODIE ne doit pas être présent, il sera ajouter.
// Ne pas mettre de slash (/) a la fin.
$HTTP_HOST_BASE = 'http://xrogaan.intuxication.org/xylfriid/liste/';
/** Chaine qui contiendra les images et codes
**
* Par défaut, la chaine représente ceci
* <p><img></p>
* <pre><img src="" height="" width="" alt=""> </pre><br/>
*/
$HTML_OUTPUT = "<p>%1\$s</p>\n"
."<pre onclick=\"disable_showDiv(0)\">&#60;img src=\"%2\$s\"\n"
."heigth=\"%3\$s\" width=\"%4\$s\" alt=\"".$STR_TEXT_REPLACEMENT."\" /&#62;</pre><br/>"
."\n\r";
// Représente %1\$s (<img>) dans la chaine $HTML_OUTPUT
// La première est si il n'y a pas de thumbernail, la deuxième, s'il y en a une
$HTML_IMG_OUTPUT = '<img src="%1$s" alt="%5$s" %2$s /> - %3$sX%4$s pixels %7$s';
$HTML_IMG_OUTPUT_THUMB = '<a href="%1$s" onclick="disable_showDiv(0)"><img src="%6$s" alt="%5$s" %2$s /></a> - %3$sX%4$s pixels %7$s';
$HTML_IMG_NOOUTPUT = '<a href="%1$s" onclick="disable_showDiv(0)"><strong>%4$s</strong></a> - %2$sX%3$s pixels %5$s';
/*
** +------------------------------------------------------------
** | Système interne
** +------------------------------------------------------------
** - Ne modifiez que si vous savez ce que vous faites.
*/
$content = array();
class images
{
/**
La hauteur et largeur minimum pour qu'un rapetissement de l'image s'effectue.
*/
private $max_width = 180;
private $max_height = 180;
/**
Saisir le type de d'image a la sortie.
- 1 = GIF (si possible, sinon utilisation du jpeg)
- 2 = JPEG
- 3 = PNG
*/
private $output_type = 3;
/**
Faire une image jpeg entrelacée si la valeur est a 1.
N'est pris en compte que si l'image de sortie est du type jpeg
*/
private $jpeg_interlace = 1;
/**
Dossier de stockage des thumbnails
*/
private $cache_path = './cache/';
private $image;
private $image_name;
private $name;
private $type;
private $attr;
private $gif_enabled;
private $noThumb;
private $noThumb_width;
private $noThumb_height;
public $width;
public $height;
public $affiche;
public function images($image,$dossier)
{
$this->image = $image;
$this->directorie = $dossier;
if ( !file_exists($this->image) || !is_readable($this->image) )
die ('L\'image est illisible ou introuvable<br/>Chaine : <b>'.$this->image.'</b>');
$this->images_details();
$this->gif_enabled = (function_exists('imagecreatefromgif')) ? true : false ;
$this->affiche = (!in_array($this->type,array(1,2,3))) ? false : true ;
}
private function images_details()
{
list($this->width, $this->height, $this->type, $this->attr) = getimagesize($this->image);
$this->name = basename($this->image);
}
public function return_image()
{
global $STR_TEXT_REPLACEMENT , $HTML_IMG_OUTPUT , $HTML_IMG_OUTPUT_THUMB;
$non_affiche = '';
if ( !$this->affiche ) {
global $HTML_IMG_NOOUTPUT;
$non_affiche = '<span class="error"> Image non-affichable par un navigateur web </span>';
return sprintf($HTML_IMG_NOOUTPUT,$this->image,
$this->width,
$this->height,
$this->name,
$non_affiche);
}
if ( ($this->width > $this->max_width) || ($this->height > $this->max_height) ) {
$name = $this->create_thumbnail();
// Support pour les images ne pouvant pas être redimentioné
if ( $this->noThumb === true ) {
// echo $this->name,' - ',$this->type,'<br/>';
return sprintf($HTML_IMG_OUTPUT_THUMB,$this->image,'width="'.$this->noThumb_width.'" height="'.$this->noThumb_height.'"',$this->width,$this->height,$this->name,$this->image,$non_affiche);
}
return sprintf($HTML_IMG_OUTPUT_THUMB,$this->image,'',$this->width,$this->height,$this->name,$name,$non_affiche);
} else {
return sprintf($HTML_IMG_OUTPUT,$this->image,$this->attr,$this->width,$this->height,$this->name,'',$non_affiche);
}
}
public function return_image_info()
{
return array($this->type,$this->width, $this->height);
}
private function update_cache($md5_s)
{
$md5 = md5_file(realpath($this->image));
if ( !file_exists($this->cache_path.$md5) ) {
return true;
} else {
return false;
}
}
private function create_thumbnail()
{
$this->images_details();
$tmp_name = $this->name;
$this->image_name = strtolower(substr($tmp_name, 0, strpos($tmp_name,'.')));
$md5 = md5_file(realpath($this->image));
if (!$this->update_cache($md5)) { return $this->cache_path.$md5; }
switch ( $this->type )
{
case 1: // On teste si le serveur supporte le format gif
if ($this->gif_enabled) {
$from_img = imageCreateFromGif($this->image);
} else {
$this->noThumb = true;
}
break;
case 2: $from_img = imageCreateFromJpeg($this->image); break;
case 3: $from_img = imageCreateFromPng ($this->image); break;
default: $this->noThumb = true; break;
}
if ( $this->width > $this->max_width)
{
$new_img_height = round($this->height * $this->max_width / $this->width);
$new_img_width = $this->max_width;
}
if ( $this->height > $this->max_height)
{
$new_img_width = round($this->width * $this->max_height / $this->height);
$new_img_height = $this->max_height;
}
if ($this->noThumb === true) {
$this->noThumb_width = $new_img_width;
$this->noThumb_height = $new_img_height;
return false;
}
$new_img = imageCreateTrueColor($new_img_width, $new_img_height);
imageCopyResampled($new_img, $from_img, 0,0,0,0, $new_img_width, $new_img_height, $this->width, $this->height);
switch ( $this->output_type )
{
case 1:
if ($this->gif_enabled) {
imageGIF ($new_img,$this->cache_path.$md5);
break;
}
case 2:
imageInterlace($new_img, $this->jpeg_interlace);
imageJPEG($new_img,$this->cache_path.$md5,100);
break;
case 3:
imagePNG ($new_img,$this->cache_path.$md5);
break;
}
chmod($this->cache_path.$md5,0776);
imagedestroy($from_img);
imagedestroy($new_img);
return $this->cache_path.$md5;
}
}
/**
Crée la liste des dossiers/fichiers
*/
function list_all($dir='',$recurcive='')
{
global $STR_DIR_GOODIE , $STR_NOT_LISTING;
$dir = ($dir == '') ? $STR_DIR_GOODIE : $dir ;
$o_dir = opendir($dir);
$list = array();
while (false !== ($in_dir = readdir($o_dir))) // Dossier 1
{
if ( !in_array($in_dir,$STR_NOT_LISTING) )
{
$temp = $dir.$in_dir;
if ( is_dir($temp) ) {
if ( $recurcive == '' )
{
foreach(list_all($temp.'/',1) as $id => $value)
{
if ( !is_int($id) ) {
if ( is_dir( $sdir = realpath($temp.'/'.$id) ) ) {
$so_dir = opendir($sdir);
while (false !== ($in_sdir = readdir($so_dir))) // Sous dossier
{
if ( !in_array($in_sdir,$STR_NOT_LISTING) ) {
$stemp = $sdir.'/'.$in_sdir;
if ( is_file($stemp) ) {
$cat = substr(strrchr($sdir,'/'),1);
$c = ( !isset($list[$in_dir][$cat]) ) ? 1 : count($list[$in_dir][$cat])+1;
$list[$in_dir][$cat][$c] = $stemp;
}
}
}
}
} elseif ( !isset($list[$in_dir][$id]) ) { // La catégorie existe pas
$list[$in_dir][$id] = $value;
} else {
$it = count($list[$in_dir])+1;
$list[$in_dir][$it] = $value;
unset($it);
}
}
}
else
{ // on a rappeler la fonction, donc récurcivitée.
$i = ( isset($list[$in_dir]) ) ? count($list[$in_dir])+1 : 0 ;
$list[$in_dir][$i] = list_all($temp,1);
}
}
elseif ( is_file($temp) ) { // Pas de dossier. On assigne la sans catégorie
$list[] = $temp;
}
}
}
return $list;
}
/**
Via la liste créée par list_all(), crée une autre liste avec les informations des fichiers images.
*/
function parse_list (&$list)
{
global $STR_DIR_GOODIE , $HTTP_HOST_BASE , $STR_TEXT_REPLACEMENT , $HTML_OUTPUT , $content ;
$image_dir = $STR_DIR_GOODIE;
foreach ($list as $base => $name)
{
$name_cat = ( !is_string($base) && is_int($base) ) ? 'Sans catégorie' : strtr($base,'./','') ;
if ( !isset($content[$name_cat]) ) { $content[$name_cat] = ''; }
if ( is_array($name) ) {
foreach ($name as $dir => $fname)
{
if ( is_array($fname) ) {
foreach ( $fname as $sdir => $sfname )
{
$nc = $name_cat.'.sd:'.$dir;
if ( !isset($content[$nc]) ) { $content[$nc] = ''; }
$adresse = $HTTP_HOST_BASE.substr($sfname,1);
$sfname = substr($sfname,strpos($sfname,trim(strtr($STR_DIR_GOODIE,'./',' '))),strlen($sfname));
$adresse = $HTTP_HOST_BASE.$sfname;
$sfname = '.'.$sfname;
$image = new images($sfname,$image_dir);
$content[$nc].= sprintf($HTML_OUTPUT,$image->return_image(),$adresse,$image->height,$image->width);
} // end foreach sous-dir
} else { // pas de sous-cat
$image = new images($fname,$image_dir);
$adresse = $HTTP_HOST_BASE.substr($fname,1);
$content[$name_cat].= sprintf($HTML_OUTPUT,$image->return_image(),$adresse,$image->height,$image->width);
}
} // end foreach1
} else { // no catégory
$image = new images($name,$image_dir);
$adresse = $HTTP_HOST_BASE.substr($name,1);
$content[$name_cat].= sprintf($HTML_OUTPUT,$image->return_image(),$adresse,$image->height,$image->width);
}
}
unset ($list);
}
/**
Transforme les caractères éligible du nom du répertoir en vue de créé un id (id="")
*/
function create_id($name)
{
return strtr($name,'0123456789- êëéèàáâäàöôòóùúüû!?()','abcdefghij__eeeeaaaaaoooouuuu____');
}
$lall = list_all();
parse_list($lall);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title> <?php echo $STR_NAME ?> </title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
var no_set_display = 1;
function disable_showDiv(load) {
if ( load == 1 ) { no_set_display = 0 ; }
else { no_set_display = (no_set_display == 1 ) ? 0 : 1; }
}
function showDiv(name) {
if ( no_set_display == 0 ) {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(name).style.display = (document.getElementById(name).style.display == 'block') ? 'none' : 'block';
} else if (document.layers) { // Netscape 4
document.layers[name].display = (document.layers[name].display == 'block') ? 'none' : 'block' ;
} else { // IE 4
document.all[name].style.display = (document.all[name].style.display == 'block') ? 'none' : 'block' ;
}
}
}
-->
</script>
</head>
<body onload="disable_showDiv(1)">
<?php
ksort($content);
$str_template = "\n\r<div %4\$s><h%1\$d>%2\$s</h%1\$d>".
"\n\t<div %5\$s>\n\t%3\$s\n\t</div>\n</div>\n\r";
// Compilage des données avant l'affichage.
foreach ( $content as $name_cat => $str_to_copy )
{
if ( strstr($name_cat,'.sd:') === FALSE ) {
$id = create_id($name_cat);
$mem[$name_cat]['begin'] = '<div class="cat">'.sprintf($str_template,1,htmlentities($name_cat,ENT_COMPAT,'ISO8859-15'),$str_to_copy,'class="base" onclick="showDiv(\''.$id.'\')"','id="'.$id.'" class="hidden"');
} else {
$eval = preg_replace('@^(.*?)\.sd:(.*?)$@','$name = \'\\1\'; $name_cat=\'\\2\';',$name_cat);
eval( $eval );
if ( !isset($mem[$name]['add']) ) { $mem[$name]['add'] = ''; }
$id = create_id($name_cat);
$mem[$name]['add'].= sprintf($str_template,2,$name_cat,$str_to_copy,'class="content" onclick="showDiv(\''.$id.'\')"','class="text_content" id="'.$id.'"');
}
}
unset($name,$name_cat,$content);
foreach ( $mem as $name_cat => $content)
{
if ( isset($mem[$name_cat]['begin'] ) ) { echo $mem[$name_cat]['begin']; }
if ( isset($mem[$name_cat]['add']) ) { echo $mem[$name_cat]['add']; }
echo '</div>';
}
?>
<p id="bottom">Maemise - goodies <?php echo VERSION ?> &copy; 2005 <a href="http://xrogaan.intuxication.org/">Xrogaan</a><br/>
<a href="http://validator.w3.org/check?uri=referer">Valid XHTML 1.0 Strict</a> - <a href="http://jigsaw.w3.org/css-validator/check?uri=referer">Valid CSS2</a></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment