473,561 Members | 3,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

png image crop

5 New Member
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
6 3415
johny10151981
1,059 Top Contributor
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
muralikalpana
5 New Member
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 Top Contributor
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
muralikalpana
5 New Member
still getting same black color
Jul 29 '10 #5
muralikalpana
5 New Member
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 Top Contributor
you didnt add those line anywhere?
you have used imagecoloralloc ate 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
9465
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 portion in the center, but I don't see how I can do this. -- Kevin Smith Kevin.Smith@sas.com
6
28680
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 want to display them as thumnails. Here is what I am trying to do:
3
11286
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 = Bitmap.FromFile("in.bmp"); // what goes here? img.Save("out.bmp"); img.Dispose();
3
8453
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?? any help would be appreciated!! thanks -- AdamPC@hotmail.com
2
2508
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 around an ellipse region I set up, and then return the cropped image from the function. I sort of have this working, but not thoroughly. If I take the output image of this function and draw it on my...
9
3350
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. zoom and cut is the only features i need. 1. the image that need manipulating is places on the server. dont need js for that ;-) 2. the client has the oppotunity to manipulate the image. cut and...
2
2355
by: Poppa Pimp | last post by:
ImageResizer.php Image Resizer PLEASE HELP The URL of the page this is on in my site is http://poppa-pimps-wallpapers.com//ImageResizer.php You can click on browse and get image,but when you upload image it will go to another page and says ]((unable to create emp directory)) Here is a site to be able to see script actually work...
1
3464
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 couldn't find an easy way to do it. Please let me know if you have any ideas. Thanks
2
2836
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 snippts.i expecting source code for crop the image .pls any one can help me.
1
1938
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', function(img,crop,bound){ $('cropped').src = "crop.php?w="+crop.width+"&h="+crop.height+ "&x="+crop.left+"&y="+crop.top; crop.php is for php page or can i use in html with know about php? i...
0
7855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8074
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7608
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5181
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3613
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
885
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.