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);
				}

my problem is in 36,37 lines. image quality. getting balck color in background of the image

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*******************/

plz anybody give a correct solution for this

try

imagepng($bgim_new,$bgth_file_name,100);

the last parameter you pass is image quality - having it 9% of the image quality may cause the black to appear!

here is my full page code. check it for .png image crop. getting background color black.

<?
	if(is_uploaded_file($_FILES["pimage"]["tmp_name"])!='')
			{
			//echo"file in";exit;
			//echo"in ";exit;
			$rand_variable1=rand(10000,100000);
			if(is_uploaded_file($_FILES["pimage"]["tmp_name"]))
			{
			   if($hidden_image!="")
			   {
			  
			   @unlink("partners_logs/".$hidden_image);
			    @unlink("partners_logs/250x150/".$hidden_image);
	           
			   }
			$file_image13=$rand_variable1.$_FILES["pimage"]["name"];
			move_uploaded_file($_FILES["pimage"]["tmp_name"], "partners_logs/".$file_image13);
				
				$Attachments3=$file_image13;
				
			}
			/************************************Resizing the image 80*60****************/
			$path3="partners_logs";
			$filetype3=$_FILES["pimage"]["type"];
			$bgim_file_name = $path3."/".$Attachments3; 
			$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/$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);
					   //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*******************/
						
			}
		 else
		{
		//echo"file out";exit;
		$Attachments3=$hidden_image;
		}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form name="UserDetails" action="" method="post"  enctype="multipart/form-data" onSubmit="return checkValid(this);">
			<input type="hidden" name="new_action" value="<?=$act?>">
			<input type="hidden" name="new_start" value="<?=$start?>">
			<? if($act == 'edit'){
			   $qUsers   = "SELECT * FROM partners WHERE auto_id = $id";
			   $rsUsers  = getSqlQuery($qUsers);
			   $resUsers = getSqlFetch($rsUsers);
			   extract($resUsers);
			?>
			<input type="hidden" name="edit_id" value="<?=$id?>">
			<? }?>
			<table width="50%"  border="0" cellspacing="0" cellpadding="4">
			<? if($errMsg == 1){?>
            <tr align="center">
              <td colspan="5" align="center" valign="middle"><FONT color=red>Admin Name already exist! Please try again.</FONT></td>
            </tr>
			<? }?>
			
			
			
			<tr align="center">
              <td>&nbsp;</td>
              <td align="left">Company Logo</td>
              <td><strong>:</strong></td>
              <td align="left"><input type="file" name="pimage" >
			   <input type="hidden" name="hidden_image" value="<?php echo $images ?>" ></td>
              <td>&nbsp;</td>
            </tr>
			<? if($images!="")
			{ ?>
			<tr align="center">
              <td>&nbsp;</td>
              <td align="left">Company Logo</td>
              <td><strong>:</strong></td>
              <td align="left"><img src="partners_logs/250x150/<?php echo $images ?>"></td>
              <td>&nbsp;</td>
            </tr>
			<? }?>
							     
	        
            <tr align="center">
              <td width="5%" height="1">&nbsp;</td>
              <td width="29%" height="1" align="left">&nbsp;</td>
              <td width="3%" height="1">&nbsp;</td>
              <td width="58%" height="1">&nbsp;</td>
              <td width="5%" height="1">&nbsp;</td>
            </tr>
            <tr>
              <td height="20" colspan="4" align="right">
			  <input type="submit" name=submit value="submit">
			  			  <!--<input name="Submit" type="submit" class="but" id="Submit" value="Submit">-->
                <!--<input name="Reset" type="reset" class="but" id="Submit" value="Reset">-->
				
              <!--<input name="Button" type="button" class="but" id="Submit" value="Cancel" onClick="history.back();">-->                </td>
              <td height="20" align="right">&nbsp;</td>
            </tr>
        </table>
		</form>
</body>
</html>

see this example and implement to your program

Create a PNG image with PHP
# Compile PHP with bundled GD library:

cd /tmp
tar xyf /usr/local/src/php-4.3.1.tar.bz2
cd php-4.3.1
'./configure' '--with-gd' '--with-mysql=/usr/local' \
'--with-mhash=/usr/local' '--with-mcrypt=/usr/local' \
'--enable-sockets' '--enable-trans-sid' '--prefix=/usr/local' \
'--with-readline=/usr' '--with-regex=system' '--with-zlib' \
'--enable-force-cgi-redirect' '--enable-track-vars' \
'--with-gettext'
make
cp sapi/cgi/php ~/public_html/php.cgi

# Sample PHP code:

<?

$w = 600;
$h = 400;

$i = imagecreate($w,$h);
$bg = imagecolorallocate ($i, 0x2B, 0x43, 0x71);
$fg = imagecolorallocate ($i, 0xFF, 0xFF, 0xFF);
$sc = imagecolorallocate ($i, 0xFF, 0xFF, 0x00);
imagestring ($i, 1, 30, $h-30, "Link integrity: number of ping packets received
in 15 minute intervals in last 5 days", $sc);

//imageline($i, 10, 10, 10, $h-10, $sc);
imageline($i, 10, 10, $w-10, 10, $sc);
imageline($i, 10, $h-10, $w-10, $h-10, $sc);
$x = 10;
$y = $h - 10;

$fd = popen ("zcat /home/tom/ping-bug.gz " .
"| perl -ne 'print \"$1\n\" if /seq=(\d+)/'", "r");
$base = 0;
$largest = 0;
$interval = 900;
$nextinterval = $interval;
$x = 11;
while (($n = fgets ($fd)) != "") {
// Each line is a sequence number >= 0 of a received packet.
// Sequence numbers wrap around to 0 after 65535.
$n += $base;
if ($n < $largest - 1000) { $base += 65536; $n += 65536; }
if ($n <= $largest) continue; // duplicate packet, or >1 sec rtt
if ($n > $largest) $largest = $n;
if ($n > $nextinterval) {
$nextinterval += $interval;
$newx = $x + 1;
$newy = $h - 10 - (($h - 20) * $pings_this_interval / $interval);
imageline($i, $x, $y, $newx, $newy, $fg);
$x = $newx;
$y = $newy;
$pings_this_interval = 0;
}
$pings_this_interval++;
}

header("Content-Type: image/png");
imagepng($i);

?>

sorry...my code is simple. but i want to change for png? just change background color

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.