tags:

views:

54

answers:

2

hello i am doing png image cropping using this function. every thing is working fine. but whenever i was uploaded .png then changing the image background color into black. here is the code.`$bgimage_attribs = getimagesize($bgim_file_name);

        if($filetype3=='image/gif') 
            {
                $bgim_old = imagecreatefromgif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 $bgim_old = imagecreatefromjpeg($bgim_file_name);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 $bgim_old = imagecreatefrompng($bgim_file_name);
            }

        $bgth_max_width =265; //for Album image
        $bgth_max_height =150;
        $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];

        $bgth_width =265;//$image_attribs[0] * $ratio; 
        $bgth_height =150;//$image_attribs[1] * $ratio;

        $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
        imageantialias($bgim_new,true); 
        $bgth_file_name = "partners_logs/250x150/$Attachments3";
        imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
        if($filetype3=='image/gif') 
            {
                imagegif($bgim_new,$bgth_file_name,100);
                //$bgim_old = imagegif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 imagejpeg($bgim_new,$bgth_file_name,100);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 imagepng($bgim_new,$bgth_file_name,9);
            }           `
+2  A: 

maybe this helps

Don't forget about imagealphablending() and imagesavealpha() if you're working with [semi]transparent png.

<?php
$file = 'semitransparent.png'; // path to png image
$img = imagecreatefrompng($file); // open image
imagealphablending($img, true); // setting alpha blending on
imagesavealpha($img, true); // save alphablending setting (important)

found at php.net - imagecreatefrompng under User Contributed Notes

NOT tested

edit see comment

<?php
$bgim_new = imagecreatetruecolor($bgth_width,$bgth_height);
imageantialias($bgim_new,true);
imageAlphaBlending($bgim_new, true);
imageSaveAlpha($bgim_new, true);
?>

After some testing and reading througt several pnp.net comments i've got a working script

<?php
$newImageName = "PNG_transparency_copy.png";

$newWidth = 265;
$newHeight = 150;

$orgAttribs = getimagesize("PNG_transparency_demonstration_1.png");

$orginal = imagecreatefrompng("PNG_transparency_demonstration_1.png");
imagesavealpha($orginal, true);

$newPng = imagecreatetruecolor($newWidth,$newHeight);

imagealphablending($newPng, false);
$color = imagecolortransparent($newPng, imagecolorallocatealpha($newPng, 0, 0, 0, 127));
imagefill($newPng, 0, 0, $color);
imagesavealpha($newPng, true);

imagecopyresampled($newPng,$orginal,0,0,0,0,$newWidth,$newHeight, $orgAttribs[0], $orgAttribs[1]);

header("Content-type: image/png");
imagepng($newPng,$newImageName);

PNG taken from Wikipedia

maggie
still not change
muralikalpana
A: 

even i tried like this also

/************************************Resizing the image 80*60****************/
        $path3="partners_logs";
        $filetype3=$_FILES["pimage"]["type"];
        $bgim_file_name = $path3."/".$Attachments2; 
        $bgimage_attribs = getimagesize($bgim_file_name);

        if($filetype3=='image/gif') 
            {
                $bgim_old = imagecreatefromgif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 $bgim_old = imagecreatefromjpeg($bgim_file_name);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 $bgim_old = imagecreatefrompng($bgim_file_name);
                 imageAlphaBlending($bgim_old, true);

imageSaveAlpha($bgim_old, true); }

        $bgth_max_width =265; //for Album image
        $bgth_max_height =150;
        $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];

        $bgth_width =265;//$image_attribs[0] * $ratio; 
        $bgth_height =150;//$image_attribs[1] * $ratio;

        $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
        imageantialias($bgim_new,true); 
        $bgth_file_name = "partners_logs/250x150/$Attachments2";
        imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
        if($filetype3=='image/gif') 
            {
                imagegif($bgim_new,$bgth_file_name,100);
                //$bgim_old = imagegif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 imagejpeg($bgim_new,$bgth_file_name,100);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 imagepng($bgim_new,$bgth_file_name,9);
                   //set the background color to your choice, paramters are int values of red,green and blue  

imagecolorallocate($bgim_new,0xFF,0xFF,0xFF); }
/******End Resize of 80*60*********/

muralikalpana
$bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); imageantialias($bgim_new,true); imageAlphaBlending($bgim_new, true); imageSaveAlpha($bgim_new, true);does this help?
maggie
no use........still maggie. even i put imagecolorallocate($bgim_new,0xFF,0xFF,0xFF);
muralikalpana
hmm i will test this the next days
maggie