23

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.

user782104
  • 13,233
  • 55
  • 172
  • 312

3 Answers3

53

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');
max-m
  • 847
  • 7
  • 9
  • Thanks your help! Would you mind teaching me how to minimize the size of the png file? Is setting '9' quality level in imagepng function is the only thing I can do ? thanks – user782104 Jun 24 '13 at 09:20
  • 1
    `imagepng`s default "quality" setting (which should be named compressions, since `png`s compressions is lossless) is 9 (afaik, I tested with no set quality (234 `Bytes`), quality 0 (a few hundred `KB`) and a setting of 9 (234 Bytes)). So I guess this is the best GD could do. – max-m Jun 24 '13 at 09:36
  • this makes my black lines disappear – Mladen Janjetovic Oct 28 '16 at 13:54
8

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");
    ?>
neuraminidase7
  • 486
  • 2
  • 7
  • 19
2

1) You can create a new png file without any existing one. 2) You get a black color image because you use imagecreatetruecolor();. It creates a highest quality image with a black background. As you need a lowest quality image use 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