469,358 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

png image crop

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 .
Expand|Select|Wrap|Line Numbers
  1. $bgimage_attribs = getimagesize($bgim_file_name);
  2.  
  3.             if($filetype3=='image/gif')    
  4.                 {
  5.                     $bgim_old = imagecreatefromgif($bgim_file_name); 
  6.                 }    
  7.             else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))    
  8.                 {
  9.                      $bgim_old = imagecreatefromjpeg($bgim_file_name);
  10.                 }
  11.               else    if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  12.                 {
  13.                      $bgim_old = imagecreatefrompng($bgim_file_name);
  14.                 }
  15.  
  16.             $bgth_max_width =265; //for Album image
  17.             $bgth_max_height =150;
  18.             $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];
  19.  
  20.             $bgth_width =265;//$image_attribs[0] * $ratio; 
  21.             $bgth_height =150;//$image_attribs[1] * $ratio;
  22.  
  23.             $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
  24.             imageantialias($bgim_new,true); 
  25.             $bgth_file_name = "partners_logs/250x150/$Attachments3";
  26.             imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
  27.             if($filetype3=='image/gif')    
  28.                 {
  29.                     imagegif($bgim_new,$bgth_file_name,100);
  30.                     //$bgim_old = imagegif($bgim_file_name); 
  31.                 }    
  32.             else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))    
  33.                 {
  34.                      imagejpeg($bgim_new,$bgth_file_name,100);
  35.                 }
  36.               else    if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  37.                 {
  38.                      imagepng($bgim_new,$bgth_file_name,9);
  39.                 }
Jul 28 '10 #1

✓ answered by johny10151981

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

6 3056
johny10151981
1,059 1GB
before line 26 you need to fill up the background of your
$bgim_new. otherwise by default the background is BLACK
Jul 29 '10 #2
tryed like this also but no use.
Expand|Select|Wrap|Line Numbers
  1. /************************************Resizing the image 80*60****************/
  2.             $path3="partners_logs";
  3.             $filetype3=$_FILES["pimage"]["type"];
  4.             $bgim_file_name = $path3."/".$Attachments2; 
  5.             $bgimage_attribs = getimagesize($bgim_file_name);
  6.  
  7.             if($filetype3=='image/gif')    
  8.                 {
  9.                     $bgim_old = imagecreatefromgif($bgim_file_name); 
  10.                 }    
  11.             else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))    
  12.                 {
  13.                      $bgim_old = imagecreatefromjpeg($bgim_file_name);
  14.                 }
  15.               else    if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  16.                 {
  17.                      $bgim_old = imagecreatefrompng($bgim_file_name);
  18.  
  19.                 }
  20.  
  21.             $bgth_max_width =265; //for Album image
  22.             $bgth_max_height =150;
  23.             $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];
  24.  
  25.             $bgth_width =265;//$image_attribs[0] * $ratio; 
  26.             $bgth_height =150;//$image_attribs[1] * $ratio;
  27.  
  28.             $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
  29.             imageantialias($bgim_new,true); 
  30.             if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  31.                 {
  32.                 imagecolorallocate($bgim_new,0xFF,0xFF,0xFF);
  33.                 }
  34.             $bgth_file_name = "partners_logs/250x150/$Attachments2";
  35.             imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
  36.             if($filetype3=='image/gif')    
  37.                 {
  38.                     imagegif($bgim_new,$bgth_file_name,100);
  39.                     //$bgim_old = imagegif($bgim_file_name); 
  40.                 }    
  41.             else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))    
  42.                 {
  43.                      imagejpeg($bgim_new,$bgth_file_name,100);
  44.                 }
  45.               else    if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  46.                 {
  47.  
  48.                      imagepng($bgim_new,$bgth_file_name,9);
  49.                        //set the background color to your choice, paramters are int values of red,green and blue  
  50.  
  51.                 }                
  52.             /************End Resize of 80*60*******************/
Jul 29 '10 #3
johny10151981
1,059 1GB
try to add this line after line 28(in the post)
Expand|Select|Wrap|Line Numbers
  1.  
  2. $white = imagecolorallocate($bgim_new, 255, 255, 255);    //FOR WHITE BACKGROUND
  3. imagefilledrectangle($bgim_new,0,0,$bgth_width,$bgth_height,$white);
  4.  
