Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a transparent png file using PHP

Currently I would like to create a 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:

  1. Do I need to specific a png file before creating any new file? Or can I create without any existing png file?

    Warning: imagecreatefrompng(test.png): failed to open stream: No such file or directory in

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

  2. Although there are error message , it still generate a png file , however, what I found that is the file is a black color image , do I need to specific any parameter to make it transparent?

Thanks.

like image 435
user782104 Avatar asked Jun 24 '13 09:06

user782104


People also ask

How do I make a PNG file transparent?

Save As A Transparent PNG ImageClick “File” -> “Save As”. Select “PNG (*. PNG) as the file format. Note that though a transparent background looks checkered in Photoshop, it will actually be transparent in the final PNG file.

How do I make a transparent PNG in HTML?

Transparency is not done in HTML, but is a part of the image itself. The browser will see the image as a PNG and display it as a PNG automatically. To add transparency to the image, you will have to edit the file with a graphics editor like Photoshop. Save this answer.

Where can I get a transparent PNG?

PNGTree has an absolutely massive database of transparent images. At the time of writing, PNGTree claims to have millions of royalty-free PNG images and that they are uploading more every single day. The PNGTree website features the familiar search bar but also breaks the library down into categories.


2 Answers

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

To 2) To enable saving of the alpha channel imagesavealpha($img, true); is used. The following code creates a 200x200px sized transparent image by enabling alpha saving and filling 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');
like image 124
max-m Avatar answered Oct 02 '22 22:10

max-m


Take a look at:

  • imagecolorallocatealpha
  • imagefill

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");
    ?>
like image 21
neuraminidase7 Avatar answered Oct 02 '22 23:10

neuraminidase7