PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/shopp/core/model/Image.php

https://github.com/kennethreitz-archive/wordpress-skeleton
PHP | 260 lines | 166 code | 42 blank | 52 comment | 29 complexity | 45d61e4b4f7e9549c85b446e24b2f653 MD5 | raw file
  1. <?php
  2. /**
  3. * ImageProcessor class
  4. *
  5. *
  6. * @author Jonathan Davis
  7. * @version 1.0
  8. * @copyright Ingenesis Limited, 17 April, 2008
  9. * @package Shopp
  10. **/
  11. class ImageProcessor {
  12. var $src;
  13. var $Processed;
  14. function ImageProcessor ($data,$width,$height) {
  15. $this->src = new StdClass();
  16. $this->src->width = $width;
  17. $this->src->height = $height;
  18. $this->src->image = imagecreatefromstring($data);
  19. }
  20. /**
  21. * scaleToWidth()
  22. * Determine the scale percentage by width of the image,
  23. * height is variable to maintain image proportions
  24. **/
  25. function scaleToWidth($width) {
  26. $scale = $width / $this->src->width;
  27. $this->Processed = new StdClass();
  28. $this->Processed->width = $width;
  29. $this->Processed->height = ceil($this->src->height * $scale);
  30. $this->Processed->image = ImageCreateTrueColor($this->Processed->width,$this->Processed->height);
  31. ImageCopyResampled($this->Processed->image, $this->src->image,
  32. 0, 0, 0, 0,
  33. $this->Processed->width, $this->Processed->height, $this->src->width, $this->src->height);
  34. }
  35. /**
  36. * scaleToHeight()
  37. * Determine the scale percentage by height of the image,
  38. * width is variable to maintain image proportions
  39. */
  40. function scaleToHeight($height) {
  41. $scale = $height / $this->src->height;
  42. $this->Processed = new StdClass();
  43. $this->Processed->height = $height;
  44. $this->Processed->width = ceil($this->src->width * $scale);
  45. $this->Processed->image = ImageCreateTrueColor($this->Processed->width,$this->Processed->height);
  46. ImageCopyResampled($this->Processed->image, $this->src->image,
  47. 0, 0, 0, 0,
  48. $this->Processed->width, $this->Processed->height, $this->src->width, $this->src->height);
  49. }
  50. /**
  51. * scaleToFit()
  52. * Resize the image directly to the provided dimensions,
  53. * do not maintain image proportions
  54. */
  55. function scaleToFit($width,$height) {
  56. $this->Processed = new StdClass();
  57. if ($this->src->width > $this->src->height) { // Scale to width
  58. $scale = $width / $this->src->width;
  59. $this->Processed->width = $width;
  60. $this->Processed->height = ceil($this->src->height * $scale);
  61. } else { // Scale to height
  62. $scale = $height / $this->src->height;
  63. $this->Processed->height = $height;
  64. $this->Processed->width = ceil($this->src->width * $scale);
  65. }
  66. $this->Processed->image = ImageCreateTrueColor($this->Processed->width,$this->Processed->height);
  67. ImageCopyResampled($this->Processed->image, $this->src->image,
  68. 0, 0, 0, 0,
  69. $this->Processed->width, $this->Processed->height, $this->src->width, $this->src->height);
  70. }
  71. /**
  72. * scaleCrop()
  73. * Scale based on the smallest dimension,
  74. * cropping the extra on the other dimension to
  75. * maintain image proportion and fit the provided
  76. * dimensions exactly
  77. */
  78. function scaleCrop($width,$height) {
  79. $this->Processed = new StdClass();
  80. $this->Processed->width = $width;
  81. $this->Processed->height = $height;
  82. $widthScale = $width / $this->src->width;
  83. $heightScale = $height / $this->src->height;
  84. $this->Processed->image = ImageCreateTrueColor($this->Processed->width,$this->Processed->height);
  85. if ($heightScale > $widthScale) {
  86. $scale = $height / $this->src->height; // Scale by height
  87. $width = ceil($this->src->width * $scale); // Determine proportional width
  88. $x = ($width - $this->Processed->width)*-0.5; // Center scaled image on the canvas
  89. ImageCopyResampled($this->Processed->image, $this->src->image,
  90. $x, 0, 0, 0,
  91. $width, $this->Processed->height, $this->src->width, $this->src->height);
  92. } else {
  93. $scale = $width / $this->src->width; // Scale by width
  94. $height = ceil($this->src->height * $scale); // Determine proportional height
  95. $y = ($height - $this->Processed->height)*-0.5; // Center scaled image on the canvas
  96. ImageCopyResampled($this->Processed->image, $this->src->image,
  97. 0, $y, 0, 0,
  98. $this->Processed->width, $height, $this->src->width, $this->src->height);
  99. }
  100. }
  101. /**
  102. * Return the processed image
  103. */
  104. function imagefile ($quality=80) {
  105. if (!isset($this->Processed->image)) return false;
  106. imageinterlace($this->Processed->image, true); // For progressive loading
  107. ob_start(); // Start capturing output buffer stream
  108. imagejpeg($this->Processed->image,NULL,$quality); // Output the image to the stream
  109. $buffer = ob_get_contents(); // Get the bugger
  110. ob_end_clean(); // Clear the buffer
  111. return $buffer; // Send it back
  112. }
  113. /**
  114. * UnsharpMask ()
  115. * version 2.1.1
  116. * Unsharp mask algorithm by Torstein Hansi <thoensi_at_netcom_dot_no>, July 2003
  117. **/
  118. function UnsharpMask ($amount=50, $radius=0.5, $threshold=3) {
  119. if (!isset($this->Processed->image)) return false;
  120. $image = $this->Processed->image;
  121. // Attempt to calibrate the parameters to Photoshop
  122. if ($amount > 500) $amount = 500;
  123. $amount = $amount * 0.016;
  124. if ($radius > 50) $radius = 50;
  125. $radius = $radius * 2;
  126. if ($threshold > 255) $threshold = 255;
  127. $radius = abs(round($radius));
  128. if ($radius == 0) return $image;
  129. $w = imagesx($image); $h = imagesy($image);
  130. $canvas = imagecreatetruecolor($w, $h);
  131. $blur = imagecreatetruecolor($w, $h);
  132. /**
  133. * Gaussian blur matrix:
  134. * 1 2 1
  135. * 2 4 2
  136. * 1 2 1
  137. **/
  138. if (function_exists('imageconvolution')) { // PHP >= 5.1
  139. $matrix = array(
  140. array( 1, 2, 1 ),
  141. array( 2, 4, 2 ),
  142. array( 1, 2, 1 )
  143. );
  144. imagecopy ($blur, $image, 0, 0, 0, 0, $w, $h);
  145. imageconvolution($blur, $matrix, 16, 0);
  146. } else {
  147. // Move copies of the image around one pixel at the time and merge them with weight
  148. // according to the matrix. The same matrix is simply repeated for higher radii.
  149. for ($i = 0; $i < $radius; $i++) {
  150. imagecopy ($blur, $image, 0, 0, 1, 0, $w - 1, $h); // left
  151. imagecopymerge ($blur, $image, 1, 0, 0, 0, $w, $h, 50); // right
  152. imagecopymerge ($blur, $image, 0, 0, 0, 0, $w, $h, 50); // center
  153. imagecopy ($canvas, $blur, 0, 0, 0, 0, $w, $h);
  154. imagecopymerge ($blur, $canvas, 0, 0, 0, 1, $w, $h - 1, 33.33333 ); // up
  155. imagecopymerge ($blur, $canvas, 0, 1, 0, 0, $w, $h, 25); // down
  156. }
  157. }
  158. if ($threshold > 0){
  159. // Calculate the difference between the blurred pixels and the original
  160. // and set the pixels
  161. for ($x = 0; $x < $w-1; $x++) { // each row
  162. for ($y = 0; $y < $h; $y++) { // each pixel
  163. $rgbOrig = ImageColorAt($image, $x, $y);
  164. $rOrig = (($rgbOrig >> 16) & 0xFF);
  165. $gOrig = (($rgbOrig >> 8) & 0xFF);
  166. $bOrig = ($rgbOrig & 0xFF);
  167. $rgbBlur = ImageColorAt($blur, $x, $y);
  168. $rBlur = (($rgbBlur >> 16) & 0xFF);
  169. $gBlur = (($rgbBlur >> 8) & 0xFF);
  170. $bBlur = ($rgbBlur & 0xFF);
  171. // When the masked pixels differ less from the original
  172. // than the threshold specifies, they are set to their original value.
  173. $rNew = (abs($rOrig - $rBlur) >= $threshold)
  174. ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig))
  175. : $rOrig;
  176. $gNew = (abs($gOrig - $gBlur) >= $threshold)
  177. ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig))
  178. : $gOrig;
  179. $bNew = (abs($bOrig - $bBlur) >= $threshold)
  180. ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig))
  181. : $bOrig;
  182. if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
  183. $pixCol = ImageColorAllocate($image, $rNew, $gNew, $bNew);
  184. ImageSetPixel($image, $x, $y, $pixCol);
  185. }
  186. }
  187. }
  188. } else {
  189. for ($x = 0; $x < $w; $x++) { // each row
  190. for ($y = 0; $y < $h; $y++) { // each pixel
  191. $rgbOrig = ImageColorAt($image, $x, $y);
  192. $rOrig = (($rgbOrig >> 16) & 0xFF);
  193. $gOrig = (($rgbOrig >> 8) & 0xFF);
  194. $bOrig = ($rgbOrig & 0xFF);
  195. $rgbBlur = ImageColorAt($blur, $x, $y);
  196. $rBlur = (($rgbBlur >> 16) & 0xFF);
  197. $gBlur = (($rgbBlur >> 8) & 0xFF);
  198. $bBlur = ($rgbBlur & 0xFF);
  199. $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;
  200. if ($rNew > 255) $rNew=255;
  201. elseif($rNew < 0) $rNew=0;
  202. $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
  203. if ($gNew > 255) $gNew=255;
  204. elseif($gNew < 0) $gNew=0;
  205. $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;
  206. if($bNew > 255) $bNew=255;
  207. elseif($bNew < 0) $bNew=0;
  208. $rgbNew = ($rNew << 16) + ($gNew <<8) + $bNew;
  209. ImageSetPixel($image, $x, $y, $rgbNew);
  210. }
  211. }
  212. }
  213. imagedestroy($canvas);
  214. imagedestroy($blur);
  215. $this->Processed->image = $image;
  216. }
  217. } // end Image class
  218. ?>