Upload and Resize PNG and GIF images

Reply

Join Date: Mar 2008
Posts: 36
Reputation: lonestar23 is an unknown quantity at this point 
Solved Threads: 0
lonestar23 lonestar23 is offline Offline
Light Poster

Upload and Resize PNG and GIF images

 
0
  #1
Dec 9th, 2008
How would I go about uploading and resizing a transparent PNG, GIF image using PHP? I have a script but it only works with JPG images and of course JPG images do not support transparent images.

The image also needs to maintain it's transparency when resized.

Thanks in Advance!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Re: Upload and Resize PNG and GIF images

 
0
  #2
Dec 10th, 2008
  1. <?php
  2. //header ("Content-type: image/jpeg");
  3. /*
  4. JPEG / PNG Image Resizer
  5. Parameters (passed via URL):
  6.  
  7. img = path / url of jpeg or png image file
  8.  
  9. percent = if this is defined, image is resized by it's
  10.   value in percent (i.e. 50 to divide by 50 percent)
  11.  
  12. w = image width
  13.  
  14. h = image height
  15.  
  16. constrain = if this is parameter is passed and w and h are set
  17.   to a size value then the size of the resulting image
  18.   is constrained by whichever dimension is smaller
  19.  
  20. Requires the PHP GD Extension
  21.  
  22. Outputs the resulting image in JPEG Format
  23.  
  24. By: Michael John G. Lopez - www.sydel.net
  25. Filename : imgsize.php
  26. */
  27. //resize('images/phone.jpg','images/phone_thumb.jpg',100);
  28.  
  29. function uploadpic($file,$thumb_file,$field,$thumb_width=100){
  30. $result=false;
  31. if(is_uploaded_file($_FILES[$field]['tmp_name']))
  32. {
  33. move_uploaded_file($_FILES[$field]['tmp_name'],$file);
  34. thumbnail($file,$thumb_file,$thumb_width);
  35. $result=true;
  36. }
  37. return $result;
  38. }
  39.  
  40.  
  41. function resize($orig_file,$thumb_file,$prop){
  42. $img = $orig_file;
  43. $constrain = true;
  44. $w = $prop;
  45. $h = $prop;
  46.  
  47. // get image size of img
  48. $x = @getimagesize($img);
  49. // image width
  50. $sw = $x[0];
  51. // image height
  52. $sh = $x[1];
  53.  
  54. if ($percent > 0) {
  55. // calculate resized height and width if percent is defined
  56. $percent = $percent * 0.01;
  57. $w = $sw * $percent;
  58. $h = $sh * $percent;
  59. } else {
  60. if (isset ($w) AND !isset ($h)) {
  61. // autocompute height if only width is set
  62. $h = (100 / ($sw / $w)) * .01;
  63. $h = @round ($sh * $h);
  64. } elseif (isset ($h) AND !isset ($w)) {
  65. // autocompute width if only height is set
  66. $w = (100 / ($sh / $h)) * .01;
  67. $w = @round ($sw * $w);
  68. } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
  69. // get the smaller resulting image dimension if both height
  70. // and width are set and $constrain is also set
  71. $hx = (100 / ($sw / $w)) * .01;
  72. $hx = @round ($sh * $hx);
  73.  
  74. $wx = (100 / ($sh / $h)) * .01;
  75. $wx = @round ($sw * $wx);
  76.  
  77. if ($hx < $h) {
  78. $h = (100 / ($sw / $w)) * .01;
  79. $h = @round ($sh * $h);
  80. } else {
  81. $w = (100 / ($sh / $h)) * .01;
  82. $w = @round ($sw * $w);
  83. }
  84. }
  85. }
  86.  
  87. $im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
  88. $im = @ImageCreateFromPNG ($img) or // or PNG Image
  89. $im = @ImageCreateFromGIF ($img) or // or GIF Image
  90. $im = false; // If image is not JPEG, PNG, or GIF
  91.  
  92. if (!$im) {
  93. // We get errors from PHP's ImageCreate functions...
  94. // So let's echo back the contents of the actual image.
  95. readfile ($img);
  96. } else {
  97. // Create the resized image destination
  98. $thumb = @ImageCreateTrueColor ($w, $h);
  99. // Copy from image source, resize it, and paste to image destination
  100. @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
  101. // Output resized image
  102. @ImageJPEG ($thumb,$thumb_file);
  103. }
  104. }
  105. ?>
:: xarz ::
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 36
Reputation: lonestar23 is an unknown quantity at this point 
Solved Threads: 0
lonestar23 lonestar23 is offline Offline
Light Poster

Re: Upload and Resize PNG and GIF images

 
0
  #3
Dec 10th, 2008
Thanks for the code sample, I will test it out!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Re: Upload and Resize PNG and GIF images

 
0
  #4
Dec 10th, 2008
Originally Posted by lonestar23 View Post
Thanks for the code sample, I will test it out!
actually, I myself use it and its really working..
:: xarz ::
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: cosmox is an unknown quantity at this point 
Solved Threads: 0
cosmox cosmox is offline Offline
Newbie Poster

Re: Upload and Resize PNG and GIF images

 
0
  #5
Dec 12th, 2008
Originally Posted by xarz View Post
actually, I myself use it and its really working..
Hi, - great script -btw,
.... but

@ImageJPEG ($thumb,$thumb_file);

will create only a JPG thumb image.
If i have a PNG image file to resize (with transparent background), it will get me a JPG image as result - with black background.

I modified the function above to the one:

@ImagePNG ($thumb,$thumb_file);

but, still having a black background on the result PNG image ( I want to have the same transparent background as the initial file)..

Can you help me with that ?!
C.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 24
Reputation: xarz is an unknown quantity at this point 
Solved Threads: 1
xarz's Avatar
xarz xarz is offline Offline
Newbie Poster

Re: Upload and Resize PNG and GIF images

 
0
  #6
Dec 12th, 2008
:: xarz ::
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: cosmox is an unknown quantity at this point 
Solved Threads: 0
cosmox cosmox is offline Offline
Newbie Poster

Re: Upload and Resize PNG and GIF images

 
0
  #7
Dec 12th, 2008
Originally Posted by xarz View Post
check this page

http://mediumexposure.com/techblog/s...and-gd-library
Super. Works super !!!.
Lots of THANKS for the link !
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC