xenon54

  • Signature
    Génération PHP.net
  • Site web
  • Nombre de sujets
    2
  • Nombre de messages
    14
  • Nombre de commentaires
    1
  • Nombre de news
    Aucune
  • Niveau en PHP
    Débutant

Ses derniers messages sur les forums

xenon54
le 25/02/2005 à 21:56
correction
Merci encore!
Xenon_54 qui vient de recevoir son livre "PHP5 avancé"

:)
Génération PHP.net
xenon54
le 14/02/2005 à 23:10
correction
J'ai terminé premier :)

Un gros merci à LA GLOBULE et son équipe!
Génération PHP.net
xenon54
le 14/02/2005 à 17:14
correction
J'ai reçu un mail vide

smiley
Génération PHP.net
xenon54
le 09/02/2005 à 21:40
correction
Sérieusement, elle est pour quand la correction? Car ça fait pas très sérieux tout ça. Surtout quand on sait que la précédente version du concours était relativement réputée.

smiley
Génération PHP.net
xenon54
le 25/01/2005 à 03:05
correction
smiley
Génération PHP.net
xenon54
le 25/01/2005 à 03:01
thumbnail.class.php
J'ai pu créé dernièrement durant mes temps libre une classe totalement PHP5 permettant la redimension d'images de types courants soit : JPG, GIF, BMP ou PNG. Les dimensions maximales avant redimension devoit être indiquée si l'on désire que redimension il y ait.
La transparence des PNG est supporté et aucun fond noir ne sera généré. La classe nécessite la compilation de la librairie libexif.

Désolé pour les anglophobes, je code toujours en anglais. :)

<?php
class thumbnail {

private $types = array(
IMAGETYPE_JPEG => 'jpeg',
IMAGETYPE_GIF => 'gif',
IMAGETYPE_BMP => 'wbmp',
IMAGETYPE_PNG => 'png'
);

private $thumbnaildir = './';

public $maxWidth = 0;
public $maxHeight = 0;


public function __construct($thumbnaildir=NULL) {

if (FALSE === extension_loaded('gd')) {
throw new Exception('gd must be compiled to use this class.');
}

if (FALSE === extension_loaded('exif')) {
throw new Exception('libexif must be compiled to use this class.');
}

$this->thumbnaildir = $thumbnaildir;

}

public function execute($filename) {

if (FALSE === file_exists($filename)) {
throw new Exception('No such file.');
}

if (FALSE === $type = exif_imagetype($filename)) {
throw new Exception('File is not an image.');
}


if (FALSE === array_key_exists($type, $this->types)) {
throw new Exception('Image type is not supported by this class.');
} else {
$type = $this->types[$type];
}

try {
$this->validation();
$result = $this->resize($filename, $type);
} catch ( Exception $e ) {
throw $e;
}

return $result;
}

private function resize($filename, $type) {

if (0 === $this->maxHeight && 0 === $this->maxWidth) {
return TRUE;
}

list($width, $height) = getimagesize($filename);

if ($width <= $this->maxWidth && $height <= $this->maxHeight) {
return TRUE;
}

if ($width > $height) {
$thumbnail_width = $this->maxWidth;
$thumbnail_height = (int) ($this->maxWidth * $height / $width);
} else {
$thumbnail_width = (int) ($this->maxHeight * $width / $height);
$thumbnail_height = $this->maxHeight;
}

$read = 'imagecreatefrom' . $type;
$write = 'image' . $type;

if (FALSE === function_exists($write) ) {
throw new Exception('Image type is not supported by your GD library.');
}

if (FALSE === $img = $read($filename) ) {
throw new Exception('An exception occured while opening original file.');
}

if ('gif' === $type) {
$des = imagecreate($thumbnail_width, $thumbnail_height);
$resize = 'imagecopyresized';
} else {
$des = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
$resize = 'imagecopyresampled';
}

if ('png' === $type) {

if (FALSE === imagealphablending($des, FALSE) ) {
throw new Exception('Enable to activate full alpha information.');
}
if (FALSE === imagesavealpha($des, TRUE)) {
throw new Exception('Enable to activate full alpha information.');
}
}

if (FALSE === $resize($des, $img, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $width, $height)) {
throw new Exception('An exception occured while resizing the original file.');
}

if (FALSE === $write($des, $this->thumbnaildir . '/' . $filename)) {
throw new Exception('An exception occured while writing thumbnail file.');
}

return TRUE;
}


private function validation() {

if (FALSE === is_writable($this->thumbnaildir)) {
throw new Exception('Thumbnails directory is not writable.');
}

if (FALSE === is_int($this->maxWidth) || FALSE === is_int($this->maxHeight)) {
throw new Exception('Maximum dimensions must be integer.');
}

if (0 > $this->maxWidth || 0 > $this->maxHeight) {
throw new Exception('Maximum dimensions must be positive integer.');
}
}


}

// Exemple d'utilisation
try {
$thumb = new thumbnail('./thumbs/');
$thumb->maxWidth = 80;
$thumb->maxHeight = 60;
$thumb->execute('image.png');
} catch (Exception $e) {

echo '';
echo 'Error message: ' , $e->getMessage() , '';
echo 'Error code: ' , $e->getCode() , '';
echo 'Script Name: ' , $e->getFile() , '';
echo 'Line Number: ' , $e->getLine() , '';
}
?>
Génération PHP.net
xenon54
le 23/01/2005 à 19:38
Avis
Personnellement, je préfaire un projet qui "impose" une manière de coder puisque dans le cas contraire, ce ne serait plus un concours.

Il est important que chacun puisse apprendre de nouvelles techniques et ne plus se limiter qu'à leur monde fermé de la programmation procédural avec register_globals à ON.

Sincérement, grandissez un peu et voyez le bien-fait de la programmation objet et propre grâce à l'utilisation des bonnes techniques. Vous n'en resortirai que meilleur.

;)
Génération PHP.net
xenon54
le 10/01/2005 à 23:52
Vos avis sur le sujet :)
Perso, j'ai terminé et je peux affirmer qu'il y a place à amélioration.

:)
Génération PHP.net
xenon54
le 10/01/2005 à 22:22
Type du champs
Type numérique, ça peut être de type float?
Génération PHP.net
LoadingChargement en cours