code_junkie

How do i resize and convert an uploaded image to a PNG using GD

Nov 10th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. <?php
  2. /*
  3. Resizes an image and converts it to PNG returning the PNG data as a string
  4. */
  5. function imageToPng($srcFile, $maxSize = 100) {
  6. list($width_orig, $height_orig, $type) = getimagesize($srcFile);
  7.  
  8. // Get the aspect ratio
  9. $ratio_orig = $width_orig / $height_orig;
  10.  
  11. $width = $maxSize;
  12. $height = $maxSize;
  13.  
  14. // resize to height (orig is portrait)
  15. if ($ratio_orig < 1) {
  16. $width = $height * $ratio_orig;
  17. }
  18. // resize to width (orig is landscape)
  19. else {
  20. $height = $width / $ratio_orig;
  21. }
  22.  
  23. // Temporarily increase the memory limit to allow for larger images
  24. ini_set('memory_limit', '32M');
  25.  
  26. switch ($type)
  27. {
  28. case IMAGETYPE_GIF:
  29. $image = imagecreatefromgif($srcFile);
  30. break;
  31. case IMAGETYPE_JPEG:
  32. $image = imagecreatefromjpeg($srcFile);
  33. break;
  34. case IMAGETYPE_PNG:
  35. $image = imagecreatefrompng($srcFile);
  36. break;
  37. default:
  38. throw new Exception('Unrecognized image type ' . $type);
  39. }
  40.  
  41. // create a new blank image
  42. $newImage = imagecreatetruecolor($width, $height);
  43.  
  44. // Copy the old image to the new image
  45. imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  46.  
  47. // Output to a temp file
  48. $destFile = tempnam();
  49. imagepng($newImage, $destFile);
  50.  
  51. // Free memory
  52. imagedestroy($newImage);
  53.  
  54. if ( is_file($destFile) ) {
  55. $f = fopen($destFile, 'rb');
  56. $data = fread($f);
  57. fclose($f);
  58.  
  59. // Remove the tempfile
  60. unlink($destFile);
  61. return $data;
  62. }
  63.  
  64. throw new Exception('Image conversion failed.');
  65. }
  66.  
  67. <?php
  68. //Input file
  69. $file = "myImage.png";
  70. $img = ImageCreateFromPNG($file);
  71.  
  72. //Dimensions
  73. $width = imagesx($img);
  74. $height = imagesy($img);
  75. $max_width = 300;
  76. $max_height = 300;
  77. $percentage = 1;
  78.  
  79. //Image scaling calculations
  80. if ( $width > $max_width ) {
  81. $percentage = ($height / ($width / $max_width)) > $max_height ?
  82. $height / $max_height :
  83. $width / $max_width;
  84. }
  85. elseif ( $height > $max_height) {
  86. $percentage = ($width / ($height / $max_height)) > $max_width ?
  87. $width / $max_width :
  88. $height / $max_height;
  89. }
  90. $new_width = $width / $percentage;
  91. $new_height = $height / $percentage;
  92.  
  93. //scaled image
  94. $out = imagecreatetruecolor($new_width, $new_height);
  95. imagecopyresampled($out, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  96.  
  97. //output image
  98. imagepng($out);
  99. ?>
Add Comment
Please, Sign In to add comment