TXT.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.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. use OCP\Files\File;
  29. use OCP\Files\FileInfo;
  30. use OCP\IImage;
  31. class TXT extends ProviderV2 {
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function getMimeType(): string {
  36. return '/text\/plain/';
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function isAvailable(FileInfo $file): bool {
  42. return $file->getSize() > 0;
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  48. $content = $file->fopen('r');
  49. if ($content === false) {
  50. return null;
  51. }
  52. $content = stream_get_contents($content,3000);
  53. //don't create previews of empty text files
  54. if(trim($content) === '') {
  55. return null;
  56. }
  57. $lines = preg_split("/\r\n|\n|\r/", $content);
  58. // Define text size of text file preview
  59. $fontSize = $maxX ? (int) ((2 / 32) * $maxX) : 5; //5px
  60. $lineSize = ceil($fontSize * 1.5);
  61. $image = imagecreate($maxX, $maxY);
  62. imagecolorallocate($image, 255, 255, 255);
  63. $textColor = imagecolorallocate($image, 0, 0, 0);
  64. $fontFile = __DIR__;
  65. $fontFile .= '/../../../core';
  66. $fontFile .= '/fonts/NotoSans-Regular.ttf';
  67. $canUseTTF = function_exists('imagettftext');
  68. foreach($lines as $index => $line) {
  69. $index = $index + 1;
  70. $x = (int) 1;
  71. $y = (int) ($index * $lineSize);
  72. if ($canUseTTF === true) {
  73. imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
  74. } else {
  75. $y -= $fontSize;
  76. imagestring($image, 1, $x, $y, $line, $textColor);
  77. }
  78. if(($index * $lineSize) >= $maxY) {
  79. break;
  80. }
  81. }
  82. $imageObject = new \OC_Image();
  83. $imageObject->setResource($image);
  84. return $imageObject->valid() ? $imageObject : null;
  85. }
  86. }