Redimensionner une série d’images

Mammouth du PHP | 19672 Messages

12 janv. 2006, 23:12

[Résolu] alors ?
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse :axe:

Eléphant du PHP | 355 Messages

12 janv. 2006, 23:16

Non le script que j'ai cité plus haut ne redimentionne pas l'image, enfin pas le fichier.jpg, il redimention la tail de l'image sur la page s'est tout, je recherche un script qui redimentionne un fichier.jpg.

Mammouth du PHP | 19672 Messages

12 janv. 2006, 23:38

Il t'avait été suggéré le forum des développeur : j'y ai trouvé ce sujet, c'est du PHP5, mais ça doit pouvoir s'adapter au besoin.
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse :axe:

Eléphant du PHP | 355 Messages

12 janv. 2006, 23:47

Merci beaucoup :wink:

Donc je doit appeler cette classe par un autre fichier.php ?

Mammouth du PHP | 19672 Messages

12 janv. 2006, 23:48

:shock: Question à deux balles : sais-tu utiliser une classe objet ?
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse :axe:

Eléphant du PHP | 355 Messages

12 janv. 2006, 23:48

Ben non :oops:

Mammouth du PHP | 19672 Messages

12 janv. 2006, 23:54

Alors on est mal barrés là : faut pas rester silencieux quand tu sais pas une chose, dis-le immédiatement on perdra moins de temps.

Tu as deux options possibles :
- comprendre le fonctionnement du code de la classe en question et te faire ta propre fonction en procédural (donc pas objet)
- Apprendre les bases de la programmation objet, ce qu'est un objet, une classe, une instance de classe, etc... ça ne s'apprend pas en trois minutes.

Ceci dit, essaye une chose : dans un fichier, fais un include de la classe en question et teste les exemples d'utilisation indiqués en bas du code de Xenon.
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse :axe:

Eléphant du PHP | 355 Messages

13 janv. 2006, 00:00

J'avai pas vu qu'on pouvai modifier le nom de l'image en bas, j'ai donc mis le nom d'un image de test.jpg mais je code me renvoi une erreur:

Code : Tout sélectionner

Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in h:\www\class_redimimg.php on line 5

Merci pour ses conseille :wink:

Mammouth du PHP | 19672 Messages

13 janv. 2006, 00:03

je me lancerai pas dans un cours ce soir : il manque probablement un point-virgule quelque part.
Codez en pensant que celui qui maintiendra votre code est un psychopathe qui connait votre adresse :axe:

Mammouth du PHP | 1311 Messages

13 janv. 2006, 00:16

si tu a utiliser la class
fait voir comment tu a fait ??

Eléphant du PHP | 355 Messages

13 janv. 2006, 00:22

Voila le code:
<?php 

class thumbnail { 

    private $types = array( 
        IMAGETYPE_JPEG => 'jpeg', 
        IMAGETYPE_GIF => 'gif', 
        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 = 80; 
    $thumb->execute('couchersoleil.jpg'); 
} catch (exception $e) { 

    echo '<br />'; 
    echo 'Error message: ' , $e->getMessage() , '<br />'; 
    echo 'Error code: ' , $e->getCode() , '<br />'; 
    echo 'Script Name: ' , $e->getFile() , '<br />'; 
    echo 'Line Number: ' , $e->getLine() , '<br />'; 
} 
?> 

Mammouth du PHP | 1311 Messages

13 janv. 2006, 00:31

chez moi elle fonctionne tres bien
qelle version de php a tu (phpinfo)

Eléphant du PHP | 355 Messages

13 janv. 2006, 00:40

Je suis sous EasyPhp, la version de php est: PHP Version 4.3.10

Mammouth du PHP | 1311 Messages

13 janv. 2006, 00:50

pour te servir de la classe a xenon tu doit etre en php5
j'ai une adaptation rapide pour php4 mais je te promet pas quelle fonctione
et tu doit rajouter libexif( comme tu la fait pour gd)
<?php

class thumbnail {

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

    var $thumbnaildir = './';

   var $maxWidth = 0;
  var $maxHeight = 0;


    public function thumbnail($thumbnaildir=NULL) {

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

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

        $this->thumbnaildir = $thumbnaildir;

    }

    public function execute($filename) {

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

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


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

        
            $this->validation();
            $result = $this->resize($filename, $type);
     

        return $result;
    }

     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) ) {
           echo ('Image type is not supported by your GD library.');
        }

        if (FALSE === $img = $read($filename) ) {
            echo ('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) ) {
                echo ('Enable to activate full alpha information.');
            }
            if (FALSE === imagesavealpha($des, TRUE)) {
                echo('Enable to activate full alpha information.');
            }
        }

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

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

        return TRUE;
    }


     function validation() {

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

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

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


}

// Exemple d'utilisation

    $thumb = new thumbnail('./thumbs/');
    $thumb->maxWidth = 80;
    $thumb->maxHeight = 80;
    $thumb->execute('couchersoleil.jpg');


Eléphant du PHP | 355 Messages

13 janv. 2006, 00:56

Merci :wink:

Je n'est pas de libexif chez moi dans mon fichier php.ini :shock: