Create a transparent png file using PHP

Currently, I would like to create transparent png with the lowest quality.

The code:

<?php function createImg ($src, $dst, $width, $height, $quality) { $newImage = imagecreatetruecolor($width,$height); $source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename. imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height); imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image. return $dst; } createImg ('test.png','test.png','1920','1080','1'); ?> 

However, there are some problems:

  • Do I need to specify a png file before creating a new file? Or can I create without any existing png file?

    Warning: imagecreatefrompng (test.png): could not open the stream: there is no such file or directory in

    C: \ DSPadmin \ DEV \ ajax_optipng1.5 \ create.php on line 4

  • Despite the fact that there is an error message, it still generates a png file, however, I found that it is a black file, do I need to specify any parameter to make it transparent?

Thanks.

+7
source share
4 answers

B 1) imagecreatefrompng('test.png') tries to open the test.png file, which can then be edited using the GD functions.

To 2) To enable saving the alpha channel imagesavealpha($img, true); is used. The following code creates a transparent image 200x200 pixels in size, allowing you to save alpha and fill it with transparency.

 <?php $img = imagecreatetruecolor(200, 200); imagesavealpha($img, true); $color = imagecolorallocatealpha($img, 0, 0, 0, 127); imagefill($img, 0, 0, $color); imagepng($img, 'test.png'); 
+26
source

Take a look at:

An example function copies transparent PNG files:

  <?php function copyTransparent($src, $output) { $dimensions = getimagesize($src); $x = $dimensions[0]; $y = $dimensions[1]; $im = imagecreatetruecolor($x,$y); $src_ = imagecreatefrompng($src); // Prepare alpha channel for transparent background $alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127); imagecolortransparent($im, $alpha_channel); // Fill image imagefill($im, 0, 0, $alpha_channel); // Copy from other imagecopy($im,$src_, 0, 0, 0, 0, $x, $y); // Save transparency imagesavealpha($im,true); // Save PNG imagepng($im,$output,9); imagedestroy($im); } $png = 'test.png'; copyTransparent($png,"png.png"); ?> 
+5
source

1) You can create a new png file without any existing one. 2) You get an image in black because you use imagecreatetruecolor(); . It creates the highest quality image with a black background. Since you need to use the image with the lowest quality imagecreate();

 <?php $tt_image = imagecreate( 100, 50 ); /* width, height */ $background = imagecolorallocatealpha( $tt_image, 0, 0, 255, 127 ); /* In RGB colors- (Red, Green, Blue, Transparency ) */ header( "Content-type: image/png" ); imagepng( $tt_image ); imagecolordeallocate( $background ); imagedestroy( $tt_image ); ?> 

You can read more in this article: How to create an image using PHP

+2
source

You can use the console console utility, which is part of ImageMagick, available on most Linux repositories and in homebrew for OSX:

  exec('convert image.png -transparent black image_transparent.png') 

In this example, black is a transparent color.

+1
source

All Articles