PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_easyblog/classes/easysimpleimage.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 440 lines | 319 code | 77 blank | 44 comment | 46 complexity | 9bf920ad0d6670f6928c2eca79bc5277 MD5 | raw file
  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. /*
  14. * File: SimpleImage.php
  15. * Author: Simon Jarvis
  16. * Copyright: 2006 Simon Jarvis
  17. * Date: 08/11/06
  18. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version 2
  23. * of the License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details:
  29. * http://www.gnu.org/licenses/gpl.html
  30. *
  31. */
  32. defined('_JEXEC') or die('Restricted access');
  33. class EasySimpleImage
  34. {
  35. var $image;
  36. var $image_type;
  37. var $src_filename;
  38. var $orientationFixed = false;
  39. function load($filename)
  40. {
  41. $image_info = getimagesize($filename);
  42. $this->image_type = $image_info[2];
  43. $this->src_filename = $filename;
  44. if( $this->image_type == IMAGETYPE_JPEG )
  45. {
  46. $this->image = imagecreatefromjpeg($filename);
  47. }
  48. elseif( $this->image_type == IMAGETYPE_GIF )
  49. {
  50. $this->image = imagecreatefromgif($filename);
  51. }
  52. elseif( $this->image_type == IMAGETYPE_PNG )
  53. {
  54. $this->image = imagecreatefrompng($filename);
  55. }
  56. $this->fixOrientation( $filename );
  57. }
  58. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null)
  59. {
  60. $contents = '';
  61. if( $image_type == IMAGETYPE_JPEG )
  62. {
  63. ob_start();
  64. imagejpeg( $this->image , null , $compression );
  65. $contents = ob_get_contents();
  66. ob_end_clean();
  67. }
  68. elseif( $image_type == IMAGETYPE_GIF )
  69. {
  70. ob_start();
  71. imagegif( $this->image , null );
  72. $contents = ob_get_contents();
  73. ob_end_clean();
  74. }
  75. elseif( $image_type == IMAGETYPE_PNG )
  76. {
  77. ob_start();
  78. imagepng( $this->image , null );
  79. $contents = ob_get_contents();
  80. ob_end_clean();
  81. }
  82. if( !$contents )
  83. {
  84. return false;
  85. }
  86. jimport( 'joomla.filesystem.file' );
  87. $status = @JFile::write( $filename , $contents );
  88. if( $permissions != null)
  89. {
  90. chmod($filename,$permissions);
  91. }
  92. return $status;
  93. }
  94. function output($image_type=IMAGETYPE_JPEG)
  95. {
  96. if( $image_type == IMAGETYPE_JPEG ) {
  97. imagejpeg($this->image);
  98. } elseif( $image_type == IMAGETYPE_GIF ) {
  99. imagegif($this->image);
  100. } elseif( $image_type == IMAGETYPE_PNG ) {
  101. imagepng($this->image);
  102. }
  103. }
  104. function getWidth()
  105. {
  106. return imagesx($this->image);
  107. }
  108. function getHeight()
  109. {
  110. return imagesy($this->image);
  111. }
  112. function resizeToHeight($height)
  113. {
  114. $ratio = $height / $this->getHeight();
  115. $width = $this->getWidth() * $ratio;
  116. $this->resize($width,$height);
  117. }
  118. function resizeToWidth($width)
  119. {
  120. $ratio = $width / $this->getWidth();
  121. $height = $this->getHeight() * $ratio;
  122. $this->resize($width,$height);
  123. }
  124. function scale($scale)
  125. {
  126. $width = $this->getWidth() * $scale/100;
  127. $height = $this->getheight() * $scale/100;
  128. $this->resize($width,$height);
  129. }
  130. function resize($width, $height)
  131. {
  132. if( $this->image_type == IMAGETYPE_JPEG )
  133. {
  134. $new_image = imagecreatetruecolor($width, $height);
  135. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  136. }
  137. elseif( $this->image_type == IMAGETYPE_GIF )
  138. {
  139. $new_image = imagecreatetruecolor($width, $height);
  140. $transparent = imagecolortransparent($this->image);
  141. imagepalettecopy( $new_image , $this->image );
  142. imagefill($new_image, 0, 0, $transparent);
  143. imagecolortransparent($new_image, $transparent);
  144. imagetruecolortopalette($new_image, true, 256);
  145. imagecopyresized($new_image, $this->image, 0, 0, 0, 0, $width , $height , $this->getWidth() , $this->getHeight() );
  146. }
  147. elseif( $this->image_type == IMAGETYPE_PNG )
  148. {
  149. $new_image = imagecreatetruecolor( $width , $height );
  150. $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  151. imagealphablending($new_image , false);
  152. imagesavealpha($new_image,true);
  153. imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
  154. imagecopyresampled($new_image , $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight() );
  155. }
  156. $this->image = $new_image;
  157. }
  158. function resizeWithin($maxWidth, $maxHeight)
  159. {
  160. //@TODO: resizeWithin should also scale smaller images, with a boolean flag to disable it.
  161. $sourceWidth = $this->getWidth();
  162. $sourceHeight = $this->getHeight();
  163. $targetWidth = $sourceWidth;
  164. $targetHeight = $sourceHeight;
  165. if (!empty($maxWidth) && $targetWidth > $maxWidth)
  166. {
  167. $ratio = $maxWidth / $sourceWidth;
  168. $targetWidth = $sourceWidth * $ratio;
  169. $targetHeight = $sourceHeight * $ratio;
  170. }
  171. if (!empty($maxHeight) && $targetHeight > $maxHeight)
  172. {
  173. $ratio = $maxHeight / $sourceHeight;
  174. $targetWidth = $sourceWidth * $ratio;
  175. $targetHeight = $sourceHeight * $ratio;
  176. }
  177. $this->resize($targetWidth, $targetHeight);
  178. }
  179. function resizeToFit($maxWidth, $maxHeight)
  180. {
  181. $sourceWidth = $this->getWidth();
  182. $sourceHeight = $this->getHeight();
  183. $targetWidth = $sourceWidth;
  184. $targetHeight = $sourceHeight;
  185. $newX = 0;
  186. $newY = 0;
  187. $oriX = 0;
  188. $oriY = 0;
  189. $newWidth = $maxWidth;
  190. $newHeight = $maxHeight;
  191. if (!empty($maxWidth) && $targetWidth > $maxWidth)
  192. {
  193. $ratio = $maxWidth / $sourceWidth;
  194. $targetWidth = $sourceWidth * $ratio;
  195. $targetHeight = $sourceHeight * $ratio;
  196. }
  197. if (!empty($maxHeight) && $targetHeight > $maxHeight)
  198. {
  199. $ratio = $maxHeight / $sourceHeight;
  200. $targetWidth = $sourceWidth * $ratio;
  201. $targetHeight = $sourceHeight * $ratio;
  202. }
  203. if( $newWidth > $targetWidth )
  204. {
  205. $newX = intval( ($newWidth - $targetWidth) / 2 );
  206. }
  207. if( $newHeight > $targetHeight )
  208. {
  209. $newY = intval( ($newHeight - $targetHeight) / 2 );
  210. }
  211. //rebuilding new image
  212. $new_image = imagecreatetruecolor($newWidth, $newHeight);
  213. if( $this->image_type == IMAGETYPE_JPEG )
  214. {
  215. imagecopyresampled($new_image , $this->image, $newX, $newY, $oriX, $oriY, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
  216. }
  217. elseif( $this->image_type == IMAGETYPE_GIF )
  218. {
  219. $transparent = imagecolortransparent($this->image);
  220. imagepalettecopy($this->image, $new_image);
  221. imagefill($new_image, 0, 0, $transparent);
  222. imagecolortransparent($new_image, $transparent);
  223. imagetruecolortopalette($new_image, true, 256);
  224. imagecopyresized($new_image, $this->image, $newX, $newY, $oriX, $oriY, $targetWidth , $targetHeight , $sourceWidth , $sourceHeight );
  225. }
  226. elseif( $this->image_type == IMAGETYPE_PNG )
  227. {
  228. $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  229. imagealphablending($new_image , false);
  230. imagesavealpha($new_image,true);
  231. imagefilledrectangle($new_image, 0, 0, $newWidth, $newHeight, $transparent);
  232. imagecopyresampled($new_image , $this->image, $newX, $newY, $oriX, $oriY, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
  233. }
  234. $this->image = $new_image;
  235. }
  236. function fixOrientation( $filename )
  237. {
  238. if( !function_exists( 'exif_read_data' ) )
  239. {
  240. return false;
  241. }
  242. // currently this only support jpeg
  243. // see http://php.net/manual/en/function.imagerotate.php
  244. if( $this->image_type == IMAGETYPE_JPEG )
  245. {
  246. $exif = exif_read_data( $filename );
  247. $orientation = ( isset( $exif['Orientation'] ) && !empty( $exif['Orientation'] ) ) ? $exif['Orientation'] : 1;
  248. switch($orientation)
  249. {
  250. case 1: // nothing
  251. break;
  252. case 2: // horizontal flip
  253. //$image->flipImage($public,1);
  254. break;
  255. case 3: // 180 rotate left
  256. $this->_rotateImage(180);
  257. $this->orientationFixed = true;
  258. break;
  259. case 4: // vertical flip
  260. //$image->flipImage($public,2);
  261. break;
  262. case 5: // vertical flip + 90 rotate right
  263. //$image->flipImage($public, 2);
  264. //$this->_rotateImage(-90);
  265. break;
  266. case 6: // 90 rotate right
  267. $this->_rotateImage(-90);
  268. $this->orientationFixed = true;
  269. break;
  270. case 7: // horizontal flip + 90 rotate right
  271. //$image->flipImage($public,1);
  272. //$this->_rotateImage(-90);
  273. break;
  274. case 8: // 90 rotate left
  275. $this->_rotateImage(90);
  276. $this->orientationFixed = true;
  277. break;
  278. }
  279. }
  280. }
  281. function _rotateImage( $degree )
  282. {
  283. $this->image = imagerotate($this->image, $degree, 0);
  284. }
  285. // TODO: Ability to expand original image source if dimension is smaller.
  286. function resizeToFill($maxWidth, $maxHeight) {
  287. $sourceWidth = $this->getWidth();
  288. $sourceHeight = $this->getHeight();
  289. $targetWidth = $sourceWidth;
  290. $targetHeight = $sourceHeight;
  291. $ratio = $maxWidth / $sourceWidth;
  292. $targetWidth = $sourceWidth * $ratio;
  293. $targetHeight = $sourceHeight * $ratio;
  294. if ($targetHeight < $maxHeight) {
  295. $ratio = $maxHeight / $sourceHeight;
  296. $targetWidth = $sourceWidth * $ratio;
  297. $targetHeight = $sourceHeight * $ratio;
  298. }
  299. $targetTop = $maxHeight - $targetHeight;
  300. $targetLeft = $maxWidth - $targetWidth;
  301. $targetWidth = ($targetWidth + $targetLeft) / $ratio;
  302. $targetHeight = ($targetHeight + $targetTop) / $ratio;
  303. $targetTop = abs($targetTop / 2) / $ratio;
  304. $targetLeft = abs($targetLeft / 2) / $ratio;
  305. //rebuilding new image
  306. $new_image = imagecreatetruecolor($maxWidth, $maxHeight);
  307. if( $this->image_type == IMAGETYPE_JPEG ) {
  308. imagecopyresampled($new_image, $this->image, 0, 0, $targetLeft, $targetTop, $maxWidth, $maxHeight, $targetWidth, $targetHeight);
  309. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  310. $transparent = imagecolortransparent($this->image);
  311. imagepalettecopy($this->image, $new_image);
  312. imagefill($new_image, 0, 0, $transparent);
  313. imagecolortransparent($new_image, $transparent);
  314. imagetruecolortopalette($new_image, true, 256);
  315. imagecopyresized($new_image, $this->image, 0, 0, $targetLeft, $targetTop, $maxWidth , $maxHeight , $targetWidth , $targetHeight );
  316. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  317. $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  318. imagealphablending($new_image , false);
  319. imagesavealpha($new_image,true);
  320. imagefilledrectangle($new_image, 0, 0, $maxWidth, $maxHeight, $transparent);
  321. imagecopyresampled($new_image , $this->image, 0, 0, $targetLeft, $targetTop, $maxWidth, $maxHeight, $targetWidth, $targetHeight);
  322. }
  323. $this->image = $new_image;
  324. }
  325. // TODO: Ability to crop the image from center of the source image.
  326. function crop($width, $height)
  327. {
  328. $oriHeight = $this->getHeight();
  329. $oriWidth = $this->getWidth();
  330. $newHeight = $height;
  331. $newWidth = $width;
  332. $newX = 0;
  333. $newY = 0;
  334. $oriX = 0;
  335. $oriY = 0;
  336. $oriX = intval( ( $oriWidth - $newWidth ) / 2 );
  337. $oriY = intval( ( $oriHeight - $newHeight ) / 2 );
  338. //rebuilding new image
  339. $new_image = imagecreatetruecolor($newWidth, $newHeight);
  340. if( $this->image_type == IMAGETYPE_JPEG )
  341. {
  342. imagecopyresampled($new_image , $this->image, $newX, $newY, $oriX, $oriY, $newWidth, $newHeight, $newWidth, $newHeight);
  343. }
  344. elseif( $this->image_type == IMAGETYPE_GIF )
  345. {
  346. $transparent = imagecolortransparent($this->image);
  347. imagepalettecopy($this->image, $new_image);
  348. imagefill($new_image, 0, 0, $transparent);
  349. imagecolortransparent($new_image, $transparent);
  350. imagetruecolortopalette($new_image, true, 256);
  351. imagecopyresized($new_image, $this->image, $newX, $newY, $oriX, $oriY, $newWidth , $newHeight , $newWidth , $newHeight );
  352. }
  353. elseif( $this->image_type == IMAGETYPE_PNG )
  354. {
  355. $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  356. imagealphablending($new_image , false);
  357. imagesavealpha($new_image,true);
  358. imagefilledrectangle($new_image, 0, 0, $newWidth, $newHeight, $transparent);
  359. imagecopyresampled($new_image , $this->image, $newX, $newY, $oriX, $oriY, $newWidth, $newHeight, $newWidth, $newHeight);
  360. }
  361. $this->image = $new_image;
  362. }
  363. }