943,545 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 18435
  • PHP RSS
Dec 9th, 2008
0

Upload and Resize PNG and GIF images

Expand Post »
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!
Similar Threads
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Dec 10th, 2008
-1

Re: Upload and Resize PNG and GIF images

php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Dec 10th, 2008
0

Re: Upload and Resize PNG and GIF images

Thanks for the code sample, I will test it out!
Reputation Points: 20
Solved Threads: 0
Light Poster
lonestar23 is offline Offline
43 posts
since Mar 2008
Dec 10th, 2008
0

Re: Upload and Resize PNG and GIF images

Click to Expand / Collapse  Quote originally posted by lonestar23 ...
Thanks for the code sample, I will test it out!
actually, I myself use it and its really working..
Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Dec 12th, 2008
0

Re: Upload and Resize PNG and GIF images

Click to Expand / Collapse  Quote originally posted by xarz ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cosmox is offline Offline
2 posts
since Dec 2008
Dec 12th, 2008
1

Re: Upload and Resize PNG and GIF images

Reputation Points: 10
Solved Threads: 1
Newbie Poster
xarz is offline Offline
24 posts
since Nov 2008
Dec 12th, 2008
0

Re: Upload and Resize PNG and GIF images

Super. Works super !!!.
Lots of THANKS for the link !
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cosmox is offline Offline
2 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: My SQLServer 2005 with apache2triad
Next Thread in PHP Forum Timeline: Adding validation after form finished





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC