Forum de la C-P-F

Version complète: phpPhotoBook
Vous regardez actuellement la version basse qualité d'un document. Voir la version complète avec le bon formatage.
Effectivement, c'est la réflexion que je me suis faite durant la nuit: la classe prendra en argument les répertoires source et destination et fera simplement une vérification de leur existence (pour avoir une gestion des erreurs potentielles) mais ne les gérera pas.
alors? cette première version ça donne quoi?

Sinon un peu hors sujet, mais je ne suis pas totalement convaincu par l'utilisation du Wiki pour la présentation d'un projet, peut-être que je n'ai pas bien saisi toutes les subtilités.

M'enfin ce n'est qu'un point de vu très personnel.
Elle est presque fonctionnelle, c'est à dire que l'administration fonctionne et le front-office (basé sur des templates) n'est pas loin d'être OK.
J'ai encore quelques points à voir:
1) Correction de la classe "Pictures" pour la rendre plus logique (voir les commentaires de Coca25) et donc adaptation du reste
2) Intégrer la configuration du site dans l'administration (c'est actuellement "en dur")
3) Intégrer des niveaux d'albums (on se demande qui a des idées pareilles)

Sitôt que j'ai un petit peu de temps, je m'occupe du point 1 et du point 3 et je met la version en disponibilité pour que vous puissiez commenter le système dans son ensemble et non plus de simples modules.

Et pour ton HS: le wiki n'est peut-être pas utile actuellement (je te le concède) mais le sera une fois qu'un projet sera "distribué". De toutes manières, il est là, il peut servir à tout, je suis ouvert à vos propositions.
Bon, je balance la dernière version de la classe Pictures, je la commenterais demain après l'avoir débuggée (je viens de la faire "en aveugle").

Il lui manque pas mal de méthodes comme la gestion d'erreur, il n'y a pour l'instant que les principales.

Code PHP :
<?php
/**
 * A small class to work with pictures
 * Using GD library
 *
 * @author CrazyCat <crazycat@c-p-f.org>
 * @version 1.3
 *
 * @package Pictures
 * @access public
 *
 * @todo optimisation
 */


class Pictures{

    /**
     * Name of the picture
     */

    var $name;
   
    /**
     * The source image
     */

    var $source;
   
    /**
     * Origine directory
     */

    var $origin;
   
    /**
     * Destination directory
     */

    var $dest;
   
    /**
     * Max dimensions of the reduced picture
     */

    var $sizeX, $sizeY;
   
    /**
     * Array of error messages
     * @var array $error
     */

    var $error = array();
   
    /**
     * Habilities of the GD library
     * @var boolean $canGif
     * @var boolean $canPNG
     * @var boolean $canJPG
     */

     var $canGif, $canPNG, $canJPG;
     
     /**
      * Array containings infos about the source image
      * @var array $imgInfos
      */

     var $imgInfos = array();
     
    /**
     * The constructor
     *
     * @param string $name
     * @param string $origin
     * @param string $dest
     * @param boolean $debug
     */

    function Pictures($origin, $dest, $debug=0){
        $this->origin = (string) $origin;
        $this->setDest($dest);
        $this->debug = (boolean) $debug;
        (function_exists('imagecreatefromgif')) ? ($this->canGIF = true) : ($this->canGIF = false);
        (function_exists('imagecreatefrompng')) ? ($this->canPNG = true) : ($this->canPNG = false);
        (function_exists('imagecreatefromjpeg')) ? ($this->canJPG = true) : ($this->canJPG = false);
    }
   
    /**
     * Method to set (and create) a destination directory
     * @param string $dest
     */

    function setDest($dest) {
        $curpath = '';
        $subpaths = explode("/", $dest);
        foreach ($subpaths as $pathname) {
            $curpath .= "/".$pathname;
            if (!is_dir($curpath)) {
                if (!mkdir($curpath, 0777)) return false;
            }
        }
        $this->dest = (string) $dest;
    }
   
    /**
     * The method to add a picture to transform
     *
     * @param mixed $source
     * @return boolean
     */

    function addImg($source) {
        if (is_array($source)) {
            if (!$this->origin = $this->uploadImg($source, $this->origin)) {
                $this->error[] = "Unable to move the temp file";
                return false;
            } else {
                return $this->getImgSize();
            }
        } else {
            $this->name = (string) $source;
            $this->origin = $this->origin.'/'.$this->name;
            return $this->getImgSize();
        }
    }
   
