TXT.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <georg@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Nmz <nemesiz@nmz.lt>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Preview;
  28. class TXT extends Provider {
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function getMimeType() {
  33. return '/text\/plain/';
  34. }
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function isAvailable(\OCP\Files\FileInfo $file) {
  39. return $file->getSize() > 0;
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  45. $content = $fileview->fopen($path, 'r');
  46. $content = stream_get_contents($content,3000);
  47. //don't create previews of empty text files
  48. if(trim($content) === '') {
  49. return false;
  50. }
  51. $lines = preg_split("/\r\n|\n|\r/", $content);
  52. $fontSize = ($maxX) ? (int) ((5 / 32) * $maxX) : 5; //5px
  53. $lineSize = ceil($fontSize * 1.25);
  54. $image = imagecreate($maxX, $maxY);
  55. imagecolorallocate($image, 255, 255, 255);
  56. $textColor = imagecolorallocate($image, 0, 0, 0);
  57. $fontFile = __DIR__;
  58. $fontFile .= '/../../../core';
  59. $fontFile .= '/fonts/OpenSans-Regular.ttf';
  60. $canUseTTF = function_exists('imagettftext');
  61. foreach($lines as $index => $line) {
  62. $index = $index + 1;
  63. $x = (int) 1;
  64. $y = (int) ($index * $lineSize);
  65. if ($canUseTTF === true) {
  66. imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
  67. } else {
  68. $y -= $fontSize;
  69. imagestring($image, 1, $x, $y, $line, $textColor);
  70. }
  71. if(($index * $lineSize) >= $maxY) {
  72. break;
  73. }
  74. }
  75. $image = new \OC_Image($image);
  76. return $image->valid() ? $image : false;
  77. }
  78. }