/plugins/media/vendors/media/adapter/gd.php

https://github.com/redlion09/ems-1 · PHP · 306 lines · 248 code · 35 blank · 23 comment · 39 complexity · b1b32fc5d5af56fdb21aa55028c4bd19 MD5 · raw file

  1. <?php
  2. /**
  3. * Gd Media Adapter File
  4. *
  5. * Copyright (c) 2007-2010 David Persson
  6. *
  7. * Distributed under the terms of the MIT License.
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * PHP version 5
  11. * CakePHP version 1.2
  12. *
  13. * @package media
  14. * @subpackage media.libs.media.adapter
  15. * @copyright 2007-2010 David Persson <davidpersson@gmx.de>
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. * @link http://github.com/davidpersson/media
  18. */
  19. /**
  20. * Gd Media Adapter Class
  21. *
  22. * @package media
  23. * @subpackage media.libs.media.adapter
  24. */
  25. class GdMediaAdapter extends MediaAdapter {
  26. var $require = array(
  27. 'mimeTypes' => array('image/gd'), /* Gets dynamically set in constructor */
  28. 'extensions' => array('gd'),
  29. );
  30. var $_Image;
  31. var $_formatMap = array(
  32. 'image/jpeg' => 'jpeg',
  33. 'image/gif' => 'gif',
  34. 'image/png' => 'png',
  35. 'image/gd' => 'gd',
  36. 'image/vnd.wap.wbmp' => 'wbmp',
  37. 'image/xbm' => 'xbm',
  38. );
  39. var $_format;
  40. var $_compression;
  41. var $_pngFilter;
  42. function compatible($Media) {
  43. $types = imageTypes();
  44. if ($types & IMG_GIF) {
  45. $this->require['mimeTypes'][] = 'image/gif';
  46. }
  47. if ($types & IMG_JPG) {
  48. $this->require['mimeTypes'][] = 'image/jpeg';
  49. }
  50. if ($types & IMG_PNG) {
  51. $this->require['mimeTypes'][] = 'image/png';
  52. }
  53. if ($types & IMG_WBMP) {
  54. $this->require['mimeTypes'][] = 'image/wbmp';
  55. }
  56. if ($types & IMG_XPM) {
  57. $this->require['mimeTypes'][] = 'image/xpm';
  58. }
  59. return parent::compatible($Media);
  60. }
  61. function initialize($Media) {
  62. $this->_format = $this->_formatMap[$Media->mimeType];
  63. if (isset($Media->resources['gd'])) {
  64. return true;
  65. }
  66. if (!isset($Media->file)) {
  67. return false;
  68. }
  69. $Media->resources['gd'] = call_user_func_array(
  70. 'imageCreateFrom' . $this->_format,
  71. array($Media->file)
  72. );
  73. if (!$this->_isResource($Media->resources['gd'])) {
  74. return false;
  75. }
  76. if (imageIsTrueColor($Media->resources['gd'])) {
  77. imageAlphaBlending($Media->resources['gd'], false);
  78. imageSaveAlpha($Media->resources['gd'], true);
  79. }
  80. return true;
  81. }
  82. function toString($Media) {
  83. ob_start();
  84. $this->store($Media, null);
  85. return ob_get_clean();
  86. }
  87. function store($Media, $file) {
  88. $args = array($Media->resources['gd'], $file);
  89. switch ($Media->mimeType) {
  90. case 'image/jpeg':
  91. if (isset($this->_compression)) {
  92. $args[] = $this->_compression;
  93. }
  94. break;
  95. case 'image/png':
  96. if (isset($this->_compression)) {
  97. $args[] = $this->_compression;
  98. if (isset($this->_pngFilter)) {
  99. $args[] = $this->_pngFilter;
  100. }
  101. }
  102. break;
  103. }
  104. return call_user_func_array('image' . $this->_format, $args);
  105. }
  106. function convert($Media, $mimeType) {
  107. if (in_array($mimeType, $this->require['mimeTypes'])) {
  108. return $this->_format = $this->_formatMap[$mimeType];
  109. }
  110. return false;
  111. }
  112. function compress($Media, $value) {
  113. switch ($Media->mimeType) {
  114. case 'image/jpeg':
  115. $this->_compression = (integer)(100 - ($value * 10));
  116. break;
  117. case 'image/png':
  118. if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
  119. $this->_compression = (integer)$value;
  120. }
  121. if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
  122. $filter = ($value * 10) % 10;
  123. $map = array(
  124. 0 => PNG_FILTER_NONE,
  125. 1 => PNG_FILTER_SUB,
  126. 2 => PNG_FILTER_UP,
  127. 3 => PNG_FILTER_AVG,
  128. 4 => PNG_FILTER_PAETH,
  129. );
  130. if (array_key_exists($filter, $map)) {
  131. $this->_pngFilter = $map[$filter];
  132. } elseif ($filter == 5) {
  133. if (intval($value) <= 5 && imageIsTrueColor($Media->resources['gd'])) {
  134. $this->_pngFilter = PNG_ALL_FILTERS;
  135. } else {
  136. $this->_pngFilter = PNG_NO_FILTER;
  137. }
  138. } else {
  139. $this->_pngFilter = PNG_ALL_FILTERS;
  140. }
  141. }
  142. break;
  143. }
  144. return true;
  145. }
  146. function crop($Media, $left, $top, $width, $height) {
  147. $left = (integer)$left;
  148. $top = (integer)$top;
  149. $width = (integer)$width;
  150. $height = (integer)$height;
  151. $Image = imageCreateTrueColor($width, $height);
  152. $this->_adjustTransparency($Media->resources['gd'], $Image);
  153. if ($this->_isTransparent($Media->resources['gd'])) {
  154. imageCopyResized(
  155. $Image,
  156. $Media->resources['gd'],
  157. 0, 0,
  158. $left, $top,
  159. $width, $height,
  160. $width, $height
  161. );
  162. } else {
  163. imageCopyResampled(
  164. $Image,
  165. $Media->resources['gd'],
  166. 0, 0,
  167. $left, $top,
  168. $width, $height,
  169. $width, $height
  170. );
  171. }
  172. if ($this->_isResource($Image)) {
  173. $Media->resources['gd'] = $Image;
  174. return true;
  175. }
  176. return false;
  177. }
  178. function resize($Media, $width, $height) {
  179. $width = (integer)$width;
  180. $height = (integer)$height;
  181. $Image = imageCreateTrueColor($width, $height);
  182. $this->_adjustTransparency($Media->resources['gd'], $Image);
  183. if ($this->_isTransparent($Media->resources['gd'])) {
  184. imageCopyResized(
  185. $Image,
  186. $Media->resources['gd'],
  187. 0, 0,
  188. 0, 0,
  189. $width, $height,
  190. $this->width($Media), $this->height($Media)
  191. );
  192. } else {
  193. imageCopyResampled(
  194. $Image,
  195. $Media->resources['gd'],
  196. 0, 0,
  197. 0, 0,
  198. $width, $height,
  199. $this->width($Media), $this->height($Media)
  200. );
  201. }
  202. if ($this->_isResource($Image)) {
  203. $Media->resources['gd'] = $Image;
  204. return true;
  205. }
  206. return false;
  207. }
  208. function cropAndResize($Media, $cropLeft, $cropTop, $cropWidth, $cropHeight, $resizeWidth, $resizeHeight) {
  209. $cropLeft = (integer)$cropLeft;
  210. $cropTop = (integer)$cropTop;
  211. $cropWidth = (integer)$cropWidth;
  212. $cropHeight = (integer)$cropHeight;
  213. $resizeWidth = (integer)$resizeWidth;
  214. $resizeHeight = (integer)$resizeHeight;
  215. $Image = imageCreateTrueColor($resizeWidth, $resizeHeight);
  216. $this->_adjustTransparency($Media->resources['gd'], $Image);
  217. if ($this->_isTransparent($Media->resources['gd'])) {
  218. imageCopyResized(
  219. $Image,
  220. $Media->resources['gd'],
  221. 0, 0,
  222. $cropLeft, $cropTop,
  223. $resizeWidth, $resizeHeight,
  224. $cropWidth, $cropHeight
  225. );
  226. } else {
  227. imageCopyResampled(
  228. $Image,
  229. $Media->resources['gd'],
  230. 0, 0,
  231. $cropLeft, $cropTop,
  232. $resizeWidth, $resizeHeight,
  233. $cropWidth, $cropHeight
  234. );
  235. }
  236. if ($this->_isResource($Image)) {
  237. $Media->resources['gd'] = $Image;
  238. return true;
  239. }
  240. return false;
  241. }
  242. function width($Media) {
  243. return imageSX($Media->resources['gd']);
  244. }
  245. function height($Media) {
  246. return imageSY($Media->resources['gd']);
  247. }
  248. function _isResource($Image) {
  249. return is_resource($Image) && get_resource_type($Image) == 'gd';
  250. }
  251. function _isTransparent($Image) {
  252. return imageColorTransparent($Image) >= 0;
  253. }
  254. function _adjustTransparency(&$Source, &$Destination) {
  255. if ($this->_isTransparent($Source)) {
  256. $rgba = imageColorsForIndex($Source, imageColorTransparent($Source));
  257. $color = imageColorAllocate($Destination, $rgba['red'], $rgba['green'], $rgba['blue']);
  258. imageColorTransparent($Destination, $color);
  259. imageFill($Destination, 0, 0, $color);
  260. } else {
  261. if ($this->_format == 'png') {
  262. imageAlphaBlending($Destination, false);
  263. imageSaveAlpha($Destination, true);
  264. } elseif ($this->_format != 'gif') {
  265. $white = imageColorAllocate($Destination, 255, 255, 255);
  266. imageFill($Destination, 0, 0 , $white);
  267. }
  268. }
  269. }
  270. }
  271. ?>