    /**
     * The method to resize
     *
     * @param integer $sizeX
     * @param integer $sizeY
     * @param boolean $constrain
     * @param string $method
     *
     * constrain: if set to false, the picture will use sizes and no ratio
     * method: could be reduce or crop
     *
     * @return boolean
     */

    function resizeImg($sizeX, $sizeY, $constrain=true, $method='reduce') {
        $this->sizeX = (integer) $sizeX;
        $this->sizeY = (integer) $sizeY;
        $destname = $this->dest.'/'.$this->name;
        if (($this->sizeX==0) || ($this->sizeX>$this->imgInfos[0])) $this->sizeX = $this->imgInfos[0];
        if (($this->sizeY==0) || ($this->sizeY>$this->imgInfos[1])) $this->sizeY = $this->imgInfos[1];
        // Here we have to calculate the properties of the destination image
        if ($constrain=true) {
            $Xratio = floor($this->sizeX/$this->imgInfos[0]);
            $Yratio = floor($this->sizeY/$this->imgInfos[1]);
            ($Xratio>$Yratio) ? ($ratio = $Yratio) : ($ratio = $Xratio);
            $this->sizeX = floor($ratio*$this->imgInfos[0]);
            $this->sizeY = floor($ratio*$this->imgInfos[1]);
        }
        // A small switch to adapt the properties of the source image
        switch($method) {
            case "crop":
                $srcX = $this->sizeX;
                $srcY = $this->sizeY;
                break;
            default:
                $srcX = $this->imgInfos[0];
                $srcY = $this->imgInfos[1];
        }
        // And now, creating the good ressource
        switch($this->imgInfos[2]) {
            case 1:
                $src = imagecreatefromgif($this->origin);
                $copy = imagecreatetruecolor($this->sizeX, $this->sizeY);
                imagecopyresampled($copy, $src, 0, 0, 0, 0, $this->sizeX, $this->sizeY, $srcX, $srcY);
                if (!imagegif($copy, $destname)) {
                    $this->error[] = "Cannot create ".$destname;
                    return false;
                } else {
                    return true;
                }
                break;
            case 2:
                $src = imagecreatefromjpeg($this->origin);
                $copy = imagecreatetruecolor($this->sizeX, $this->sizeY);
                imagecopyresampled($copy, $src, 0, 0, 0, 0, $this->sizeX, $this->sizeY, $srcX, $srcY);
                if (!imagejpeg($copy, $destname)) {
                    $this->error[] = "Cannot create ".$destname;
                    return false;
                } else {
                    return true;
                }
                break;
            case 3:
                $src = imagecreatefrompng($this->origin);
                $copy = imagecreatetruecolor($this->sizeX, $this->sizeY);
                imagecopyresampled($copy, $src, 0, 0, 0, 0, $this->sizeX, $this->sizeY, $srcX, $srcY);
                if (!imagepng($copy, $destname)) {
                    $this->error[] = "Cannot create ".$destname;
                    return false;
                } else {
                    return true;
                }
                break;
            default:
                return false;
                break;
        }
    }
   
    /**
     * The method to manage uploaded picutres
     *
     * @param array $source Upload informations
     * @param string $origin Directory where to store the file
     * @return string
     */

    function uploadImg($source, $origin) {
        if (!is_uploaded_file($source['tmp_name'])) {
            return false;
        }
        $this->name = date("YmdHis").'_'.$source['name'];
        $dest = $origin.'/'.$this->name;
        if (move_uploaded_file($source['tmp_name'], $dest)) {
            return $dest;
        } else {
            return false;
        }
    }
   
    /**
     * Getting informations about the source picture
     *
     * @return boolean $status
     */

    function getImgSize() {
        if (!$this->imgInfos = getimagesize($this->origin)) {
            $this->error[] = 'Not a picture: '.$this->origin;
            $status = false;
        } else {
            // The image is ok, but we have to verify we can treat it
            switch($this->imageInfos[2]) {
                case 1:
                    if ($this->canGIF===false) {
                        $this->error[] = "Can not treat GIF file";
                        $status = false;
                    }
                    break;
                case 2:
                    if ($this->canJPG===false) {
                        $this->error[] = "Can not treat JPG file";
                        $status = false;
                    }
                    break;
                case 3:
                    if ($this->canPNG===false) {
                        $this->error[] = "Can not treat PNG file";
                        $status = false;
                    }
                    break;
                default:
                    $this->error[] = "Unknown image format";
                    $status = false;
                    break;
            }
        }
        $status = true;
        return $status;
    }
}
?>
Pages: 1 2 3
URLs de référence