Posted By

iTony on 11/20/08


Tagged

class gd images php5


Versions (?)

image wrapper class using gd library


 / Published in: PHP
 

  1. <?php
  2.  
  3.  
  4. class image
  5. {
  6. private $supported, $edit, $root, $image;
  7.  
  8. public function __construct( $root )
  9. {
  10. //Check if we're working with an actual
  11. //Directory
  12. if( is_dir($root) )
  13. {
  14. //Fill in the array of the things we can do
  15. $this->edit = array(
  16. 'negative' => IMG_FILTER_NEGATE,
  17. 'greyscale' => IMG_FILTER_GRAYSCALE,
  18. 'brightness'=> IMG_FILTER_BRIGHTNESS,
  19. 'contract' => IMG_FILTER_CONTRAST,
  20. 'colorize' => IMG_FILTER_COLORIZE,
  21. 'edge' => IMG_FILTER_EDGEDETECT,
  22. 'emboss' => IMG_FILTER_EMBOSS,
  23. 'guassion' => IMG_FILTER_GAUSSIAN_BLUR,
  24. 'blur' => IMG_FILTER_SELECTIVE_BLUR,
  25. 'sketch' => IMG_FILTER_MEAN_REMOVAL,
  26. 'smooth' => IMG_FILTER_SMOOTH
  27. );
  28.  
  29. //Fill in an array of the image formats we support
  30. $this->supported = array(
  31. 'jpeg' => 'jpeg',
  32. 'jpg' => 'jpeg',
  33. 'png' => 'png',
  34. 'gif' => 'gif'
  35. );
  36. //Finally fill in the root and enforce trailing slash
  37. $this->root = rtrim( $root, '/' ) . '/';
  38.  
  39. } else {
  40.  
  41. throw new Exception("[Class Of: Image] Provided root directory does not exists");
  42. }
  43. }
  44.  
  45. public function set( $img )
  46. {
  47. //See if our image file exists and it's supprted
  48. if( file_exists( $this->root . $img ) )
  49. {
  50. $type = pathinfo( $this->root . $img, PATHINFO_EXTENSION );
  51. if( array_key_exists( $type, $this->supported ) )
  52. {
  53.  
  54. //Set all neccesary information
  55. $function = 'imagecreatefrom' . $this->supported[ $type ];
  56. $this->image['file'] = $img;
  57. $this->image['oResource'] = $function( $this->root . $img );
  58. $this->image['width'] = imagesx( $this->image['oResource'] );
  59. $this->image['height'] = imagesy( $this->image['oResource'] );
  60. $this->image['type'] = $this->supported[ $type ];
  61.  
  62. ///Below is error reprting
  63. } else {
  64.  
  65. throw new Exception("[Class Of: Image] Image file is not currently supported");
  66. }
  67.  
  68. } else {
  69.  
  70. throw new Exception("[Class Of: Image] Image file does not exists relative to root");
  71. }
  72.  
  73. return $this;
  74. }
  75.  
  76. public function execute( $filename = '' )
  77. {
  78. //Make a filename of one is not provided
  79. if( $filename == '' )
  80. {
  81. $filename = '_edited_' . $this->image['file'];
  82. }
  83. //Create image
  84. $function = 'image' . $this->image['type'];
  85. $function( $this->image['oResource'], $this->root . $filename, 100);
  86.  
  87. //Clean cache
  88. imagedestroy( $this->image['oResource'] );
  89. }
  90.  
  91. public function effect()
  92. {
  93. //gather up all our arguments
  94. $args = func_get_args();
  95.  
  96. //Check if I support what they want done
  97. if( array_key_exists( $args[0], $this->edit ) )
  98. {
  99. //Create priliminary argument array
  100. $arguments = array( $this->image['oResource'], $this->edit[ $args[0] ]);
  101. unset($args[0]); sort( $args ); //unset the unnecesaries
  102.  
  103. //Now all our image filter function
  104. $arguments = array_merge( $arguments, $args );
  105. call_user_func_array( 'imagefilter', $arguments );
  106. return $this;
  107.  
  108. ///Error reporting
  109. } else {
  110.  
  111. throw new Exception("[Class Of: Gallery] Effect not supported");
  112. }
  113. }
  114.  
  115. public function ratio( $maxWidth )
  116. {
  117. //Calculate the max height and send off to resize function
  118. $maxHeight = ($maxWidth * $this->image['height']) / $this->image['width'];
  119. $this->resize( $maxWidth, $maxHeight );
  120. }
  121.  
  122. public function resize( $width, $height = '' )
  123. {
  124. if( empty($height) )
  125. {
  126. //Assume width is a percentage value now
  127. //Check for .20 || .2 bug
  128. $num = ( $num > 10 ) ? '.' . $num : '.0' . $num;
  129. $width = $this->image['width'] - ($this->image['width'] * $num);
  130. $height = $this->image['height'] - ($this->image['height'] * $num);
  131. }
  132. //Create a temporary pallete
  133. $img = imagecreatetruecolor( $width, $height );
  134. imagecopyresampled( $img, $this->image['oResource'], 0, 0, 0, 0, $width, $height, $this->image['width'], $this->image['height']);
  135.  
  136. //Fill in the original with the new
  137. $this->image['oResource'] = $img;
  138. $this->image['width'] = $width;
  139. $this->image['height'] = $height;
  140.  
  141. return $this;
  142. }
  143.  
  144. public function flip( $dir )
  145. {
  146. //Check is we support basically
  147. switch( $dir )
  148. {
  149. //The process for flipping either left and right
  150. //Deviates by the subtraction of one variable
  151. case $dir == 'left' || $dir == 'right':
  152. {
  153. //Create a pallete
  154. $img = imagecreatetruecolor( $this->image['height'], $this->image['width'] );
  155.  
  156. //Begin traversing the image plane at the left uppermost corner
  157. //travel down to the right down corner
  158. for( $y = $this->image['height']; $y >= 0; --$y )
  159. {
  160. for( $x = 0; $x <= $this->image['width']; ++$x )
  161. {
  162. //Extract color index
  163. $rgb = imagecolorat( $this->image['oResource'], $x, $y );
  164.  
  165. //Determine side so we can check which variable to subtract from
  166. if( $dir == 'left' )
  167. {
  168. imagesetpixel( $img, $y, $this->image['width'] - $x, $rgb );
  169.  
  170. } else {
  171.  
  172. imagesetpixel( $img, $this->image['height'] - $y, $x, $rgb);
  173. }
  174. }
  175. }
  176.  
  177. //set new height and width
  178. $this->image['width'] = imagesx( $img );
  179. $this->image['height'] = imagesy( $img );
  180.  
  181. break;
  182. }
  183.  
  184. //Down is rather simple...
  185. case $dir == 'down' :
  186. {
  187. $img = imagerotate( $this->image['oResource'], 180, 0);
  188. }
  189. }
  190.  
  191. //set new image resource
  192. $this->image['oResource'] = $img;
  193.  
  194. return $this;
  195. }
  196.  
  197. public function crop( $oX, $oY, $nX, $nY )
  198. {
  199. //We must determine the number by which to
  200. //incrementing variables as well as the length
  201. //and height of the new image
  202. if( $oX > $nX )
  203. {
  204. $xBegin = $nX;
  205. $xLen = $oX - $nX;
  206. } else {
  207.  
  208. $xBegin = $oX;
  209. $xLen = $nX - $oX;
  210. }
  211.  
  212. if( $oY > $nY )
  213. {
  214. $yBegin = $nY;
  215. $yLen = $oY - $nY;
  216. } else {
  217.  
  218. $yBegin = $oY;
  219. $yLen = $nY - $oY;
  220. }
  221.  
  222. //Create pallete
  223. $img = imagecreatetruecolor( $xLen, $yLen );
  224.  
  225. //Begin incrementing at 0 as if traversing
  226. //the new image instead of the old
  227. for( $y = 0; $y <= $yLen; ++$y )
  228. {
  229. for( $x = 0; $x <= $xLen; ++$x )
  230. {
  231. //Adding our pretermined value takes us the pixel
  232. //location we wanted to go to
  233. $rgb = imagecolorat( $this->image['oResource'], $x + $xBegin, $y + $yBegin);
  234. imagesetpixel( $img, $x, $y , $rgb );
  235. }
  236. }
  237.  
  238. //Set neccesary information
  239. $this->image['oResource'] = $img;
  240. $this->image['height'] = imagesy( $img );
  241. $this->image['width'] = imagesx( $img );
  242.  
  243. return $this;
  244. }
  245. }
  246.  
  247. ?>

Report this snippet  

You need to login to post a comment.