TXT.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 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. if ($content === false) {
  47. return false;
  48. }
  49. $content = stream_get_contents($content,3000);
  50. //don't create previews of empty text files
  51. if(trim($content) === '') {
  52. return false;
  53. }
  54. $lines = preg_split("/\r\n|\n|\r/", $content);
  55. // Define text size of text file preview
  56. $fontSize = $maxX ? (int) ((2 / 32) * $maxX) : 5; //5px
  57. $lineSize = ceil($fontSize * 1.5);
  58. $image = imagecreate($maxX, $maxY);
  59. imagecolorallocate($image, 255, 255, 255);
  60. $textColor = imagecolorallocate($image, 0, 0, 0);
  61. $fontFile = __DIR__;
  62. $fontFile .= '/../../../core';
  63. $fontFile .= '/fonts/Nunito-Regular.ttf';
  64. $canUseTTF = function_exists('imagettftext');
  65. foreach($lines as $index => $line) {
  66. $index = $index + 1;
  67. $x = (int) 1;
  68. $y = (int) ($index * $lineSize);
  69. if ($canUseTTF === true) {
  70. imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
  71. } else {
  72. $y -= $fontSize;
  73. imagestring($image, 1, $x, $y, $line, $textColor);
  74. }
  75. if(($index * $lineSize) >= $maxY) {
  76. break;
  77. }
  78. }
  79. $imageObject = new \OC_Image();
  80. $imageObject->setResource($image);
  81. return $imageObject->valid() ? $imageObject : false;
  82. }
  83. }