TXT.php 2.8 KB

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