Allow uploaded PNGs to be transparent

J W Posted in Component Development 8 years ago

Hey, i'd like to give the group user the opportunity to upload pngs and therefor i want the transparency to be maintained. So right now all alpha channels are underlayed with a white background. Can i disable this ? or at least make the background black?

Replies
German J W Replied 8 years ago

Actually you could create a flag in that function to allow or forbid the transparency

German J W Replied 8 years ago

Theres no hook or callback placed for the lib image, expect the image size...
Could you update the ossn.lib.image?

( inspired by -> Simon Jarvis https://gist.github.com/laminr/8390141 )

German J W Replied 8 years ago

// Get the size information from the image
$info = getimagesize($input_name);
if ($info == false) {
return false;
}
$width = $info[0];
$height = $info[1];

$accepted_formats = array(
    "image/jpeg" => "jpeg",
    "image/pjpeg" => "jpeg",
    "image/png" => "png",
    "image/x-png" => "png",
    "image/gif" => "gif"
);

// make sure the function is available
$load_function = "imagecreatefrom" . $accepted_formats[$info["mime"]];
if (!is_callable($load_function)) {
    return false;
}

// get the parameters for resizing the image
$options = array(
    "maxwidth" => $maxwidth,
    "maxheight" => $maxheight,
    "square" => $square,
    "upscale" => $upscale,
    "x1" => $x1,
    "y1" => $y1,
    "x2" => $x2,
    "y2" => $y2,
);
$params = ossn_image_resize_parameters($width, $height, $options);
if ($params == false) {
    return false;
}

// load original image
$original_image = call_user_func($load_function, $input_name);
if (!$original_image) {
    return false;
}

// allocate the new image
$new_image = imagecreatetruecolor($params["newwidth"], $params["newheight"]);
if (!$new_image) {
    return false;
}

  if ( ( $info[2] == IMAGETYPE_GIF ) || ( $info[2] == IMAGETYPE_PNG ) ) {
        $trnprt_indx = imagecolortransparent( $original_image );
        // If we have a specific transparent color
        if ($trnprt_indx >= 0) {
              // Get the original image"s transparent color"s RGB values
              $trnprt_color  = imagecolorsforindex($original_image, $trnprt_indx);
              // Allocate the same color in the new image resource
              $trnprt_indx       = imagecolorallocate( $new_image, $trnprt_color["red"], $trnprt_color["green"], $trnprt_color["blue"]);
              // Completely fill the background of the new image with allocated color.
              imagefill( $new_image, 0, 0, $trnprt_indx);
              // Set the background color for new image to transparent
              imagecolortransparent( $new_image, $trnprt_indx);
        } 
        // Always make a transparent background color for PNGs that don"t have one allocated already
        elseif ($info[2] == IMAGETYPE_PNG) { 
              // Turn off transparency blending (temporarily)
              imagealphablending( $new_image, false);
              // Create a new transparent color for image
              $color = imagecolorallocatealpha( $new_image, 0, 0, 0, 127);
              // Completely fill the background of the new image with allocated color.
              imagefill( $new_image, 0, 0, $color);
              // Restore transparency blending
              imagesavealpha( $new_image, true);
        }
  }

// imagecopyresampled( $new_image, $original_image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);

// color transparencies white (default is black)

// imagefilledrectangle($new_image, 0, 0, $params['newwidth'], $params['newheight'], imagecolorallocate($new_image, 255, 255, 255));

$rtn_code = imagecopyresampled($new_image, $original_image, 0, 0, $params["xoffset"], $params["yoffset"], $params["newwidth"], $params["newheight"], $params["selectionwidth"], $params["selectionheight"]);
if (!$rtn_code) {
    return false;
}

// grab a compressed jpeg version of the image
ob_start();
  switch ( $info[2] ) {
        case IMAGETYPE_GIF:
              imagegif( $new_image, $output);
              break;
        case IMAGETYPE_JPEG:
              imagejpeg( $new_image, $output);
              break;
        case IMAGETYPE_PNG:
              imagepng( $new_image, $output );
              break;
        default: return false;
  }
$final_image = ob_get_clean();

imagedestroy( $new_image );
imagedestroy( $original_image );

return $final_image;
Indonesian Arsalan Shah Replied 8 years ago

Hi Julius, Its a good idea, we are still thinking to take this into consideration if you create ticket on github , i'll mark it as feature request and will take a look in future version.