png image crop – PHP

  php

Q(Question):

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.my problem is image quality. getting balck color in background of the image .

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

A(Answer):

before line 26 you need to fill up the background of your
$bgim_new. otherwise by default the background is BLACK

A(Answer):

tryed like this also but no use.

/************************************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);
}
$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);
if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
{
imagecolorallocate($bgim_new,0xFF,0xFF,0xFF);
}
$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
}
/************End Resize of 80*60*******************/

A(Answer):

try to add this line after line 28(in the post)


$white = imagecolorallocate($bgim_new, 255, 255, 255); //FOR WHITE BACKGROUND
imagefilledrectangle($bgim_new,0,0,$bgth_width,$bgth_height,$white);

A(Answer):

still getting same black color

A(Answer):

here this is my full page. upload png once see what is going on?

<?
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>

A(Answer):

you didnt add those line anywhere?
you have used imagecolorallocate function but you didnt receive the returned value.
you need to receive the returned value and must have to fill all the background with that returned value

LEAVE A COMMENT