Hi everyone!
When generating a new text file thumbnail (*.md for example), I’m always getting this Error in the logs:
[PHP] Error: imagettftext(): Problem doing text layout at /var/www/nextcloud/lib/private/Preview/TXT.php#91
GET /index.php/core/preview?fileId=1986&x=32&y=32
from *** by *** at 2020-04-14T12:00:25+00:00
This is my server details:
## Server configuration detail
**Operating system:** Linux 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64
**Webserver:** Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.2.29 (apache2handler)
**Database:** mysql 10.3.22
**PHP version:** 7.2.29
Modules loaded: Core, date, libxml, openssl, pcre, zlib, filter, hash, Reflection, SPL, session, standard, apache2handler, bz2, calendar, ctype, curl, dom, mbstring, fileinfo, ftp, gd, gettext, iconv, intl, json, exif, mysqlnd, PDO, Phar, posix, shmop, SimpleXML, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xml, xmlwriter, xsl, mysqli, pdo_mysql, pdo_sqlite, wddx, xmlreader, apcu, imagick, zip, Zend OPcache
**Nextcloud version:** 18.0.3 - 18.0.3.0
**Browser:** Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0
And here is the TXT.php in question:
36 class TXT extends ProviderV2 {
37 /**
38 * {@inheritDoc}
39 */
40 public function getMimeType(): string {
41 return '/text\/plain/';
42 }
43
44 /**
45 * {@inheritDoc}
46 */
47 public function isAvailable(FileInfo $file): bool {
48 return $file->getSize() > 0;
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
55 $content = $file->fopen('r');
56
57 if ($content === false) {
58 return null;
59 }
60
61 $content = stream_get_contents($content,3000);
62
63 //don't create previews of empty text files
64 if(trim($content) === '') {
65 return null;
66 }
67
68 $lines = preg_split("/\r\n|\n|\r/", $content);
69
70 // Define text size of text file preview
71 $fontSize = $maxX ? (int) ((1 / 32) * $maxX) : 5; //5px
72 $lineSize = ceil($fontSize * 1.5);
73
74 $image = imagecreate($maxX, $maxY);
75 imagecolorallocate($image, 255, 255, 255);
76 $textColor = imagecolorallocate($image, 0, 0, 0);
77
78 $fontFile = __DIR__;
79 $fontFile .= '/../../../core';
80 $fontFile .= '/fonts/NotoSans-Regular.ttf';
81
82 $canUseTTF = function_exists('imagettftext');
83
84 foreach($lines as $index => $line) {
85 $index = $index + 1;
86
87 $x = (int) 1;
88 $y = (int) ($index * $lineSize);
89
90 if ($canUseTTF === true) {
91 imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
92 } else {
93 $y -= $fontSize;
94 imagestring($image, 1, $x, $y, $line, $textColor);
95 }
96
97 if(($index * $lineSize) >= $maxY) {
98 break;
99 }
100 }
101
102 $imageObject = new \OC_Image();
103 $imageObject->setResource($image);
104
105 return $imageObject->valid() ? $imageObject : null;
106 }
107 }
# php -i | grep "FreeType"
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.8.0
Any help would be greatly appreciated.
P.S.: First post, let me know if need any more info.
Thanks!