txt.php 1.65 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

30
		$content = $fileview->fopen($path, 'r');
Nmz's avatar
Nmz committed
31
		$content = stream_get_contents($content,3000);
32

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

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

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

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

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

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

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

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

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

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

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

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

Georg Ehrke's avatar
Georg Ehrke committed
77
78
class MarkDown extends TXT {
	public function getMimeType() {
Thomas Müller's avatar
Thomas Müller committed
79
		return '/text\/(x-)?markdown/';
Georg Ehrke's avatar
Georg Ehrke committed
80
81
82
	}

}