How to set a transparency or colorize background image with PHP? ← (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:

image

I would like transparency or colorized background, not black.

Now: black. Please see the created image:

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 );

Source