PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/Gloo/Image/Helper.php

http://webgloo.googlecode.com/
PHP | 157 lines | 81 code | 23 blank | 53 comment | 9 complexity | e3e66413c18e96eb99e7324af3b66cfd MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @author rajeevj
  5. */
  6. class Gloo_Image_Helper {
  7. function __construct() {
  8. }
  9. /**
  10. *
  11. * @param <type> $imageURI - The URI on internet where this image is available
  12. * @return <type> - image binary data fetched from this URI
  13. *
  14. */
  15. static function getDataFromURI($imageURI) {
  16. //use curl to pull image data
  17. $ch = curl_init($imageURI);
  18. curl_setopt($ch, CURLOPT_HEADER, 0);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  21. $imageData = curl_exec($ch);
  22. curl_close($ch);
  23. return $imageData;
  24. }
  25. /**
  26. *
  27. * @param <type> $imageData - image binary data, stored as a string
  28. * @return <type> - Dimensions of this image binary
  29. *
  30. */
  31. static function getDimensions($imageData) {
  32. //created an image from raw data using GD2 library
  33. $oSourceImage = imagecreatefromstring($imageData);
  34. if ($oSourceImage == false) {
  35. //unrecoverable error
  36. $errorMsg = "GD2 : Not able to create source image from supplied file data ";
  37. trigger_error($errorMsg, E_USER_ERROR);
  38. }
  39. //original image dimensions
  40. $nWidth = imagesx($oSourceImage);
  41. $nHeight = imagesy($oSourceImage);
  42. $dimensions = array('height' => $nHeight, 'width' => $nWidth);
  43. return $dimensions;
  44. }
  45. /**
  46. *
  47. * @param <type> $width original image width
  48. * @param <type> $height original image height
  49. * @param <type> $frameWidth scale original image width to this width
  50. * @return <type> new dimensions
  51. *
  52. *
  53. */
  54. static function getScaledDimensions($width, $height, $frameWidth, $frameHeight=NULL) {
  55. if (empty($frameHeight)) {
  56. //Determine frame height from original aspect ratio
  57. $frameHeight = ($height / $width) * ($frameWidth);
  58. }
  59. //first try original dimensions for frame
  60. $newHeight = $height;
  61. $newWidth = $width;
  62. //calculate new height/ width using aspect-ratio
  63. if ($newHeight > $frameHeight) {
  64. $aspectRatio = ($frameHeight / $newHeight);
  65. $newHeight = $aspectRatio * $newHeight;
  66. $newWidth = $aspectRatio * $newWidth;
  67. }
  68. if ($newWidth > $frameWidth) {
  69. $aspectRatio = ($frameWidth / $newWidth);
  70. $newHeight = $aspectRatio * $newHeight;
  71. $newWidth = $aspectRatio * $newWidth;
  72. }
  73. //Round up to nearest integer
  74. $newWidth = floor($newWidth);
  75. $newHeight = floor($newHeight);
  76. $dimensions = array('width' => $newWidth, 'height' => $newHeight);
  77. return $dimensions;
  78. }
  79. /**
  80. *
  81. * Take an image binary and resize it to fit frame width and height
  82. *
  83. * @param <type> $sImageData source Image file binary data
  84. * @param <type> $frameWidth frame width to resize
  85. * @param <type> $frameHeight frame height to resize
  86. * @return <type> - New resized image binary
  87. *
  88. */
  89. static function getResizedImage($sImageData, $frameWidth, $frameHeight) {
  90. $oSourceImage = imagecreatefromstring($sImageData);
  91. if ($oSourceImage == false) {
  92. //unrecoverable error
  93. $errorMsg = "GD2 : Not able to create source image from supplied file data ";
  94. trigger_error($errorMsg, E_USER_ERROR);
  95. }
  96. //original width and height
  97. $nWidth = imagesx($oSourceImage);
  98. $nHeight = imagesy($oSourceImage);
  99. //we try original dimensions for frame
  100. $nDestinationHeight = $nHeight;
  101. $nDestinationWidth = $nWidth;
  102. //calculate new height/ width using aspect-ratio
  103. if ($nDestinationHeight > $frameHeight) {
  104. $aspectRatio = ($frameHeight / $nDestinationHeight);
  105. $nDestinationHeight = $aspectRatio * $nDestinationHeight;
  106. $nDestinationWidth = $aspectRatio * $nDestinationWidth;
  107. }
  108. if ($nDestinationWidth > $frameWidth) {
  109. $aspectRatio = ($frameWidth / $nDestinationWidth);
  110. $nDestinationHeight = $aspectRatio * $nDestinationHeight;
  111. $nDestinationWidth = $aspectRatio * $nDestinationWidth;
  112. }
  113. //Round up to nearest integer
  114. $nDestinationWidth = floor($nDestinationWidth);
  115. $nDestinationHeight = floor($nDestinationHeight);
  116. //an image identifier representing a blank image of specified size
  117. $oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
  118. //http://in2.php.net/manual/en/function.imagecopyresized.php
  119. // resize the image
  120. imagecopyresized($oDestinationImage,
  121. $oSourceImage, 0, 0, 0, 0,
  122. $nDestinationWidth, $nDestinationHeight,
  123. $nWidth, $nHeight);
  124. ob_start();
  125. //default is 75. is Quality 75 good enough for us?
  126. imageJPEG($oDestinationImage, NULL, 75);
  127. $sNewImage = ob_get_contents();
  128. ob_end_clean();
  129. // Free up memory
  130. imagedestroy($oDestinationImage);
  131. return $sNewImage;
  132. }
  133. }
  134. ?>