IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[GD] Miniature en cache d'image PNG avec transparence


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre à l'essai
    Inscrit en
    Juillet 2008
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Juillet 2008
    Messages : 20
    Points : 14
    Points
    14
    Par défaut [GD] Miniature en cache d'image PNG avec transparence
    Bonjour à tous, j'ai un probleme et je souhaite votre aide s'il vous plait.
    J'ai récupéré un systeme de gestion de miniature. il gère tout partfaitement bien sauf les images png.
    Au final, elle à un fond noir.

    Le probleme, c'est que j'ai pres de 12000 png, je peux donc pas les traiter à la main.

    Voici mon fichier de generation de miniature. je souhaiterais qu'il gère la transparance des png


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
     
    <?php
     
    // function de création d'image
     
     
                            $url = htmlspecialchars (htmlentities  ($_GET['uri'])); //RECUPERE L'ID DE L'ARTICLE EN GET
     
     
                            /* VERIFICATION PHOTO */
                            $je_recherche_dans_le_dossier = $url;
                            // Si la photo existe
                            if(is_file($je_recherche_dans_le_dossier)) {
                                    if (@file_exists($je_recherche_dans_le_dossier)) {
                                     $url = htmlspecialchars (htmlentities  ($_GET['uri'])); //RECUPERE L'ID DE L'ARTICLE EN GET
                                    } else {
                                    $url = 'http://'.$_SERVER['HTTP_HOST'].'/ressources/medias/noimage/noimage.jpg';
                            }                              
     
                            }
                            else {
                                    $url = 'http://'.$_SERVER['HTTP_HOST'].'/ressources/medias/noimage/noimage.jpg';
                            }
     
    $url_formate = substr($url, (strrpos($url, '/') + 1)); // formattage de l'url
    $width = htmlspecialchars (htmlentities ($_GET['w'])); //RECUPERE L'ID DE L'ARTICLE EN GET
    $height = htmlspecialchars (htmlentities ($_GET['h'])); //RECUPERE L'ID DE L'ARTICLE EN GET
    $rep = "_cache/";
    $filename = $rep.$width."_".$url_formate;
     
     
     
    if (!file_exists($filename)) {
            if(!function_exists("create_square_image")){
                    function create_square_image($original_file, $destination_file=NULL, $square_size = 96){
     
                            if(isset($destination_file) and $destination_file!=NULL){
                                    if(!is_writable($destination_file)){
                                            //echo '<p style="color:#FF0000">Le dossier cache n\'est pas accessible en écriture</p>';
                                    }
                            }
     
                            // get width and height of original image
                            $imagedata = getimagesize($original_file);
                            $original_width = $imagedata[0];       
                            $original_height = $imagedata[1];
     
                            if($original_width > $original_height){
                                    $new_height = $square_size;
                                    $new_width = $new_height*($original_width/$original_height);
                            }
                            if($original_height > $original_width){
                                    $new_width = $square_size;
                                    $new_height = $new_width*($original_height/$original_width);
                            }
                            if($original_height == $original_width){
                                    $new_width = $square_size;
                                    $new_height = $square_size;
                            }
     
                            $new_width = round($new_width);
                            $new_height = round($new_height);
     
                            // load the image
                            if(substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")){
                                    $original_image = imagecreatefromjpeg($original_file);
                            }
                            if(substr_count(strtolower($original_file), ".gif")){
                                    $original_image = imagecreatefromgif($original_file);
                            }
                            if(substr_count(strtolower($original_file), ".png")){
                                    $original_image = imagecreatefrompng($original_file);
                            }
     
                            $smaller_image = imagecreatetruecolor($new_width, $new_height);
                            $square_image = imagecreatetruecolor($square_size, $square_size);
     
                            imagecopyresampled($smaller_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
     
                            if($new_width>$new_height){
                                    $difference = $new_width-$new_height;
                                    $half_difference =  round($difference/2);
                                    imagecopyresampled($square_image, $smaller_image, 0-$half_difference+1, 0, 0, 0, $square_size+$difference, $square_size, $new_width, $new_height);
                            }
                            if($new_height>$new_width){
                                    $difference = $new_height-$new_width;
                                    $half_difference =  round($difference/2);
                                    imagecopyresampled($square_image, $smaller_image, 0, 0-$half_difference+1, 0, 0, $square_size, $square_size+$difference, $new_width, $new_height);
                            }
                            if($new_height == $new_width){
                                    imagecopyresampled($square_image, $smaller_image, 0, 0, 0, 0, $square_size, $square_size, $new_width, $new_height);
                            }
     
     
                            // if no destination file was given then display a png         
                            if(!$destination_file){
                                    imagepng($square_image,NULL,9);
                            }
     
                            // save the smaller image FILE if destination file given
                            if(substr_count(strtolower($destination_file), ".jpg")){
                                    imagejpeg($square_image,$destination_file,100);
                            }
                            if(substr_count(strtolower($destination_file), ".gif")){
                                    imagegif($square_image,$destination_file);
                            }
                            if(substr_count(strtolower($destination_file), ".png")){
                                    imagepng($square_image,$destination_file,9);
                            }
     
     
     
     
                            imagedestroy($original_image);
                            imagedestroy($smaller_image);
                            imagedestroy($square_image);
     
                    }
            }
     
     
            create_square_image($url,$rep.$width."_".$url_formate,$width);
     
     
    }
     
        // Get actual size of source image
        $imgInfo = getimagesize($rep.$width."_".$url_formate) or die("Unable to open '$uri'");
        $srcType   = $imgInfo[2];
        switch($srcType) {
            case 1 : $srcType = "gif"; break;
            case 2 : $srcType = "jpeg"; break;
            case 3 : $srcType = "png"; break;
            default: $srcType = "???";
        }
     
     
     
    header("Content-Type:image/".$srcType."");
    readfile($rep.$width."_".$url_formate) or diewith("Unable to open cached thumb '$cacheFile'");
     
     
     
    ?>

  2. #2
    Membre expert
    Avatar de ericd69
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2011
    Messages
    1 919
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 1 919
    Points : 3 295
    Points
    3 295
    Billets dans le blog
    1
    Par défaut
    salut,

    il faut activer la transparence: imagesavealpha

Discussions similaires

  1. Image PNG avec transparence
    Par BlackStorm dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 11/09/2007, 15h00
  2. decouper une image (.png) avec c++
    Par MSM_007 dans le forum C++
    Réponses: 1
    Dernier message: 07/02/2007, 08h11
  3. [D5] Zoom d'image PNG avec transparence
    Par Thierry Laborde dans le forum Delphi
    Réponses: 9
    Dernier message: 12/06/2006, 15h41
  4. [GD] Obtention d'une image png avec gd2 inactive
    Par JavaAcro dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 05/03/2006, 16h44
  5. Traitements d'image sur PNG avec transparence
    Par Ingham dans le forum Langage
    Réponses: 16
    Dernier message: 02/08/2004, 16h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo