I have this image upload script that does not want to co-operate.
It insists on turning transparent pngs to black .
$img = getimagesize($imgUrl);
switch(strtolower($img['mime']))
{
case 'image/png':
//$img_r = imagecreatefrompng($imgUrl);
$source_image = imagecreatefrompng($imgUrl);
imagealphablending($source_image, false);
imagesavealpha($source_image, true);
$type = '.png';
break;
case 'image/jpeg':
//$img_r = imagecreatefromjpeg($imgUrl);
$source_image = imagecreatefromjpeg($imgUrl);
error_log("jpg");
$type = '.jpeg';
break;
case 'image/gif':
//$img_r = imagecreatefromgif($imgUrl);
$source_image = imagecreatefromgif($imgUrl);
$type = '.gif';
break;
default: die('image type not supported');
}
// resize the original image to size of editor
$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
$final_image = imagecreatetruecolor($cropW, $cropH);
imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
imagecopyresampled($final_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
// finally output png image
if($type == '.png' ) {
imagepng($final_image, $output_filename.$type);
} else {
imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
}
$imgur = new Imgur_ctrl($output_filename.$type);
$img_url = $imgur->upload();
I tried alphablending and saving but it still turns to black.. not sure how to fix this.
Thank for your help
EDIT:
I have tried the alpha blending right before the output as found in many other similar problems you find googling.
$final_image = imagecreatetruecolor($cropW, $cropH);
imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
imagecopyresampled($final_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
imagealphablending($final_image, false);
imagesavealpha($final_image, true);
// finally output png image
if($type == '.png' ) {
imagepng($final_image, $output_filename.$type);
} else {
imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
}
it still turns to black.
I do not want to the remain transparent (it would be great if it did) but it will work even with white.