Page 1 sur 1

Créer une image avec un fond transparent ...

Posté : 15 mars 2020, 13:51
par thirt
Bonjour à tous,

en fait, j'essaie de redimensionner une image avec un fond transparent mais malheureusement, j'obtiens toujours un fond noir.

Pourtant, j'ai essayé pas mal de choses mais je n'arrive pas à obtenir le résultat escompté.

Voici mon code :

Code : Tout sélectionner

$im_temp = imagecreatetruecolor($th_width, $th_height); // preserve transparency if ($file_mime_type == "image/gif" || $file_mime_type == "image/png") { imagecolortransparent($im_temp, imagecolorallocatealpha($im_temp, 255, 255, 255, 127)); imagealphablending($im_temp, false); imagesavealpha($im_temp, true); } imageCopyResampled($im_temp, $im_old, 0, 0, 0, 0, $th_width, $th_height, $picture_width, $picture_height); $x0 = ( $th_width - $th_max_width ) / 2; $y0 = ( $th_height - $th_max_height ) / 2; $im_new = imagecreatetruecolor($th_max_width, $th_max_height); // preserve transparency if ($file_mime_type == "image/gif" || $file_mime_type == "image/png") { imagecolortransparent($im_new, imagecolorallocatealpha($im_new, 255, 255, 255, 127)); imagealphablending($im_new, false); imagesavealpha($im_new, true); } imagecopy($im_new, $im_temp, 0, 0, $x0, $y0, $th_max_width, $th_max_height);
Les images que j'ai voulu créer un thumbnail sont des images au format "jpg".

Avez-vous une idée du problème que je rencontre et qui ne me permet pas d'avoir un fond transparent ? Comme je le disais, j'ai toujours un fond noir.

Merci d'avance

Bon dimanche à tous.
Thierry

Re: Créer une image avec un fond transparent ...

Posté : 16 mars 2020, 13:35
par Saian
Salut, il faut que tu enregistres l'image au format png. Le format jpg ne permet pas la transparence.

Re: Créer une image avec un fond transparent ...

Posté : 16 mars 2020, 13:52
par thirt300575
bonjour Saian,

tout d'abord, merci pour votre réponse à mon message.

Je suppose qu'il est possible d'utiliser une fonction php pour convertir l'image du format jpeg en png ?

Merci d'avance

Bonne journée
Thierry

Re: Créer une image avec un fond transparent ...

Posté : 16 mars 2020, 15:03
par Saian
https://stackoverflow.com/questions/279 ... ncy-in-php
Avec je suppose un imagecreatefrompng pour l'image source et un imagepng à la fin pour sauver l'image redimensionnée.

Re: Créer une image avec un fond transparent ...

Posté : 19 mai 2020, 17:13
par louischabot83
Je pense qu'il faudra la prendre en amont en png ? La convertir ne la rendra pas transparente non ?