image.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 2011 Thomas Tanghus <thomas@tanghus.net>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. //From user comments at http://dk2.php.net/manual/en/function.exif-imagetype.php
  23. if ( ! function_exists( 'exif_imagetype' ) ) {
  24. function exif_imagetype ( $filename ) {
  25. if ( ( list($width, $height, $type, $attr) = getimagesize( $filename ) ) !== false ) {
  26. return $type;
  27. }
  28. return false;
  29. }
  30. }
  31. function ellipsis($str, $maxlen) {
  32. if (strlen($str) > $maxlen) {
  33. $characters = floor($maxlen / 2);
  34. return substr($str, 0, $characters) . '...' . substr($str, -1 * $characters);
  35. }
  36. return $str;
  37. }
  38. /**
  39. * Class for basic image manipulation
  40. *
  41. */
  42. class OC_Image {
  43. protected $resource = false; // tmp resource.
  44. protected $imagetype = IMAGETYPE_PNG; // Default to png if file type isn't evident.
  45. protected $filepath = null;
  46. /**
  47. * @brief Get mime type for an image file.
  48. * @param $filepath The path to a local image file.
  49. * @returns string The mime type if the it could be determined, otherwise an empty string.
  50. */
  51. static public function getMimeTypeForFile($filepath) {
  52. $imagetype = exif_imagetype($filepath);
  53. return $imagetype ? image_type_to_mime_type($imagetype) : '';
  54. }
  55. /**
  56. * @brief Constructor.
  57. * @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function.
  58. * @returns bool False on error
  59. */
  60. public function __construct($imageref = null) {
  61. //OC_Log::write('core',__METHOD__.'(): start', OC_Log::DEBUG);
  62. if(!extension_loaded('gd') || !function_exists('gd_info')) {
  63. //if(!function_exists('imagecreatefromjpeg')) {
  64. OC_Log::write('core',__METHOD__.'(): GD module not installed', OC_Log::ERROR);
  65. return false;
  66. }
  67. if(!is_null($imageref)) {
  68. $this->load($imageref);
  69. }
  70. }
  71. /**
  72. * @brief Determine whether the object contains an image resource.
  73. * @returns bool
  74. */
  75. public function valid() { // apparently you can't name a method 'empty'...
  76. return is_resource($this->resource);
  77. }
  78. /**
  79. * @brief Returns the MIME type of the image or an empty string if no image is loaded.
  80. * @returns int
  81. */
  82. public function mimeType() {
  83. return $this->valid() ? image_type_to_mime_type($this->imagetype) : '';
  84. }
  85. /**
  86. * @brief Returns the width of the image or -1 if no image is loaded.
  87. * @returns int
  88. */
  89. public function width() {
  90. return $this->valid() ? imagesx($this->resource) : -1;
  91. }
  92. /**
  93. * @brief Returns the height of the image or -1 if no image is loaded.
  94. * @returns int
  95. */
  96. public function height() {
  97. return $this->valid() ? imagesy($this->resource) : -1;
  98. }
  99. /**
  100. * @brief Outputs the image.
  101. * @returns bool
  102. */
  103. public function show() {
  104. header('Content-Type: '.$this->mimeType());
  105. return $this->_output();
  106. }
  107. /**
  108. * @brief Saves the image.
  109. * @returns bool
  110. */
  111. public function save($filepath=null) {
  112. if($filepath === null && $this->filepath === null) {
  113. OC_Log::write('core',__METHOD__.'(): called with no path.', OC_Log::ERROR);
  114. return false;
  115. } elseif($filepath === null && $this->filepath !== null) {
  116. $filepath = $this->filepath;
  117. }
  118. return $this->_output($filepath);
  119. }
  120. /**
  121. * @brief Outputs/saves the image.
  122. */
  123. private function _output($filepath=null) {
  124. if($filepath) {
  125. if(!is_writable(dirname($filepath))) {
  126. OC_Log::write('core',__METHOD__.'(): Directory \''.dirname($filepath).'\' is not writable.', OC_Log::ERROR);
  127. return false;
  128. } elseif(is_writable(dirname($filepath)) && file_exists($filepath) && !is_writable($filepath)) {
  129. OC_Log::write('core',__METHOD__.'(): File \''.$filepath.'\' is not writable.', OC_Log::ERROR);
  130. return false;
  131. }
  132. }
  133. if (!$this->valid()) {
  134. return false;
  135. }
  136. $retval = false;
  137. switch($this->imagetype) {
  138. case IMAGETYPE_GIF:
  139. $retval = imagegif($this->resource, $filepath);
  140. break;
  141. case IMAGETYPE_JPEG:
  142. $retval = imagejpeg($this->resource, $filepath);
  143. break;
  144. case IMAGETYPE_PNG:
  145. $retval = imagepng($this->resource, $filepath);
  146. break;
  147. case IMAGETYPE_XBM:
  148. $retval = imagexbm($this->resource, $filepath);
  149. break;
  150. case IMAGETYPE_WBMP:
  151. case IMAGETYPE_BMP:
  152. $retval = imagewbmp($this->resource, $filepath);
  153. break;
  154. default:
  155. $retval = imagepng($this->resource, $filepath);
  156. }
  157. return $retval;
  158. }
  159. /**
  160. * @brief Prints the image when called as $image().
  161. */
  162. public function __invoke() {
  163. return $this->show();
  164. }
  165. /**
  166. * @returns Returns the image resource in any.
  167. */
  168. public function resource() {
  169. return $this->resource;
  170. }
  171. /**
  172. * @returns Returns a base64 encoded string suitable for embedding in a VCard.
  173. */
  174. function __toString() {
  175. ob_start();
  176. $res = imagepng($this->resource);
  177. if (!$res) {
  178. OC_Log::write('core','OC_Image->__toString. Error writing image',OC_Log::ERROR);
  179. }
  180. return base64_encode(ob_get_clean());
  181. }
  182. /**
  183. * (I'm open for suggestions on better method name ;)
  184. * @brief Fixes orientation based on EXIF data.
  185. * @returns bool.
  186. */
  187. public function fixOrientation() {
  188. if(!is_callable('exif_read_data')){
  189. OC_Log::write('core','OC_Image->fixOrientation() Exif module not enabled.', OC_Log::DEBUG);
  190. return false;
  191. }
  192. if(!$this->valid()) {
  193. OC_Log::write('core','OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG);
  194. return false;
  195. }
  196. if(is_null($this->filepath) || !is_readable($this->filepath)) {
  197. OC_Log::write('core','OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
  198. return false;
  199. }
  200. $exif = @exif_read_data($this->filepath, 'IFD0');
  201. if(!$exif) {
  202. return false;
  203. }
  204. if(!isset($exif['Orientation'])) {
  205. return true; // Nothing to fix
  206. }
  207. $o = $exif['Orientation'];
  208. OC_Log::write('core','OC_Image->fixOrientation() Orientation: '.$o, OC_Log::DEBUG);
  209. $rotate = 0;
  210. $flip = false;
  211. switch($o) {
  212. case 1:
  213. $rotate = 0;
  214. $flip = false;
  215. break;
  216. case 2: // Not tested
  217. $rotate = 0;
  218. $flip = true;
  219. break;
  220. case 3:
  221. $rotate = 180;
  222. $flip = false;
  223. break;
  224. case 4: // Not tested
  225. $rotate = 180;
  226. $flip = true;
  227. break;
  228. case 5: // Not tested
  229. $rotate = 90;
  230. $flip = true;
  231. break;
  232. case 6:
  233. //$rotate = 90;
  234. $rotate = 270;
  235. $flip = false;
  236. break;
  237. case 7: // Not tested
  238. $rotate = 270;
  239. $flip = true;
  240. break;
  241. case 8:
  242. $rotate = 90;
  243. $flip = false;
  244. break;
  245. }
  246. if($rotate) {
  247. $res = imagerotate($this->resource, $rotate, -1);
  248. if($res) {
  249. if(imagealphablending($res, true)) {
  250. if(imagesavealpha($res, true)) {
  251. imagedestroy($this->resource);
  252. $this->resource = $res;
  253. return true;
  254. } else {
  255. OC_Log::write('core','OC_Image->fixOrientation() Error during alphasaving.', OC_Log::DEBUG);
  256. return false;
  257. }
  258. } else {
  259. OC_Log::write('core','OC_Image->fixOrientation() Error during alphablending.', OC_Log::DEBUG);
  260. return false;
  261. }
  262. } else {
  263. OC_Log::write('core','OC_Image->fixOrientation() Error during oriention fixing.', OC_Log::DEBUG);
  264. return false;
  265. }
  266. }
  267. }
  268. /**
  269. * @brief Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
  270. * @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle ).
  271. * @returns An image resource or false on error
  272. */
  273. public function load($imageref) {
  274. if(is_resource($imageref)) {
  275. if(get_resource_type($imageref) == 'gd') {
  276. $this->resource = $res;
  277. return $this->resource;
  278. } elseif(in_array(get_resource_type($imageref), array('file','stream'))) {
  279. return $this->loadFromFileHandle($imageref);
  280. }
  281. } elseif($this->loadFromFile($imageref) !== false) {
  282. return $this->resource;
  283. } elseif($this->loadFromBase64($imageref) !== false) {
  284. return $this->resource;
  285. } elseif($this->loadFromData($imageref) !== false) {
  286. return $this->resource;
  287. } else {
  288. OC_Log::write('core',__METHOD__.'(): couldn\'t load anything. Giving up!', OC_Log::DEBUG);
  289. return false;
  290. }
  291. }
  292. /**
  293. * @brief Loads an image from an open file handle.
  294. * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
  295. * @param $handle
  296. * @returns An image resource or false on error
  297. */
  298. public function loadFromFileHandle($handle) {
  299. OC_Log::write('core',__METHOD__.'(): Trying', OC_Log::DEBUG);
  300. $contents = stream_get_contents($handle);
  301. if($this->loadFromData($contents)) {
  302. return $this->resource;
  303. }
  304. }
  305. /**
  306. * @brief Loads an image from a local file.
  307. * @param $imageref The path to a local file.
  308. * @returns An image resource or false on error
  309. */
  310. public function loadFromFile($imagepath=false) {
  311. if(!is_file($imagepath) || !file_exists($imagepath) || !is_readable($imagepath)) {
  312. // Debug output disabled because this method is tried before loadFromBase64?
  313. OC_Log::write('core','OC_Image->loadFromFile, couldn\'t load: '.ellipsis($imagepath, 50), OC_Log::DEBUG);
  314. return false;
  315. }
  316. $itype = exif_imagetype($imagepath);
  317. switch($itype) {
  318. case IMAGETYPE_GIF:
  319. if (imagetypes() & IMG_GIF) {
  320. $this->resource = imagecreatefromgif($imagepath);
  321. } else {
  322. OC_Log::write('core','OC_Image->loadFromFile, GIF images not supported: '.$imagepath, OC_Log::DEBUG);
  323. }
  324. break;
  325. case IMAGETYPE_JPEG:
  326. if (imagetypes() & IMG_JPG) {
  327. $this->resource = imagecreatefromjpeg($imagepath);
  328. } else {
  329. OC_Log::write('core','OC_Image->loadFromFile, JPG images not supported: '.$imagepath, OC_Log::DEBUG);
  330. }
  331. break;
  332. case IMAGETYPE_PNG:
  333. if (imagetypes() & IMG_PNG) {
  334. $this->resource = imagecreatefrompng($imagepath);
  335. } else {
  336. OC_Log::write('core','OC_Image->loadFromFile, PNG images not supported: '.$imagepath, OC_Log::DEBUG);
  337. }
  338. break;
  339. case IMAGETYPE_XBM:
  340. if (imagetypes() & IMG_XPM) {
  341. $this->resource = imagecreatefromxbm($imagepath);
  342. } else {
  343. OC_Log::write('core','OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagepath, OC_Log::DEBUG);
  344. }
  345. break;
  346. case IMAGETYPE_WBMP:
  347. case IMAGETYPE_BMP:
  348. if (imagetypes() & IMG_WBMP) {
  349. $this->resource = imagecreatefromwbmp($imagepath);
  350. } else {
  351. OC_Log::write('core','OC_Image->loadFromFile, (W)BMP images not supported: '.$imagepath, OC_Log::DEBUG);
  352. }
  353. break;
  354. /*
  355. case IMAGETYPE_TIFF_II: // (intel byte order)
  356. break;
  357. case IMAGETYPE_TIFF_MM: // (motorola byte order)
  358. break;
  359. case IMAGETYPE_JPC:
  360. break;
  361. case IMAGETYPE_JP2:
  362. break;
  363. case IMAGETYPE_JPX:
  364. break;
  365. case IMAGETYPE_JB2:
  366. break;
  367. case IMAGETYPE_SWC:
  368. break;
  369. case IMAGETYPE_IFF:
  370. break;
  371. case IMAGETYPE_ICO:
  372. break;
  373. case IMAGETYPE_SWF:
  374. break;
  375. case IMAGETYPE_PSD:
  376. break;
  377. */
  378. default:
  379. $this->resource = imagecreatefromstring(file_get_contents($imagepath));
  380. $itype = IMAGETYPE_PNG;
  381. OC_Log::write('core','OC_Image->loadFromFile, Default', OC_Log::DEBUG);
  382. break;
  383. }
  384. if($this->valid()) {
  385. $this->imagetype = $itype;
  386. $this->filepath = $imagepath;
  387. }
  388. return $this->resource;
  389. }
  390. /**
  391. * @brief Loads an image from a string of data.
  392. * @param $str A string of image data as read from a file.
  393. * @returns An image resource or false on error
  394. */
  395. public function loadFromData($str) {
  396. if(is_resource($str)) {
  397. return false;
  398. }
  399. $this->resource = imagecreatefromstring($str);
  400. if(!$this->resource) {
  401. OC_Log::write('core','OC_Image->loadFromData, couldn\'t load', OC_Log::DEBUG);
  402. return false;
  403. }
  404. return $this->resource;
  405. }
  406. /**
  407. * @brief Loads an image from a base64 encoded string.
  408. * @param $str A string base64 encoded string of image data.
  409. * @returns An image resource or false on error
  410. */
  411. public function loadFromBase64($str) {
  412. if(!is_string($str)) {
  413. return false;
  414. }
  415. $data = base64_decode($str);
  416. if($data) { // try to load from string data
  417. $this->resource = imagecreatefromstring($data);
  418. if(!$this->resource) {
  419. OC_Log::write('core','OC_Image->loadFromBase64, couldn\'t load', OC_Log::DEBUG);
  420. return false;
  421. }
  422. return $this->resource;
  423. } else {
  424. return false;
  425. }
  426. }
  427. /**
  428. * @brief Resizes the image preserving ratio.
  429. * @param $maxsize The maximum size of either the width or height.
  430. * @returns bool
  431. */
  432. public function resize($maxsize) {
  433. if(!$this->valid()) {
  434. OC_Log::write('core',__METHOD__.'(): No image loaded', OC_Log::ERROR);
  435. return false;
  436. }
  437. $width_orig=imageSX($this->resource);
  438. $height_orig=imageSY($this->resource);
  439. $ratio_orig = $width_orig/$height_orig;
  440. if ($ratio_orig > 1) {
  441. $new_height = round($maxsize/$ratio_orig);
  442. $new_width = $maxsize;
  443. } else {
  444. $new_width = round($maxsize*$ratio_orig);
  445. $new_height = $maxsize;
  446. }
  447. $process = imagecreatetruecolor(round($new_width), round($new_height));
  448. if ($process == false) {
  449. OC_Log::write('core',__METHOD__.'(): Error creating true color image',OC_Log::ERROR);
  450. imagedestroy($process);
  451. return false;
  452. }
  453. imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
  454. if ($process == false) {
  455. OC_Log::write('core',__METHOD__.'(): Error resampling process image '.$new_width.'x'.$new_height,OC_Log::ERROR);
  456. imagedestroy($process);
  457. return false;
  458. }
  459. imagedestroy($this->resource);
  460. $this->resource = $process;
  461. return true;
  462. }
  463. /**
  464. * @brief Crops the image to the middle square. If the image is already square it just returns.
  465. * @param int maximum size for the result (optional)
  466. * @returns bool for success or failure
  467. */
  468. public function centerCrop($size=0) {
  469. if(!$this->valid()) {
  470. OC_Log::write('core','OC_Image->centerCrop, No image loaded', OC_Log::ERROR);
  471. return false;
  472. }
  473. $width_orig=imageSX($this->resource);
  474. $height_orig=imageSY($this->resource);
  475. if($width_orig === $height_orig and $size==0) {
  476. return true;
  477. }
  478. $ratio_orig = $width_orig/$height_orig;
  479. $width = $height = min($width_orig, $height_orig);
  480. if ($ratio_orig > 1) {
  481. $x = ($width_orig/2) - ($width/2);
  482. $y = 0;
  483. } else {
  484. $y = ($height_orig/2) - ($height/2);
  485. $x = 0;
  486. }
  487. if($size>0){
  488. $targetWidth=$size;
  489. $targetHeight=$size;
  490. }else{
  491. $targetWidth=$width;
  492. $targetHeight=$height;
  493. }
  494. $process = imagecreatetruecolor($targetWidth, $targetHeight);
  495. if ($process == false) {
  496. OC_Log::write('core','OC_Image->centerCrop. Error creating true color image',OC_Log::ERROR);
  497. imagedestroy($process);
  498. return false;
  499. }
  500. imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
  501. if ($process == false) {
  502. OC_Log::write('core','OC_Image->centerCrop. Error resampling process image '.$width.'x'.$height,OC_Log::ERROR);
  503. imagedestroy($process);
  504. return false;
  505. }
  506. imagedestroy($this->resource);
  507. $this->resource = $process;
  508. return true;
  509. }
  510. /**
  511. * @brief Crops the image from point $x$y with dimension $wx$h.
  512. * @param $x Horizontal position
  513. * @param $y Vertical position
  514. * @param $w Width
  515. * @param $h Hight
  516. * @returns bool for success or failure
  517. */
  518. public function crop($x, $y, $w, $h) {
  519. if(!$this->valid()) {
  520. OC_Log::write('core',__METHOD__.'(): No image loaded', OC_Log::ERROR);
  521. return false;
  522. }
  523. $width_orig=imageSX($this->resource);
  524. $height_orig=imageSY($this->resource);
  525. //OC_Log::write('core',__METHOD__.'(): Original size: '.$width_orig.'x'.$height_orig, OC_Log::DEBUG);
  526. $process = imagecreatetruecolor($w, $h);
  527. if ($process == false) {
  528. OC_Log::write('core',__METHOD__.'(): Error creating true color image',OC_Log::ERROR);
  529. imagedestroy($process);
  530. return false;
  531. }
  532. imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
  533. if ($process == false) {
  534. OC_Log::write('core',__METHOD__.'(): Error resampling process image '.$w.'x'.$h,OC_Log::ERROR);
  535. imagedestroy($process);
  536. return false;
  537. }
  538. imagedestroy($this->resource);
  539. $this->resource = $process;
  540. return true;
  541. }
  542. public function destroy(){
  543. if($this->valid()){
  544. imagedestroy($this->resource);
  545. }
  546. $this->resource=null;
  547. }
  548. public function __destruct(){
  549. $this->destroy();
  550. }
  551. }