Page 1 sur 1

Problème avec imagecreatefrompng...

Posté : 12 févr. 2020, 20:36
par pjdebray
Hello à tous !

Avant de devenir complètement fou, je me suis dit que j'allais demander de l'aide à de vrais développeurs...
Voici mon problème : j’expérimente la création d'image avec PHP, ou plutôt la fusion dans mon cas.
Le code ci-dessous fonctionne très bien quand j'indique directement l'adresse de mon image dans "imagecreatefrompng" genre ça :

$a = imagecreatefrompng("image1.png");
$b = imagecreatefrompng("image2.png");
$c = imagecreatefrompng("image3.png ");


Le problème, c'est que j'aimerais que l'image vienne d'un formulaire en méthode GET. Pour le moment, pas de problème. Mais dès que j'ajoute les variables "$img1" dans "imagecreatefrompng" ça ne fonctionne plus...

Le code qui ne fonctionne pas :
$a = imagecreatefrompng( $img1 );
$b = imagecreatefrompng( $img2 );
$c = imagecreatefrompng( $img3 );


Une idée ? :)

Merci d'avance !

Mon code complet :

<?php 
            //define the width and height of our images
            define("WIDTH", 300);
            define("HEIGHT", 380);


            $dest_image = imagecreatetruecolor(WIDTH, HEIGHT);

            //make sure the transparency information is saved
            imagesavealpha($dest_image, true);

            //create a fully transparent background (127 means fully transparent)
            $trans_background = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);

            //fill the image with a transparent background
            imagefill($dest_image, 0, 0, $trans_background);
			$imga = $_GET['carousel'];
			$imgb = $_GET['thumb'];
			$imgc = $_GET['scale'];
			
			$img1 = "$imga".".png";
			$img2 = "$imgb".".png";
			$img3 = "$imgc".".png";

			//echo $img1;
			//echo $img2;
			//echo $img3;
			//echo '<img src="".$img1.".png">';

            //take create image resources out of the 3 pngs we want to merge into destination image
            $a = imagecreatefrompng( $img1 );
            $b = imagecreatefrompng( $img2 );
            $c = imagecreatefrompng( $img3 );

            //copy each png file on top of the destination (result) png
            imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT);
            imagecopy($dest_image, $b, 0, 114, 0, 0, WIDTH, HEIGHT);
            imagecopy($dest_image, $c, 0, 255, 0, 0, WIDTH, HEIGHT);

            //send the appropriate headers and output the image in the browser
header('Content-Type: image/png');
            imagepng($dest_image);

            //destroy all the image resources to free up memory
            imagedestroy($a);
            imagedestroy($b);
            imagedestroy($c);
            imagedestroy($dest_image);
    ?>

Re: Problème avec imagecreatefrompng...

Posté : 12 févr. 2020, 20:53
par pjdebray
Ok, j'ai réglé mon problème seul, visiblement, imagecreatefrompng ne fonctionne pas quand il y a un ECHO dans la même page... voici le code fonctionnel si ça intéresse des gens :
<?php 
			$imga = $_GET['carousel'];
			$imgb = $_GET['thumb'];
			$imgc = $_GET['scale'];
			
			
			$img1 = "$imga".".png";
			$img2 = "$imgb".".png";
			$img3 = "$imgc".".png";

			//echo '<img src="".$img1.".png">';
            //define the width and height of our images
			
            define("WIDTH", 300);
            define("HEIGHT", 380);
		

            $dest_image = imagecreatetruecolor(WIDTH, HEIGHT);

            //make sure the transparency information is saved
            imagesavealpha($dest_image, true);

            //create a fully transparent background (127 means fully transparent)
            $trans_background = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);

            //fill the image with a transparent background
            imagefill($dest_image, 0, 0, $trans_background);


            //take create image resources out of the 3 pngs we want to merge into destination image
            $a = imagecreatefrompng($img1);
            $b = imagecreatefrompng($img2);
            $c = imagecreatefrompng($img3);

            //copy each png file on top of the destination (result) png
            imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT);
            imagecopy($dest_image, $b, 0, 114, 0, 0, WIDTH, HEIGHT);
            imagecopy($dest_image, $c, 0, 255, 0, 0, WIDTH, HEIGHT);

            //send the appropriate headers and output the image in the browser
header('Content-Type: image/png');
            imagepng($dest_image);

            //destroy all the image resources to free up memory
            imagedestroy($a);
            imagedestroy($b);
            imagedestroy($c);
            imagedestroy($dest_image);
    ?>

Re: Problème avec imagecreatefrompng...

Posté : 12 févr. 2020, 21:07
par or 1
c'est header('Content-Type: image/png'); qui ne fonctionne pas si un echo a été exécuté avant.

Re: [RESOLU] Problème avec imagecreatefrompng...

Posté : 13 févr. 2020, 09:33
par pjdebray
Ok ! Merci pour la précision !