Jul 29 '10 #4
still getting same black color
Jul 29 '10 #5
here this is my full page. upload png once see what is going on?
Expand|Select|Wrap|Line Numbers
  1. <?
  2.     if(is_uploaded_file($_FILES["pimage"]["tmp_name"])!='')
  3.             {
  4.             //echo"file in";exit;
  5.             //echo"in ";exit;
  6.             $rand_variable1=rand(10000,100000);
  7.             if(is_uploaded_file($_FILES["pimage"]["tmp_name"]))
  8.             {
  9.                if($hidden_image!="")
  10.                {
  11.  
  12.                @unlink("partners_logs/".$hidden_image);
  13.                 @unlink("partners_logs/250x150/".$hidden_image);
  14.  
  15.                }
  16.             $file_image13=$rand_variable1.$_FILES["pimage"]["name"];
  17.             move_uploaded_file($_FILES["pimage"]["tmp_name"], "partners_logs/".$file_image13);
  18.  
  19.                 $Attachments3=$file_image13;
  20.  
  21.             }
  22.             /************************************Resizing the image 80*60****************/
  23.             $path3="partners_logs";
  24.             $filetype3=$_FILES["pimage"]["type"];
  25.             $bgim_file_name = $path3."/".$Attachments3; 
  26.             $bgimage_attribs = getimagesize($bgim_file_name);
  27.  
  28.             if($filetype3=='image/gif')    
  29.                 {
  30.                     $bgim_old = imagecreatefromgif($bgim_file_name); 
  31.                 }    
  32.             else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))    
  33.                 {
  34.                      $bgim_old = imagecreatefromjpeg($bgim_file_name);
  35.                 }
  36.               else    if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  37.                 {
  38.                      $bgim_old = imagecreatefrompng($bgim_file_name);
  39.                      imageAlphaBlending($bgim_old, true);
  40. imageSaveAlpha($bgim_old, true);
  41.                 }
  42.  
  43.             $bgth_max_width =265; //for Album image
  44.             $bgth_max_height =150;
  45.             $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];
  46.  
  47.             $bgth_width =265;//$image_attribs[0] * $ratio; 
  48.             $bgth_height =150;//$image_attribs[1] * $ratio;
  49.  
  50.             $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
  51.             imageantialias($bgim_new,true); 
  52.             $bgth_file_name = "partners_logs/250x150/$Attachments3";
  53.             imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
  54.             if($filetype3=='image/gif')    
  55.                 {
  56.                     imagegif($bgim_new,$bgth_file_name,100);
  57.                     //$bgim_old = imagegif($bgim_file_name); 
  58.                 }    
  59.             else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))    
  60.                 {
  61.                      imagejpeg($bgim_new,$bgth_file_name,100);
  62.                 }
  63.               else    if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
  64.                 {
  65.                      imagepng($bgim_new,$bgth_file_name,9);
  66.                        //set the background color to your choice, paramters are int values of red,green and blue  
  67. imagecolorallocate($bgim_new,0xFF,0xFF,0xFF);
  68.                 }                
  69.             /************End Resize of 80*60*******************/
  70.  
  71.             }
  72.          else
  73.         {
  74.         //echo"file out";exit;
  75.         $Attachments3=$hidden_image;
  76.         }
  77. ?>
  78. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  79. <html xmlns="http://www.w3.org/1999/xhtml">
  80. <head>
  81. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  82. <title>Untitled Document</title>
  83. </head>
  84.  
  85. <body>
  86. <form name="UserDetails" action="" method="post"  enctype="multipart/form-data" onSubmit="return checkValid(this);">
  87.             <input type="hidden" name="new_action" value="<?=$act?>">
  88.             <input type="hidden" name="new_start" value="<?=$start?>">
  89.             <? if($act == 'edit'){
  90.                $qUsers   = "SELECT * FROM partners WHERE auto_id = $id";
  91.                $rsUsers  = getSqlQuery($qUsers);
  92.                $resUsers = getSqlFetch($rsUsers);
  93.                extract($resUsers);
  94.             ?>
  95.             <input type="hidden" name="edit_id" value="<?=$id?>">
  96.             <? }?>
  97.             <table width="50%"  border="0" cellspacing="0" cellpadding="4">
  98.             <? if($errMsg == 1){?>
  99.             <tr align="center">
  100.               <td colspan="5" align="center" valign="middle"><FONT color=red>Admin Name already exist! Please try again.</FONT></td>
  101.             </tr>
  102.             <? }?>
  103.  
  104.  
  105.  
  106.             <tr align="center">
  107.               <td>&nbsp;</td>
  108.               <td align="left">Company Logo</td>
  109.               <td><strong>:</strong></td>
  110.               <td align="left"><input type="file" name="pimage" >
  111.                <input type="hidden" name="hidden_image" value="<?php echo $images ?>" ></td>
  112.               <td>&nbsp;</td>
  113.             </tr>
  114.             <? if($images!="")
  115.             { ?>
  116.             <tr align="center">
  117.               <td>&nbsp;</td>
  118.               <td align="left">Company Logo</td>
  119.               <td><strong>:</strong></td>
  120.               <td align="left"><img src="partners_logs/250x150/<?php echo $images ?>"></td>
  121.               <td>&nbsp;</td>
  122.             </tr>
  123.             <? }?>
  124.  
  125.  
  126.             <tr align="center">
  127.               <td width="5%" height="1">&nbsp;</td>
  128.               <td width="29%" height="1" align="left">&nbsp;</td>
  129.               <td width="3%" height="1">&nbsp;</td>
  130.               <td width="58%" height="1">&nbsp;</td>
  131.               <td width="5%" height="1">&nbsp;</td>
  132.             </tr>
  133.             <tr>
  134.               <td height="20" colspan="4" align="right">
  135.               <input type="submit" name=submit value="submit">
  136.                             <!--<input name="Submit" type="submit" class="but" id="Submit" value="Submit">-->
  137.                 <!--<input name="Reset" type="reset" class="but" id="Submit" value="Reset">-->
  138.  
  139.               <!--<input name="Button" type="button" class="but" id="Submit" value="Cancel" onClick="history.back();">-->                </td>
  140.               <td height="20" align="right">&nbsp;</td>
  141.             </tr>
  142.         </table>
  143.         </form>
  144. </body>
  145. </html>
Jul 29 '10 #6
johny10151981
1,059 1GB
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
Jul 29 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Kevin Smith | last post: by
6 posts views Thread by J. VerSchave | last post: by
3 posts views Thread by Paul E Collins | last post: by
3 posts views Thread by ACaunter | last post: by
9 posts views Thread by zacariaz | last post: by
1 post views Thread by Ofir Picazo | last post: by
1 post views Thread by koyanpaing | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.