-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageTable.php
More file actions
131 lines (103 loc) · 2.94 KB
/
ImageTable.php
File metadata and controls
131 lines (103 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace jakerb;
class ImageTable {
/**
* @var string[]
* A list of allowed MIME Types.
*/
protected $allowedMimeTypes = [
'image/png',
'image/jpeg',
];
/**
* @var string
* The path to the image being processed.
*/
protected $imageSrc;
/**
* @var string $imageMime
* The MIME type of image used to specify to GD.
*/
protected $imageMIME;
/**
* ImageTable constructor.
*
* @param string $imageSrc Path to the image.
*
* @return \jakerb\ImageTable
* @throws \Exception
*/
public function __construct($imageSrc) {
if (!extension_loaded('gd')) {
throw new \Exception("ImageTable requires GD library to work.", 1);
}
if (!file_exists($imageSrc)) {
throw new \Exception("The file you provided could not be found.", 1);
}
$this->imageMIME = mime_content_type($imageSrc);
if (!in_array(strtolower($this->imageMIME), $this->allowedMimeTypes)) {
throw new \Exception("The file you provided is of an unsupported or unrecognised type.", 1);
}
$this->imageSrc = $imageSrc;
return $this;
}
/**
* @param bool|string $outputFileName Path of output file, or false to skip
* file generation.
*
* @return \jakerb\ImageTable
*/
public function renderTable($outputFileName = FALSE) {
$write_to_file = $outputFileName !== false;
$image = null;
$file = null;
switch ($this->imageMIME) {
case 'image/jpeg':
$image = imagecreatefromjpeg($this->imageSrc);
break;
case 'image/png':
$image = imagecreatefrompng($this->imageSrc);
break;
}
if (!isset($image)) {
throw new \Exception("Failed to create image from supplied file.", 1);
}
if ($write_to_file) {
if (file_exists($outputFileName)) {
throw new \Exception("Cannot render table to specified file as it already exists.", 1);
}
$file = fopen($outputFileName, "w");
ob_start();
}
$width = imagesx($image);
$height = imagesy($image);
$style = '<style>table.imagetable tr td { padding: 0; width: 1px; height: 1px; }</style>';
$table_open = '<table class="imagetable" style="border:0;border-collapse:collapse;">';
$table_close = '</table>';
$table_row_open = '<tr>';
$table_row_close = '</tr>';
echo $style;
echo $table_open;
for ($y = 0; $y < $height; $y++) {
echo $table_row_open;
for ($x = 0; $x < $width; $x++) {
if($rgb = imagecolorat($image, $x, $y)) {
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$table_column = sprintf("<td bgcolor=\"#%02x%02x%02x\"></td>", $r, $g, $b);
echo $table_column;
}
}
echo $table_row_close;
}
echo $table_close;
if($write_to_file){
$file_contents = ob_get_contents();
ob_end_clean();
fwrite($file, $file_contents);
fclose($file);
}
return $this;
}
}