How to set a transparency or colorize background image with PHP?

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.imgur.com/mYT7xoo.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:
I would like transparency or colorized background, not black.
Now: black. Please see the created image:
Answer
Solution:
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 );
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: ftp_put(): can't open that file: no such file or directory
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.