Jump to content

Help with image process--- transparent background on image save


treybraid

Recommended Posts

...right now when the image is saved it has a white background.  What would I need to add so the background of the image would be transparent like the original is?  I've already changed the filetype from JPG to PNG.

 

I've also tried adding RGB for transparent bg:  $backgroundcolor = imagecolorallocate($dimg, 255, 255, 255, 0.1); and it didn't work..

 

Here is the code:

 

    // check to make sure the file is of the supported type;
    $allowed_file_types = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif');
    if (!in_array($file['type'], $allowed_file_types)) {
        $error = "Unsupported image file type uploaded!";
        return $error;
    }

    // everything looks good for the file upload so resize and move it;
    $filename .= ".png";
    move_uploaded_file($file['tmp_name'], $filepath . $filename);
    $size = getimagesize($filepath . $filename);
    $w = $size[0];
    $h = $size[1];
    $stype = $size[2];
    switch($stype) {
        case '1':
            $simg = imagecreatefromgif($filepath . $filename);
            break;
        case '2':
            $simg = imagecreatefromjpeg($filepath . $filename);
            break;
        default:
            $simg = imagecreatefrompng($filepath . $filename);
            break;
    }

    if ($w > $width || $h > $height) {
        $dimg = imagecreatetruecolor($width, $height);
        imagealphablending($dimg, true);
        $backgroundcolor = imagecolorallocate($dimg, 255, 255, 255);
        imagefilledrectangle($dimg, 0, 0, $width, $height, $backgroundcolor);
        $wm = $w/$width;
        $hm = $h/$height;
        $h_height = $height/2;
        $w_height = $width/2;
        if ($w > $h) {
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;
            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$height,$w,$h);
        }
        else if (($w < $h) || ($w == $h)) {
            $adjusted_height = $h / $wm;
            $half_height = $adjusted_height / 2;
            $int_height = $half_height - $h_height;
            imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$width,$adjusted_height,$w,$h);
        }
        else {
            imagecopyresampled($dimg,$simg,0,0,0,0,$width,$height,$w,$h);
        }
    }
    else {
        $dimg = imagecreatetruecolor($w, $h);
        imagealphablending($dimg, true);
        $backgroundcolor = imagecolorallocate($dimg, 255, 255, 255);
        imagefilledrectangle($dimg, 0, 0, $w, $h, $backgroundcolor);
        imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $w, $h, $w, $h);
    }
    imagejpeg($dimg, $filepath . $filename, 80);
    imagedestroy($dimg);
    return false;
}
?>

 

Thanks

Trey

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.