0

I speak a little English.

This is very good, it works:

<?php

    header( 'Content-Type: image/png' );

    $imgname = '../assets/images/lock-224x224.png'; // https://i.stack.imgur.com/9f6SL.png

    $im = imagecreatefrompng( $imgname );

    imagealphablending( $im, true );
    imagesavealpha( $im, true );

    imagefilter ( $im, IMG_FILTER_COLORIZE, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) );

    $im = imagerotate( $im, rand( 0, 360 ), 0 );

    $im = imagescale( $im, -1, 224 );

    imagepng( $im );
    imagedestroy( $im );

?>

Source (transparent) [lock-224x224.png] image:

image

I would like transparency or colorized background, not black.

Now: black. Please see the created image:

image

jozs2021
  • 91
  • 7
  • What do you mean by transparent or colorized? Is your png truly transparent? First check whether your png background is transparent and then you will be able to use your image anywhere and the background of the image will take on whatever color it is placed on. If your PNG isn't transparent, first step is to remove the background in an image editor. Placing a transparent image on a black background will cause the background of the image to be black and so on. – Grogu Jul 16 '21 at 18:36
  • Edit the file lock-224x224.png and give it a transparent background. Save it with the same name. This is not a programming question actually. – hakre Jul 16 '21 at 18:45
  • Dear Grogu & hakre, the source image is transparent background: https://i.imgur.com/mYT7xoo.png – jozs2021 Jul 16 '21 at 18:49
  • I would like change the created black background to transparent or if it not possible: the background color is, example only: #FF0000; – jozs2021 Jul 16 '21 at 18:51
  • Does this answer your question? [PHP/GD - transparent background](https://stackoverflow.com/questions/15246813/php-gd-transparent-background) – cetver Jul 16 '21 at 18:57
  • Dear cetver, thanks, but not solving my problem. – jozs2021 Jul 16 '21 at 19:03

1 Answers1

0

Solved:

$im = imagecreatefrompng( $imgname );
$im = imagerotate( $im, rand( 0, 360 ), imagecolorallocatealpha( $im, 0, 0, 0, 127 ) );
imagealphablending( $im, false );
$im = imagescale( $im, -1, 224 );
imagefilter ( $im, IMG_FILTER_COLORIZE, rand( 0, 255 ), rand( 0, 255 ), rand( 0, 255 ) );
imagesavealpha( $im, true );
imagepng( $im );
jozs2021
  • 91
  • 7