txt.php 1.55 KB
Newer Older
1
2
3
4
5
6
7
<?php
/**
 * Copyright (c) 2013 Georg Ehrke georg@ownCloud.com
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
Georg Ehrke's avatar
Georg Ehrke committed
8
9
namespace OC\Preview;

Georg Ehrke's avatar
Georg Ehrke committed
10
class TXT extends Provider {
11
12
13
	/**
	 * {@inheritDoc}
	 */
Georg Ehrke's avatar
Georg Ehrke committed
14
	public function getMimeType() {
15
		return '/text\/plain/';
16
17
	}

18
	/**
19
	 * {@inheritDoc}
20
21
22
23
24
	 */
	public function isAvailable($file) {
		return $file->getSize() > 5;
	}

25
	/**
26
	 * {@inheritDoc}
27
	 */
Georg Ehrke's avatar
Georg Ehrke committed
28
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
29
		$content = $fileview->fopen($path, 'r');
Nmz's avatar
Nmz committed
30
		$content = stream_get_contents($content,3000);
31

32
		//don't create previews of empty text files
33
34
35
36
		if(trim($content) === '') {
			return false;
		}

37
38
		$lines = preg_split("/\r\n|\n|\r/", $content);

Georg Ehrke's avatar
Georg Ehrke committed
39
40
		$fontSize = 5; //5px
		$lineSize = ceil($fontSize * 1.25);
41
42

		$image = imagecreate($maxX, $maxY);
Georg Ehrke's avatar
Georg Ehrke committed
43
44
		imagecolorallocate($image, 255, 255, 255);
		$textColor = imagecolorallocate($image, 0, 0, 0);
45

46
47
48
49
		$fontFile  = __DIR__;
		$fontFile .= '/../../../core';
		$fontFile .= '/fonts/OpenSans-Regular.ttf';

50
51
		$canUseTTF = function_exists('imagettftext');

Georg Ehrke's avatar
Georg Ehrke committed
52
		foreach($lines as $index => $line) {
53
54
55
			$index = $index + 1;

			$x = (int) 1;
56
			$y = (int) ($index * $lineSize);
57

58
59
60
61
62
63
			if ($canUseTTF === true) {
				imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
			} else {
				$y -= $fontSize;
				imagestring($image, 1, $x, $y, $line, $textColor);
			}
64

Georg Ehrke's avatar
Georg Ehrke committed
65
			if(($index * $lineSize) >= $maxY) {
66
67
68
69
70
				break;
			}
		}

		$image = new \OC_Image($image);
Georg Ehrke's avatar
Georg Ehrke committed
71

Georg Ehrke's avatar
Georg Ehrke committed
72
		return $image->valid() ? $image : false;
73
74
	}
}