login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > GD library > Image Manipulator (Watermark and Thumb

Image Manipulator (Watermark and Thumb

Auteur: Sean - 20 maart 2005 - 19:08 - Gekeurd door: XenoX - Hits: 6746 - Aantal punten: 3.50 (4 stemmen)




Alle informatie staat in de comments, echter staat het wel in het engels zodat het script niet alleen door Nederlands-sprekenden gebruikt kan worden.

Code:
  1. <?php
  2. /**
  3.  * Image manipulation class. Create thumbnails or add watermarks to an image
  4.  * Created for easy usage
  5.  *
  6.  * @author Sean Koole
  7.  * @access public
  8.  */
  9. class Image
  10. {
  11. /**
  12. * Create a thumbnail
  13. *
  14. * @param string $imgPath The path of the image
  15. * @param string $newPath The path to where the image should be copied to
  16. * @param bool $overwrite Do you wish to overwrite the file if it already exists
  17. * @param int $imgMaxWidth The maximum width of the thumbnail
  18. * @param int $imgMaxHeight The maximum height of the thumbnail
  19. * @param int $quality The quality of the image (100 is max and best)
  20. * @return bool
  21. * @access public
  22. */
  23. function CreateThumb ($imgPath, $newPath, $overwrite = false, $imgMaxWidth = 100, $imgMaxHeight = 100, $quality = 100)
  24. {
  25. /**
  26. * Basic checks
  27. * - Check if the maxwidth and maxheight are integers
  28. * - Check if the image exists to be thumbnailed
  29. * - Check if we need to overwrite or not, if necessary
  30. * - Check if we have gd enabled
  31. */
  32. if (!ctype_digit ($imgMaxWidth) || !ctype_digit ($imgMaxHeight))
  33. return false;
  34. if (!file_exists ($imgPath))
  35. return false;
  36. if (file_exists ($newPath) && ($overwrite == false || $overwrite == ''))
  37. return false;
  38. if (!extension_loaded ('gd'))
  39. return false;
  40.  
  41. /**
  42. * Store the information in an array for easy use
  43. */
  44. $thumb = array ();
  45. $thumb['imgOldPath'] = $imgPath;
  46. $thumb['imgNewPath'] = $newPath;
  47. $thumb['imgMaxWidth'] = $imgMaxWidth;
  48. $thumb['imgMaxHeight'] = $imgMaxHeight;
  49. $thumb['imgQuality'] = $quality;
  50. $thumb['imgExtension'] = strtolower (end(explode ('.', $thumb['imgOldPath'])));
  51.  
  52. list ($thumb['imgWidth'], $thumb['imgHeight']) = getimagesize ($thumb['imgOldPath']);
  53.  
  54. /**
  55. * Create the new height and width for the thumbnail image
  56. */
  57. $ratio = ($thumb['imgWidth'] > $thumb['imgMaxWidth']) ? (real)($thumb['imgMaxWidth'] / $thumb['imgWidth']) : 1;
  58. $width = ((int)($thumb['imgWidth'] * $ratio));
  59. $height = ((int)($thumb['imgHeight'] * $ratio));
  60.  
  61. $ratio = ($height > $height) ? (real)($thumb['imgMaxHeight'] / $height) : 1;
  62. $width = ((int)($width * $ratio));
  63. $height = ((int)($height * $ratio));
  64.  
  65. /**
  66. * Choose the functions to use
  67. */
  68. if ($thumb['imgExtension'] == 'jpg' || $thumb['imgExtension'] == 'jpeg' || $thumb['imgExtension'] == 'jpe' || $thumb['imgExtension'] == 'jfif')
  69. {
  70. $thumb['imgExtension'] = 'jpg';
  71. $func1 = 'imagecreatefromjpeg';
  72. $func2 = 'imagejpeg';
  73. }
  74. else if ($thumb['imgExtension'] == 'gif')
  75. {
  76. $thumb['imgExtension'] = 'gif';
  77. $func1 = 'imagecreatefromgif';
  78. $func2 = 'imagegif';
  79. }
  80. else if ($thumb['imgExtension'] == 'png')
  81. {
  82. $thumb['imgExtension'] = 'png';
  83. $func1 = 'imagecreatefrompng';
  84. $func2 = 'imagepng';
  85. }
  86. else
  87. return false;
  88.  
  89. /**
  90. * Check if we can manipulate this image
  91. */
  92. $check = defined ('IMG_' . strtoupper ($thumb['imgExtension'])) ? constant ('IMG_' . strtoupper ($thumb['imgExtension'])) : 0;
  93. if ($check == 0)
  94. return false;
  95.  
  96. /**
  97. * Create the thumbnail and stuff
  98. */
  99. $img_new = imagecreatetruecolor ($width, $height);
  100. $img_old = $func1 ($thumb['imgOldPath']);
  101. imagecopyresampled ($img_new, $img_old, 0, 0, 0, 0, $width, $height, $thumb['imgWidth'], $thumb['imgHeight']);
  102. $func2 ($img_new, $thumb['imgNewPath'], $thumb['imgQuality']);
  103. imagedestroy ($img_new);
  104. imagedestroy ($img_old);
  105.  
  106. /**
  107. * Return true
  108. */
  109. return true;
  110. }
  111.  
  112.  
  113. /**
  114. * Add a watermark to an image
  115. *
  116. * @param string $image The path of the image
  117. * @param string $watermark The path of the watermark image
  118. * @param bool $transparency The transparancy level of the image
  119. * @param int $wposition The amount of pixels the watermark should be placed from the left of the image
  120. * @param int $hposition The amount of pixels the watermark should be placed from the top of the image
  121. * @return bool
  122. * @access public
  123. */
  124. function AddWatermark ($image, $watermark, $transparency = 16, $wposition = 0, $hposition = 0)
  125. {
  126. /**
  127. * Check if the image and the watermark image exist
  128. */
  129. if (!file_exists ($image))
  130. return trigger_error ('Could not add watermark to image ' . $_IMG['IMGName'] . ', image file does not exist');
  131. if (!file_exists ($watermark))
  132. return trigger_error ('Could not add watermark to image ' . $_IMG['IMGName'] . ', watermark file does not exist');
  133.  
  134. /**
  135. * Store the information in an array for easy use
  136. */
  137. $_IMG = array ();
  138. $_IMG['transparency'] = $transparency;
  139. $_IMG['image'] = $image;
  140. $_IMG['watermark'] = $watermark;
  141. $_IMG['wposition'] = $wposition;
  142. $_IMG['hposition'] = $hposition;
  143.  
  144. $_EXT = array ();
  145. $_EXT['image'] = end (explode ('.', strtolower ($_IMG['image'])));
  146. $_EXT['watermark'] = end (explode ('.', strtolower ($_IMG['watermark'])));
  147.  
  148. /**
  149. * Make sure we use the correct things
  150. */
  151. switch ($_EXT['image'])
  152. {
  153. case 'jpg':
  154. $_IMG['iinfo'] = array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
  155. break;
  156. case 'gif':
  157. $_IMG['iinfo'] = array ('image/gif', 'imagecreatefromgif', 'imagegif');
  158. break;
  159. case 'png':
  160. $_IMG['iinfo'] = array ('image/png', 'imagecreatefrompng', 'imagepng');
  161. break;
  162. default:
  163. $_IMG['iinfo'] = array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
  164. break;
  165. }
  166. switch ($_EXT['watermark'])
  167. {
  168. case 'jpg':
  169. $_IMG['winfo'] = array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
  170. break;
  171. case 'gif':
  172. $_IMG['winfo'] = array ('image/gif', 'imagecreatefromgif', 'imagegif');
  173. break;
  174. case 'png':
  175. $_IMG['winfo'] = array ('image/png', 'imagecreatefrompng', 'imagepng');
  176. break;
  177. default:
  178. $_IMG['winfo'] = array ('image/jpeg', 'imagecreatefromjpeg', 'imagejpeg');
  179. break;
  180. }
  181.  
  182. /**
  183. * Create the images
  184. */
  185. $_IMG['image_mark'] = $_IMG['winfo'][1] ($_IMG['watermark']);
  186. $_IMG['image_image'] = $_IMG['iinfo'][1] ($_IMG['image']);
  187.  
  188. /**
  189. * Set the correct widht/height etc...
  190. */
  191. $_IMG['image_imagew'] = imagesx ($_IMG['image_image']);
  192. $_IMG['image_imageh'] = imagesy ($_IMG['image_image']);
  193. $_IMG['image_markw'] = imagesx ($_IMG['image_mark']);
  194. $_IMG['image_markh'] = imagesy ($_IMG['image_mark']);
  195.  
  196. /**
  197. * Make sure we use the correct position of the watermark
  198. */
  199. if ($_IMG['hposition'] == 0)
  200. $_IMG['hposition'] = ($_IMG['image_imageh'] - $_IMG['image_markh']);
  201.  
  202. if ($_IMG['wposition'] == 0)
  203. $_IMG['wposition'] = 5;
  204.  
  205. /**
  206. * Create the watermark
  207. */
  208. imagecopymerge ($_IMG['image_image'], $_IMG['image_mark'], $_IMG['wposition'], $_IMG['hposition'], 0, 0, $_IMG['image_markw'], $_IMG['image_markh'], $_IMG['transparency']);
  209.  
  210. /**
  211. * Set the correct content header
  212. */
  213. header ('Content-Type: ' . $_IMG['iinfo'][0]);
  214.  
  215. /**
  216. * Create the image
  217. */
  218. $_IMG['iinfo'][2] ($_IMG['image_image'], '', 100);
  219.  
  220. /**
  221. * Destroy the image
  222. */
  223. imagedestroy ($_IMG['image_image']);
  224.  
  225. /**
  226. * Return true
  227. */
  228. return true;
  229. }
  230. }
  231. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (7)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.038s