TXT.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. $fontSize = $maxX ? (int) ((5 / 32) * $maxX) : 5; //5px
  56. $lineSize = ceil($fontSize * 1.25);
  57. $image = imagecreate($maxX, $maxY);
  58. imagecolorallocate($image, 255, 255, 255);
  59. $textColor = imagecolorallocate($image, 0, 0, 0);
  60. $fontFile = __DIR__;
  61. $fontFile .= '/../../../core';
  62. $fontFile .= '/fonts/OpenSans-Regular.ttf';
  63. $canUseTTF = function_exists('imagettftext');
  64. foreach($lines as $index => $line) {
  65. $index = $index + 1;
  66. $x = (int) 1;
  67. $y = (int) ($index * $lineSize);
  68. if ($canUseTTF === true) {
  69. imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
  70. } else {
  71. $y -= $fontSize;
  72. imagestring($image, 1, $x, $y, $line, $textColor);
  73. }
  74. if(($index * $lineSize) >= $maxY) {
  75. break;
  76. }
  77. }
  78. $imageObject = new \OC_Image();
  79. $imageObject->setResource($image);
  80. return $imageObject->valid() ? $imageObject : false;
  81. }
  82. }