Reputation: 105
I'm testing out image upload functions with PHP and I couldn't manage to make image fully transparent, Only the start of image gets worked on, the rest of image remains black, here's before resizing image:
http://s13.postimg.org/3jswfzmx3/xnationals_png_pagespeed_ic_k_Mnf2qx2k.png
And when I use resizing functions I'm left with this:
http://s10.postimg.org/4el00d5o9/389056751644.png
Here's the code I got:
$img = imagecreatefrompng($target);
$tci = imagecreatetruecolor($width, $height);
etruecolor(200, 200);
imagecopyresampled($tci, $img, 0, 0, 0, 0, 200, 200, $w_orig, $h_orig);
imagealphablending($tci, true);
imagesavealpha($tci, true);
imagefill($tci,0,0,0x7fff0000);
imagepng($tci, $newcopy, 9);
imagedestroy($tci);
Upvotes: 2
Views: 114
Reputation: 3143
If you're working on a png resize and the image transparent it's a bit different. You also don't have imagecolorallocatealpha
Below is a basic workaround to this issue, keep it in a function so that it's reusable or try it before you do that:
function resizeImg($im, $dst_width, $dst_height) {
$width = imagesx($im);
$height = imagesy($im);
$newImg = imagecreatetruecolor($dst_width, $dst_height);
imagealphablending($newImg, false);
imagesavealpha($newImg, true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $width, $height, $transparent);
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);
return $newImg;
}
Upvotes: 2