Vignettes d'images

Résolu
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008 - 28 mai 2006 à 04:58
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 - 28 mai 2006 à 18:43
Salut, sa fait 4 heures de temps que je gosses apprais ce code:

function imageResize ($fichier_dossier, $fileName, $KEEP_PROPORTIONS) {
    $aProportions = array ('DO_NOT_KEEP_PROPORTIONS', 'KEEP_PROPORTIONS_ON_WIDTH', 'KEEP_PROPORTIONS_ON_HEIGHT', 'KEEP_PROPORTIONS_ON_BIGGEST', 'KEEP_PROPORTIONS_ON_SMALLEST');
    if (!file_exists ($fileName) || !is_array ($KEEP_PROPORTIONS) || empty ($KEEP_PROPORTIONS)) {
        return false;
    } else {
        $aImg = @getimagesize ($fileName);
        if (false === $aImg) {
            return false;
        } else {            $aTypes array (1> 'gif', 2 => 'jpeg', 3 => 'png');
            if (!in_array ($aImg[2], array_keys ($aTypes))) {
                return false;
            } else {
                if (!in_array ($KEEP_PROPORTIONS[0], $aProportions)) {
                    return false;
                }
                $iCmpt = count ($KEEP_PROPORTIONS);
                if (!empty ($KEEP_PROPORTIONS) && is_array ($KEEP_PROPORTIONS) && ($iCmpt >= 2) && is_int ($KEEP_PROPORTIONS[1])) {
                    switch ($KEEP_PROPORTIONS[0]) {
                        case 'KEEP_PROPORTIONS_ON_WIDTH' :
                            $width = $KEEP_PROPORTIONS[1];
                            $height = round ($aImg[1] / (round ($aImg[0]/$KEEP_PROPORTIONS[1])));
                            break;
                        case 'KEEP_PROPORTIONS_ON_HEIGHT' :
                            $height = $KEEP_PROPORTIONS[1];
                            $width = round ($aImg[0]/ (round ($aImg[1]/$KEEP_PROPORTIONS[1])));
                            break;
                        case 'KEEP_PROPORTIONS_ON_BIGGEST' :
                            if ($aImg[0] >= $aImg[1]) {
                                $width = $KEEP_PROPORTIONS[1];
                                $height = round ($aImg[1] / (round ($aImg[0]/$KEEP_PROPORTIONS[1])));
                            } else {
                                $height = $KEEP_PROPORTIONS[1];
                                $width = round ($aImg[0] / (round ($aImg[1]/$KEEP_PROPORTIONS[1])));
                            }
                            break;
                        case 'KEEP_PROPORTIONS_ON_SMALLEST' :
                            if ($aImg[0] <= $aImg[1]) {
                                $width = $KEEP_PROPORTIONS[1];
                                $height = round ($aImg[1] / (round ($aImg[0]/$KEEP_PROPORTIONS[1])));
                            } else {
                                $height = $KEEP_PROPORTIONS[1];
                                $width = round ($aImg[0] / (round ($aImg[1]/$KEEP_PROPORTIONS[1])));
                            }
                            break;
                        case 'DO_NOT_KEEP_PROPORTIONS':
                            if ($iCmpt !== 3 || !is_int ($KEEP_PROPORTIONS[2])) {
                                return false;
                            }
                            $width = $KEEP_PROPORTIONS[1];
                            $height = $KEEP_PROPORTIONS[2];
                            break;
                    }
                }
                $getImg = create_function ('$fileName', 'return @imagecreatefrom'.$aTypes[$aImg[2]].'($fileName);');
                $im = $getImg ($fileName);
                $image_p = imagecreatetruecolor($width, $height);
                imagecopyresampled($image_p, $im, 0, 0, 0, 0, $width, $height, $aImg[0], $aImg[1]);
                $saveImg = create_function ('$img, $fileName', 'return @image'.$aTypes[$aImg[2]].'($img, $fichier_dossier.$fileName);');
                if ($saveImg ($image_p, $fileName)) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
}
imageResize ('thumbnails','eoliennbnes.jpg', array ('DO_NOT_KEEP_PROPORTIONS', 100, 100));

Sa marche pour créé des vignettes mais je peu pas les enregistre ou je veux, j'ai essayer en rajoutent "$fichier_dossier." et sa marche pas.

merci en avance si vous trouvez mon erreure.

13 réponses

malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
28 mai 2006 à 18:06
Tien, mon code...

$saveImg = create_function ('$img, $fileName', 'return @image'.$aTypes[$aImg[2]].'($img, $fichier_dossier.$fileName);');

tu mets quoi dans fichier_dossier ? et n'oublie pas les chemins relatifs/absolus, comme te le suggère J_G.

Et puis...là, faut voir...
$saveImg = create_function ('$img, $fileName', 'return @image'.$aTypes[$aImg[2]].'($img, $fichier_dossier.$fileName);');
if ($saveImg ($image_p, $fileName))

Mais je crée une fonction...si tu lis bien. On passe 2 arguments à cette fonction. $img et $fileName. Ce ne sont pas les mêmes que ceux de la fonction de départ!! Je les passe, ce sont de nouvelles variables. Pour utiliser une variable "globale" connue de ma fonction première, je la concatène :
'return @image'.$aTypes[$aImg[2]].'...
Mais je ne concatène ni $img, ni $fileName.
La fonction créée va utiliser celles quiu lui sont passées en argument. Mais tu ne passes pas $fichier_dossier...! Donc cette fonction dynamique ne connais pas cette fonction.
Bref :
$saveImg = create_function ('$img, $fileName, $fichier_dossier', 'return @image'.$aTypes[$aImg[2]].'($img, $fichier_dossier.$fileName);');
if ($saveImg ($image_p, $fileName, $fichier_dossier))

ca devrait le faire.
3
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 18:33
Sa marche c'est de ma faute j'ai aublier le \.
Voici le code:
<?php
function imageResize ($fichier_dossier, $fileName, $KEEP_PROPORTIONS) {
    $aProportions = array ('DO_NOT_KEEP_PROPORTIONS', 'KEEP_PROPORTIONS_ON_WIDTH', 'KEEP_PROPORTIONS_ON_HEIGHT', 'KEEP_PROPORTIONS_ON_BIGGEST', 'KEEP_PROPORTIONS_ON_SMALLEST');
    if (!file_exists ($fileName) || !is_array ($KEEP_PROPORTIONS) || empty ($KEEP_PROPORTIONS)) {
        return false;
    } else {
        $aImg = @getimagesize ($fileName);
        if (false === $aImg) {
            return false;
        } else {            $aTypes array (1> 'gif', 2 => 'jpeg', 3 => 'png');
            if (!in_array ($aImg[2], array_keys ($aTypes))) {
                return false;
            } else {
                if (!in_array ($KEEP_PROPORTIONS[0], $aProportions)) {
                    return false;
                }
                $iCmpt = count ($KEEP_PROPORTIONS);
                if (!empty ($KEEP_PROPORTIONS) && is_array ($KEEP_PROPORTIONS) && ($iCmpt >= 2) && is_int ($KEEP_PROPORTIONS[1])) {
                    switch ($KEEP_PROPORTIONS[0]) {
                        case 'KEEP_PROPORTIONS_ON_WIDTH' :
                            $width = $KEEP_PROPORTIONS[1];
                            $height = round ($aImg[1] / (round ($aImg[0]/$KEEP_PROPORTIONS[1])));
                            break;
                        case 'KEEP_PROPORTIONS_ON_HEIGHT' :
                            $height = $KEEP_PROPORTIONS[1];
                            $width = round ($aImg[0]/ (round ($aImg[1]/$KEEP_PROPORTIONS[1])));
                            break;
                        case 'KEEP_PROPORTIONS_ON_BIGGEST' :
                            if ($aImg[0] >= $aImg[1]) {
                                $width = $KEEP_PROPORTIONS[1];
                                $height = round ($aImg[1] / (round ($aImg[0]/$KEEP_PROPORTIONS[1])));
                            } else {
                                $height = $KEEP_PROPORTIONS[1];
                                $width = round ($aImg[0] / (round ($aImg[1]/$KEEP_PROPORTIONS[1])));
                            }
                            break;
                        case 'KEEP_PROPORTIONS_ON_SMALLEST' :
                            if ($aImg[0] <= $aImg[1]) {
                                $width = $KEEP_PROPORTIONS[1];
                                $height = round ($aImg[1] / (round ($aImg[0]/$KEEP_PROPORTIONS[1])));
                            } else {
                                $height = $KEEP_PROPORTIONS[1];
                                $width = round ($aImg[0] / (round ($aImg[1]/$KEEP_PROPORTIONS[1])));
                            }
                            break;
                        case 'DO_NOT_KEEP_PROPORTIONS':
                            if ($iCmpt !== 3 || !is_int ($KEEP_PROPORTIONS[2])) {
                                return false;
                            }
                            $width = $KEEP_PROPORTIONS[1];
                            $height = $KEEP_PROPORTIONS[2];
                            break;
                    }
                }
                $getImg = create_function ('$fileName', 'return @imagecreatefrom'.$aTypes[$aImg[2]].'($fileName);');
                $im = $getImg ($fileName);
                $image_p = imagecreatetruecolor($width, $height);
                imagecopyresampled($image_p, $im, 0, 0, 0, 0, $width, $height, $aImg[0], $aImg[1]);
                $saveImg = create_function ('$img, $fileName, $fichier_dossier', 'return @image'.$aTypes[$aImg[2]].'($img, $fichier_dossier.$fileName);');
                if ($saveImg ($image_p, $fileName, $fichier_dossier)) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
}
imageResize ('thumbnails\ ','Eolienne.jpg', array ('DO_NOT_KEEP_PROPORTIONS', 100, 100));
?>

A être distribuer.

Merci beaucoup. A+
3
J_G Messages postés 1406 Date d'inscription mercredi 17 août 2005 Statut Membre Dernière intervention 28 août 2007 10
28 mai 2006 à 11:43
Salut,

Ben genre, si tu fais imagepng($image_resource, 'fichier_sauve.png'); ça devrait le faire...

Non ?
0
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 16:20
Oui, je peu changer le nom de l'image qui se fait enregistrer, mais moi je veu qu'il s'enregistre dans un autre dossier.

Merci quand même.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
J_G Messages postés 1406 Date d'inscription mercredi 17 août 2005 Statut Membre Dernière intervention 28 août 2007 10
28 mai 2006 à 17:13
?

imagepng( $image_resource, '/un/autre/dossier/../meme/relatif/image.png'):
0
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 17:19
$fichier_dossier.$fileName);');



Je les fait ici mais sa marche juste pas. Essaye le code de ton coter. C'est peut-être moi.


Merci pour ton aide.
0
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 18:30
Sa vas pas l'enregistrer dans le dossier thumbnails sa l'enregistre dans le même dossier que l'image originale et sa change son nom.

de Eolienne.jpg

a thumbnailsEolienne.jpg

mais moi je veux qu'il garde son nom originale mais qu'il senregistre dans le dossier thumbnails.

Merci, de votre aide.
0
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
28 mai 2006 à 18:33
Je viens de t'expliquer.
A toi de passer les bons paramètres après cette modification.
0
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 18:35
Questions rapide. J'aimerais aprendre commen utiliser le GD library de php quelle documentation me recomende tu ou tutorial.

Merci, encore A+.
0
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
28 mai 2006 à 18:38
Perso, je n'ai utilisé aucun tuto pour GD, j'ai juste bossé la doc officielle sur php.net.
Elle est très complète, la doc php, et très très bien expliquée et commentée :-)
0
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 18:39
Ok, merci.
0
jnbdzjnbdz Messages postés 79 Date d'inscription vendredi 26 mai 2006 Statut Membre Dernière intervention 10 juillet 2008
28 mai 2006 à 18:41
J'ai trouver sa hiere si tu veu le poster sur CS (proposition):

http://mtodorovic.developpez.com/php/gd/
0
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
28 mai 2006 à 18:43
Nan je ne peux pas lol...je ne peux pas piquer un tuto de developpez.com pour le mettre sur CS. Vont certainement pas apprécier... ;-)

Mais merci ;-)

J'ai tout un tas de tuto à mettre sur CS...faut que je trouve le temps de les peaufiner et de les compléter.
0
Rejoignez-nous