472,348 Members | 1,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

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

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

Similar topics

5
by: Kevin Smith | last post by:
Is there a way to "auto-crop" an image in PIL? I have some PNG images with white backgrounds that I want cropped to show only the non-white...
6
by: J. VerSchave | last post by:
I have a bunch of images that are 600x800 800x600 640x480 480x640 I want to use HTML to contrain the size of these images. I basically...
3
by: Paul E Collins | last post by:
I need to load a bitmap image, crop it, and save it. By cropping, I don't mean resizing - I mean reducing it to a fixed size area. Image img =...
3
by: ACaunter | last post by:
Hi there, I was wondering if there was a way to crop a peice of an ASP.Net Image, allowing the user to zoom in on only a section of the picture?? ...
2
by: Mark Denardo | last post by:
Hi, I need some expert GDI+ person to help me with my RoundOffImage Function: What I'm trying to do is take in an image, crop off the edges...
9
by: zacariaz | last post by:
I dont know, and i dont much like javascript, however, i am told that the only way to do want i want to do, is with javascript, so here goes. ...
2
by: Poppa Pimp | last post by:
ImageResizer.php Image Resizer PLEASE HELP The URL of the page this is on in my site is...
1
by: Ofir Picazo | last post by:
Hello everyone, I'm trying to crop a polygon (defined as an array of coordinates) from a larger image. I read lots about gd and imagick but...
2
by: sampath nslabs | last post by:
sir How to crop the image in visual basic 6.0.iam geting in vb.net but i want purelly in vb.plese give the code for that or give me useful...
1
by: koyanpaing | last post by:
Hello everyone, I am using image cropping tools moocrop for cropping image. I would like to know the meaning of crop5.addEvent('onDblClk',...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.