1

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.

Andy
  • 17
  • 6
  • Duplicate question: http://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent – NikitaBaksalyar Feb 21 '15 at 02:30
  • @NikitaBaksalyar . thanks for your reply Nikita - I have tried implementing blending and saving alpha later in the order of operations in an earlier iteration but to no avail. EDIT: I have tried most solutions found by googling this but none of them seem to work for me – Andy Feb 21 '15 at 02:32
  • This seems like a very specific PHP image manipulation problem that I am not skilled enough to solve. for eg: I didnt know PHP had an extensive image manipulation capabilities http://www.magickwand.org/ – Andy Feb 21 '15 at 02:35
  • What are you using `$img_r` to? – Cyclonecode Feb 21 '15 at 02:38
  • @Andy, perhaps you might try to call `imagesavealpha` and `imagealphablending` before output for your destination image as well (`$final_image`)? – NikitaBaksalyar Feb 21 '15 at 02:39
  • @Cyclone $imgUrl = $_POST['imgUrl']; its the url posted by a jquery cropping plugin http://www.croppic.net/ – Andy Feb 21 '15 at 02:40
  • @Andy - What? I mean these rows: `$img_r = imagecreatefromjpeg($imgUrl);`. What are you using the `$img_r` variable to? I don't see that it's being used anywhere in your code or am I blind? – Cyclonecode Feb 21 '15 at 02:41
  • @Cyclone My apologize, Its copy pasta code from an example - I dont not require the original image source since im only uploading the cropped one. – Andy Feb 21 '15 at 02:46
  • I kinda got it working but it breaks the png and makes it look like it was made in ms paint. but atleast i got somewhat of a white background :D – Andy Feb 21 '15 at 03:45

0 Answers0