@@ -27,74 +27,74 @@ |
||
27 | 27 | namespace OC\Preview; |
28 | 28 | |
29 | 29 | class TXT extends Provider { |
30 | - /** |
|
31 | - * {@inheritDoc} |
|
32 | - */ |
|
33 | - public function getMimeType() { |
|
34 | - return '/text\/plain/'; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * {@inheritDoc} |
|
39 | - */ |
|
40 | - public function isAvailable(\OCP\Files\FileInfo $file) { |
|
41 | - return $file->getSize() > 0; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * {@inheritDoc} |
|
46 | - */ |
|
47 | - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { |
|
48 | - $content = $fileview->fopen($path, 'r'); |
|
49 | - |
|
50 | - if ($content === false) { |
|
51 | - return false; |
|
52 | - } |
|
53 | - |
|
54 | - $content = stream_get_contents($content,3000); |
|
55 | - |
|
56 | - //don't create previews of empty text files |
|
57 | - if(trim($content) === '') { |
|
58 | - return false; |
|
59 | - } |
|
60 | - |
|
61 | - $lines = preg_split("/\r\n|\n|\r/", $content); |
|
62 | - |
|
63 | - // Define text size of text file preview |
|
64 | - $fontSize = $maxX ? (int) ((2 / 32) * $maxX) : 5; //5px |
|
65 | - $lineSize = ceil($fontSize * 1.5); |
|
66 | - |
|
67 | - $image = imagecreate($maxX, $maxY); |
|
68 | - imagecolorallocate($image, 255, 255, 255); |
|
69 | - $textColor = imagecolorallocate($image, 0, 0, 0); |
|
70 | - |
|
71 | - $fontFile = __DIR__; |
|
72 | - $fontFile .= '/../../../core'; |
|
73 | - $fontFile .= '/fonts/Nunito-Regular.ttf'; |
|
74 | - |
|
75 | - $canUseTTF = function_exists('imagettftext'); |
|
76 | - |
|
77 | - foreach($lines as $index => $line) { |
|
78 | - $index = $index + 1; |
|
79 | - |
|
80 | - $x = (int) 1; |
|
81 | - $y = (int) ($index * $lineSize); |
|
82 | - |
|
83 | - if ($canUseTTF === true) { |
|
84 | - imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); |
|
85 | - } else { |
|
86 | - $y -= $fontSize; |
|
87 | - imagestring($image, 1, $x, $y, $line, $textColor); |
|
88 | - } |
|
89 | - |
|
90 | - if(($index * $lineSize) >= $maxY) { |
|
91 | - break; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - $imageObject = new \OC_Image(); |
|
96 | - $imageObject->setResource($image); |
|
97 | - |
|
98 | - return $imageObject->valid() ? $imageObject : false; |
|
99 | - } |
|
30 | + /** |
|
31 | + * {@inheritDoc} |
|
32 | + */ |
|
33 | + public function getMimeType() { |
|
34 | + return '/text\/plain/'; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * {@inheritDoc} |
|
39 | + */ |
|
40 | + public function isAvailable(\OCP\Files\FileInfo $file) { |
|
41 | + return $file->getSize() > 0; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * {@inheritDoc} |
|
46 | + */ |
|
47 | + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { |
|
48 | + $content = $fileview->fopen($path, 'r'); |
|
49 | + |
|
50 | + if ($content === false) { |
|
51 | + return false; |
|
52 | + } |
|
53 | + |
|
54 | + $content = stream_get_contents($content,3000); |
|
55 | + |
|
56 | + //don't create previews of empty text files |
|
57 | + if(trim($content) === '') { |
|
58 | + return false; |
|
59 | + } |
|
60 | + |
|
61 | + $lines = preg_split("/\r\n|\n|\r/", $content); |
|
62 | + |
|
63 | + // Define text size of text file preview |
|
64 | + $fontSize = $maxX ? (int) ((2 / 32) * $maxX) : 5; //5px |
|
65 | + $lineSize = ceil($fontSize * 1.5); |
|
66 | + |
|
67 | + $image = imagecreate($maxX, $maxY); |
|
68 | + imagecolorallocate($image, 255, 255, 255); |
|
69 | + $textColor = imagecolorallocate($image, 0, 0, 0); |
|
70 | + |
|
71 | + $fontFile = __DIR__; |
|
72 | + $fontFile .= '/../../../core'; |
|
73 | + $fontFile .= '/fonts/Nunito-Regular.ttf'; |
|
74 | + |
|
75 | + $canUseTTF = function_exists('imagettftext'); |
|
76 | + |
|
77 | + foreach($lines as $index => $line) { |
|
78 | + $index = $index + 1; |
|
79 | + |
|
80 | + $x = (int) 1; |
|
81 | + $y = (int) ($index * $lineSize); |
|
82 | + |
|
83 | + if ($canUseTTF === true) { |
|
84 | + imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); |
|
85 | + } else { |
|
86 | + $y -= $fontSize; |
|
87 | + imagestring($image, 1, $x, $y, $line, $textColor); |
|
88 | + } |
|
89 | + |
|
90 | + if(($index * $lineSize) >= $maxY) { |
|
91 | + break; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + $imageObject = new \OC_Image(); |
|
96 | + $imageObject->setResource($image); |
|
97 | + |
|
98 | + return $imageObject->valid() ? $imageObject : false; |
|
99 | + } |
|
100 | 100 | } |