-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateRGBArray.php
More file actions
executable file
·115 lines (107 loc) · 3.05 KB
/
createRGBArray.php
File metadata and controls
executable file
·115 lines (107 loc) · 3.05 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
<?php
function getBestSkip($totalPixels, $imageSizes) {
$toReturn = 0;
foreach($imageSizes as $arr) { // => $array) {
// var_dump($arr);
if ($arr["Pixels"] >= $totalPixels) {
// echo $arr["skipSize"];
return $toReturn * 3; // $arr["skipSize"]; TODO
} else {
$toReturn = $arr["skipSize"];
}
// echo ($toReturn);
// echo "<br>";
}
return 25;
}
/*
* Returns an image php object to pass to createRGBArray method
*/
function getImageFromURL($url) {
if (strlen($url) == 0) {
return;
}
$urlArray = explode(".", $url);
$image = null;
switch ($urlArray[count($urlArray) - 1]) {
case 'png':
$image = imagecreatefrompng($url);
break;
case 'jpeg':
$image = imagecreatefromjpeg($url);
break;
case 'png':
$image = imagecreatefrompng($url);
break;
case 'gif':
$image = imagecreatefromgif($url);
break;
case 'bmp':
$image = imagecreatefrombmp($url);
break;
case 'xbm':
$image = imagecreatefromxbm($url);
break;
default:
$imageTry = imagepng(
imagecreatefromstring(file_get_contents($url)),
"temp"
);
if ($imageTry != null && $imageTry != false) {
$image = imagecreatefrompng("temp");
}
break;
}
if ($image == null) {
echo "\nalert(\"file type not supported at this time!\");</script>";
die();
}
return $image;
}
function getImageWidth($image) {
return imagesx($image);
}
function getImageHeight($image) {
return imagesy($image);
}
function getTotalPixels($image) {
return getImageWidth($image) * getImageHeight($image);
}
/**
* Creates the array of rgb values based on the given url in JavaScript form.
**/
function createRGBArray($image, $skipSize) {
$width = imagesx($image);
$height = imagesy($image);
// $skipSize = 2; //TODO : A function to automate this number to be as low as possible.
//($width >= $height ? $height : $height) / 50;
$newWidth = $width / $skipSize; //imagesx($image);// / $skipSize; //imagesx($image)
$newHeight = $height / $skipSize; //imagesy($image);// / $skipSize;
$rgbArray = "const rgbArray = [";
for ($x = 0; $x < $width; $x += $skipSize) {
//for loop runs through each column and...
$rgbArray = $rgbArray . "[";
for ($y = 0; $y < $height; $y += $skipSize) {
// each row to add rgb values.
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xff;
$g = ($rgb >> 8) & 0xff;
$b = $rgb & 0xff;
$rgbArray =
$rgbArray .
"[" .
$r .
"," .
$g .
"," .
$b .
"]" .
($y + $skipSize < $height ? "," : "");
}
$rgbArray = $rgbArray . "]" . ($x + $skipSize < $width ? "," : "");
}
$rgbArray = $rgbArray . "];";
imagedestroy($image);
return $rgbArray;
}
?>