Les Snippets

Connexion

Redimensionner une image

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 19/03/2006 10:43:34 et initié par neodante [Liste]
Date de mise à jour : 21/07/2006 17:24:32
Vue : 16499
Catégorie(s) : API, Multimédia
Langages dispo pour ce code :
- Java
- PHP 4, PHP 5
- ColdFusion MX
- mIRC
- HTML
- Javascript
- VB 2005, VB 2008
- Voir tous les langages pour ce code snippet



Langage : PHP 4 , PHP 5
Date ajout : 21/03/2006
Posté par malalam [Liste]
DateMAJ : 21/07/2006
<?php
function imageResize ($sFileNameFrom, $sFileNameTo, $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 ($sFileNameFrom) || !is_array ($KEEP_PROPORTIONS) || empty ($KEEP_PROPORTIONS)) {
  return false;
 } else {
  $aImg = @getimagesize ($sFileNameFrom);
  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 ('$sFileNameFrom', 'return @imagecreatefrom'.$aTypes[$aImg[2]].'($sFileNameFrom);');
    $saveImg = create_function ('$img, $sFileNameTo', 'return @image'.$aTypes[$aImg[2]].'($img, $sFileNameTo);');
    $im = $getImg ($sFileNameFrom);
    $image_p = imagecreatetruecolor($width, $height);
    imagecolortransparent ($image_p, 0);
    imagecopyresampled($image_p, $im, 0, 0, 0, 0, $width, $height, $aImg[0], $aImg[1]);
    if ($saveImg ($image_p, $sFileNameTo)) {
     return true;
    } else {
     return false;
    }
   }
  }
 }
}
imageResize ('transparent.gif', 'thumb_transparent.gif', array ('DO_NOT_KEEP_PROPORTIONS', 100, 100));
?>


Snippets en rapport avec : Redimension, Image, Picture, Resize, Bitmap



Codes sources en rapport avec : Redimension, Image, Picture, Resize, Bitmap

{JAVA / J2EE} FAIRE DEFILER UNE IMAGE
...

{C / C++ / C++.NET} IMPRIMEUR D'IMAGES BMP (API)
Suite à une question dans le forum, voici un code source montrant comment imprimer des images BMP. L...

{Visual Basic, VB6, VB.NET, VB 2005} REDIMENSION D'IMAGE
J'avais besoin d'un programme pour redimensionner à une grandeur fixe des images, j'ai trouvé seulem...

{PHP} CRÉE UNE IMAGE DE PILE DE MINIATURES EN UTILISANT LA BIBLIOTHÈQUE GD
Tout est dans le titre. Regarder la capture pour mieux comprendre. N'hésitez pas à me contacter ...

{Visual Basic, VB6, VB.NET, VB 2005} LOUPE PICTURE BOX
Une loupe agrandissant une image réduite aux dimensions de l’écran Ayant été confronté a afficher d...

{Delphi} MINIMISER LES COULEURS D' UNE IMAGE VENANT DU SCANNER OU AUTRE
Dans le même esprit que ma source : http://www.delphifr.com/codes/CHANGER-COULEUR-PIXEL-PIXELS-COUL...

{PHP} UPLOAD MULTIPLE IMAGE GIF/JPG/PNG AVEC ROTATION ET REDIMENSIONNEMENT GARDANT LES PROPORTIONS
Alors voilà, Maintenant que j'ai assez profité de toutes les astuces que fournis ce site, je me s...

{PHP} CLASSE DE REDIMENSION D'IMAGE.
Voila une premiere version d'une classe permettant de gérer une redimension d'image. La source es...

{C / C++ / C++.NET} BOITE DE DIALOGUE ANIMÉE AVEC IMAGE DE FOND
Ceci est mon premier code. Je pense que si on est resté longtemps consommateur, on peut en retour d...

{Visual Basic, VB6, VB.NET, VB 2005} OUVERTURE D'UN FICHIER BITMAP EN NATIF
Ce code permet d'ouvir un fichier Bmp. Oui, c'est sur il y a le composant Microsoft qui fait mieu...