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

Langage PHP Discussion :

Transparence image PNG


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    320
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 320
    Points : 74
    Points
    74
    Par défaut Transparence image PNG
    bonjour le script suivant donne une transparence a une image PNG de 75%. JE , (ontien aucun resulta ( ce srcipt n 'est pas de moi mais je comprend eidement son fonctionnement... enfin presque puissque cela ne fonctionne pas pouvez vous le tester et me dire si cela fonctionne de votre côté. Merci d'avance

    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
     
     
    <?php
    function filter_opacity( &$img, $opacity ) //params: image resource id, opacity in percentage (eg. 80)
            {
                if( !isset( $opacity ) )
                    { return false; }
                $opacity /= 100;
     
                //get image width and height
                $w = imagesx( $img );
                $h = imagesy( $img );
     
                //turn alpha blending off
                imagealphablending( $img, false );
     
                //find the most opaque pixel in the image (the one with the smallest alpha value)
                $minalpha = 127;
                for( $x = 0; $x < $w; $x++ )
                    for( $y = 0; $y < $h; $y++ )
                        {
                            $alpha = ( imagecolorat( $img, $x, $y ) >> 24 ) & 0xFF;
                            if( $alpha < $minalpha )
                                { $minalpha = $alpha; }
                        }
     
                //loop through image pixels and modify alpha for each
                for( $x = 0; $x < $w; $x++ )
                    {
                        for( $y = 0; $y < $h; $y++ )
                            {
                                //get current alpha value (represents the TANSPARENCY!)
                                $colorxy = imagecolorat( $img, $x, $y );
                                $alpha = ( $colorxy >> 24 ) & 0xFF;
                                //calculate new alpha
                                if( $minalpha !== 127 )
                                    { $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha ); }
                                else
                                    { $alpha += 127 * $opacity; }
                                //get the color index with new alpha
                                $alphacolorxy = imagecolorallocatealpha( $img, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
                                //set pixel with the new color + opacity
                                if( !imagesetpixel( $img, $x, $y, $alphacolorxy ) )
                                    { return false; }
                            }
                    }
                return true;
            }
    ?>
     
    Example for use:
     
    <?php
      $image = imagecreatefrompng( "centre.png" );
      filter_opacity( $image, 75 );
      header( "content-type: image/png" );
      imagepng( $image );
      imagedestroy( $image );
    ?>

  2. #2
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Points : 16 372
    Points
    16 372
    Par défaut
    Essaye en ajoutant d'abord un appel à imagesavealpha :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    imagesavealpha($img, true);
    imagealphablending($img, false);

  3. #3
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    320
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 320
    Points : 74
    Points
    74
    Par défaut
    bonjour Celira,

    Merci pour ton aide. Je dois certainement attendre une réponse, mais cela ne donne rien . ou alors je ne le place pas au bon endroit

  4. #4
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par loribac Voir le message
    ...ou alors je ne le place pas au bon endroit...
    On ne risque pas de deviner, puisque tu ne montres pas le code...


    Ce code fonctionne tel quel (+1 Celira) :

    1- Fichier img-opacity.php :
    Code php : 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
    <?php
    header( "content-type: image/png" );
    $image = imagecreatefrompng( "centre.png" );
     
    filter_opacity( $image, 75 );
    imagepng( $image );
    imagedestroy( $image );
     
    // -------------------
    function filter_opacity( &$img, $opacity ) //params: image resource id, opacity in percentage (eg. 80)
    {
    	if( !isset( $opacity ) )
    		{ return false; }
    	$opacity /= 100;
     
    	//get image width and height
    	$w = imagesx( $img );
    	$h = imagesy( $img );
     
    	imagesavealpha($img, true);
    	//turn alpha blending off
    	imagealphablending( $img, false );
     
    	//find the most opaque pixel in the image (the one with the smallest alpha value)
    	$minalpha = 127;
    	for( $x = 0; $x < $w; $x++ )
    	{
    		for( $y = 0; $y < $h; $y++ )
    		{
    			$alpha = ( imagecolorat( $img, $x, $y ) >> 24 ) & 0xFF;
    			if( $alpha < $minalpha )
    				{ $minalpha = $alpha; }
    		}
    	}
    	//loop through image pixels and modify alpha for each
    	for( $x = 0; $x < $w; $x++ )
    	{
    		for( $y = 0; $y < $h; $y++ )
    		{
    			//get current alpha value (represents the TANSPARENCY!)
    			$colorxy = imagecolorat( $img, $x, $y );
    			$alpha = ( $colorxy >> 24 ) & 0xFF;
    			//calculate new alpha
    			if( $minalpha !== 127 )
    				{ $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha ); }
    			else
    				{ $alpha += 127 * $opacity; }
    			//get the color index with new alpha
    			$alphacolorxy = imagecolorallocatealpha( $img, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
    			//set pixel with the new color + opacity
    			if( !imagesetpixel( $img, $x, $y, $alphacolorxy ) )
    				{ return false; }
    		}
    	}
    	return true;
    }
    // -------------------

    Évidemment, tu sais qu'avant un header(), on ne met pas de "sortie HTML".
    Donc, pas de :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    ?>
     
    Example for use:
     
    <?php
    2- Pour afficher cette image dans une page HTML :

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    Mon image : <img src="img-opacity.php" />
    Dernière modification par Invité ; 24/04/2019 à 12h15.

  5. #5
    Invité
    Invité(e)
    Par défaut
    Pour aller plus loin...

    On peut définir des paramètres : url de l'image, et opacité.

    1- Fichier img-opacity.php :
    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
    <?php
    header( "content-type: image/png" );
     
    $img_url = $_GET['img'];
    $opacity = ( is_numeric($_GET['opacity']) && $_GET['opacity']>=0 && $_GET['opacity']<=100 )? intval($_GET['opacity']) : 100;
     
    $image = filter_opacity( $img_url, $opacity );
    imagepng( $image );
    imagedestroy( $image );
    // -------------------
    function filter_opacity( $img_url, $opacity ) //params: image URL, opacity in percentage (eg. 80)
    {
    	if( !isset( $opacity ) )
    		{ return false; }
    	$opacity /= 100;
     
    	$extension = strtolower(pathinfo($img_url, PATHINFO_EXTENSION));
    	if( $extension == 'png' )
    	{
    		$img = imagecreatefrompng( $img_url );
    	} 
    	elseif( in_array($extension, ['jpg','jpeg']) )
    	{
    		imagepng(imagecreatefromstring(file_get_contents($img_url)), "output.png");
    		$img = imagecreatefrompng( "output.png" );
    	} else {
    		return false;
    	}
     
    	//get image width and height
    	$w = imagesx( $img );
    	$h = imagesy( $img );
     
    	imagesavealpha($img, true);
    	//turn alpha blending off
    	imagealphablending( $img, false );
     
    	//find the most opaque pixel in the image (the one with the smallest alpha value)
    	$minalpha = 127;
    	for( $x = 0; $x < $w; $x++ )
    	{
    		for( $y = 0; $y < $h; $y++ )
    		{
    			$alpha = ( imagecolorat( $img, $x, $y ) >> 24 ) & 0xFF;
    			if( $alpha < $minalpha )
    				{ $minalpha = $alpha; }
    		}
    	}
    	//loop through image pixels and modify alpha for each
    	for( $x = 0; $x < $w; $x++ )
    	{
    		for( $y = 0; $y < $h; $y++ )
    		{
    			//get current alpha value (represents the TANSPARENCY!)
    			$colorxy = imagecolorat( $img, $x, $y );
    			$alpha = ( $colorxy >> 24 ) & 0xFF;
    			//calculate new alpha
    			if( $minalpha !== 127 )
    				{ $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha ); }
    			else
    				{ $alpha += 127 * $opacity; }
    			//get the color index with new alpha
    			$alphacolorxy = imagecolorallocatealpha( $img, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
    			//set pixel with the new color + opacity
    			if( !imagesetpixel( $img, $x, $y, $alphacolorxy ) )
    				{ return false; }
    		}
    	}
    	return $img;
    }
    // -------------------
    Remarque : j'ai modifié la fonction, pour gérer aussi les images jpg et jpeg.
    (par contre, ça ne fonctionne pas correctement avec les images gif, que j'ai donc exclues)

    2- Affichage :

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <p>Mon image 100% : <img src="img-opacity.php?img=centre.png&opacity=100" /></p>
    <p>Mon image 75% : <img src="img-opacity.php?img=centre.png&opacity=75" /></p>
    <p>Mon image 50% : <img src="img-opacity.php?img=centre.png&opacity=50" /></p>
    <p>Mon image 25% : <img src="img-opacity.php?img=centre.png&opacity=25" /></p>
    <p>Mon image 0% : <img src="img-opacity.php?img=centre.png&opacity=0" /></p>
    Dernière modification par Invité ; 24/04/2019 à 12h18.

  6. #6
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    320
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 320
    Points : 74
    Points
    74
    Par défaut
    Merci de vos efforts. J 'obtiend une image noir avec un carré blanc au milieu.
    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
     
     
    <?php
    header( "content-type: image/png" );
    $image = imagecreatefrompng( "centre.png" );
     
    filter_opacity( $image, 75 );
    imagepng( $image );
    imagedestroy( $image );
     
    // -------------------
    function filter_opacity( &$img, $opacity ) //params: image resource id, opacity in percentage (eg. 80)
    {
    	if( !isset( $opacity ) )
    		{ return false; }
    	$opacity /= 100;
     
    	//get image width and height
    	$w = imagesx( $img );
    	$h = imagesy( $img );
     
    	imagesavealpha($img, true);
    	//turn alpha blending off
    	imagealphablending( $img, false );
     
    	//find the most opaque pixel in the image (the one with the smallest alpha value)
    	$minalpha = 127;
    	for( $x = 0; $x < $w; $x++ )
    	{
    		for( $y = 0; $y < $h; $y++ )
    		{
    			$alpha = ( imagecolorat( $img, $x, $y ) >> 24 ) & 0xFF;
    			if( $alpha < $minalpha )
    				{ $minalpha = $alpha; }
    		}
    	}
    	//loop through image pixels and modify alpha for each
    	for( $x = 0; $x < $w; $x++ )
    	{
    		for( $y = 0; $y < $h; $y++ )
    		{
    			//get current alpha value (represents the TANSPARENCY!)
    			$colorxy = imagecolorat( $img, $x, $y );
    			$alpha = ( $colorxy >> 24 ) & 0xFF;
    			//calculate new alpha
    			if( $minalpha !== 127 )
    				{ $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha ); }
    			else
    				{ $alpha += 127 * $opacity; }
    			//get the color index with new alpha
    			$alphacolorxy = imagecolorallocatealpha( $img, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
    			//set pixel with the new color + opacity
    			if( !imagesetpixel( $img, $x, $y, $alphacolorxy ) )
    				{ return false; }
    		}
    	}
    	return true;
    }
     
     
    ?>
     
     
    Mon image : <img src="img-opacity.php" />

  7. #7
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juin 2012
    Messages
    320
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 320
    Points : 74
    Points
    74
    Par défaut
    Bonsoir, voici le lien direct vers l 'image https://www.eustormchasers.com/COPY/centre.png dont je veux régler l 'opacité. vous comprendrez peut-être mieux les problèmes que je rencontre

    Bien à vous

Discussions similaires

  1. [Débutant] Transparence image *.png
    Par Luis Vieira da Silva dans le forum Images
    Réponses: 2
    Dernier message: 11/01/2011, 08h40
  2. transparence image png siteweb
    Par fasyr dans le forum Webdesign & Ergonomie
    Réponses: 4
    Dernier message: 21/04/2009, 12h45
  3. Transparence des images PNG sous Internet Explorer
    Par Torpedox dans le forum Webdesign & Ergonomie
    Réponses: 12
    Dernier message: 14/10/2008, 23h41
  4. [ImageMagick] Transparence alpha image png
    Par ShadoX dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 12/06/2006, 18h32
  5. [D5] Zoom d'image PNG avec transparence
    Par Thierry Laborde dans le forum Delphi
    Réponses: 9
    Dernier message: 12/06/2006, 15h41

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