imagecreatefrompng() Makes a black background instead of transparent?
After imagecreatetruecolor():
<?php
// ... Before imagecreatetruecolor()
$dimg = imagecreatetruecolor($width_new, $height_new); // png ?: gif
// start changes
switch ($stype) {
case 'gif':
case 'png':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($dimg , 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($dimg, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($dimg, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($dimg, true);
break;
default:
break;
}
// end changes
$wm = $w/$nw;
$hm = $h/$nh;
// ...
imagecreatefrompng() transparent part of png is black
Well that was easy XD Converting comment to answer:
Your mistake was in defining the background colour. You should use this:
$background = imagecolorallocatealpha($src,0,0,0,127);
However, it is probably a good idea to be safe, and avoid using a "transparent" colour that already exists on your image. The "traditional" transparent colour from old sprite-based games is magenta, since it is very unlikely that you'll have straight magenta on your image!
$background = imagecolorallocatealpha($src,255,0,255,127);
PHP GD - transparent PNG black background
It should be something along the lines of:
switch ($source_type)
{
case IMAGETYPE_PNG:
$background = imagecolorallocate($source, 0, 0, 0);
// remove the black
imagecolortransparent($source, $background);
// turn off alpha blending
imagealphablending($source, false);
imagesavealpha($source, true);
break;
}
There is a similar question here
Black background instead of transparent on PNG images when processing them
This $background = imagecolorallocate($newimg, 0, 0, 0);
is for black background
use $background = imagecolorallocatealpha($newimg, 255, 255, 255);
instead
PNG with transparent background = thumbnail with black backgroud
Using:
$tci = imagecreate($w, $h);
Instead:
$tci = imagecreatetruecolor($w, $h);
My issue is now solved :)
PHP GD - Transparent areas goes black
You should call these two functions before saving the png image, imagealphablending() and imagesavealpha():
imagealphablending( $image, false );
imagesavealpha( $image, true );
Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
did it for me. Thanks ceejayoz.
note, the target image needs the alpha settings, not the source image.
Edit:
full replacement code. See also answers below and their comments. This is not guaranteed to be be perfect in any way, but did achieve my needs at the time.
$uploadTempFile = $myField[ 'tmp_name' ]
list( $uploadWidth, $uploadHeight, $uploadType )
= getimagesize( $uploadTempFile );
$srcImage = imagecreatefrompng( $uploadTempFile );
$targetImage = imagecreatetruecolor( 128, 128 );
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
imagecopyresampled( $targetImage, $srcImage,
0, 0,
0, 0,
128, 128,
$uploadWidth, $uploadHeight );
imagepng( $targetImage, 'out.png', 9 );
Related Topics
PHP Multiple Delimiters in Explode
How to Get Video Duration, Dimension and Size in PHP
What Is the Canonical Way to Determine Commandline Vs. Http Execution of a PHP Script
Send Json Data from JavaScript to PHP
Error in PHP5 ..Unable to Load Dynamic Library
PHP Curly Brace Syntax For Member Variable
Difference Between "Not Equal" Operators ≪≫ and != in PHP
PHP Json_Encode Not Working on Arrays Partially
Imagecreatefrompng() Makes a Black Background Instead of Transparent
PHP Equivalent of .Net/Java'S Tostring()
Find the Last Element of an Array While Using a Foreach Loop in PHP
PHPmailer Godaddy Server Smtp Connection Refused
Remove Text Between Parentheses PHP
How to Pass Data from JavaScript to PHP and Vice Versa
How to Rely on PHP PHP.Ini Precision Workaround For Floating Point Issue