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. :)
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() , '';
}
?>