-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
122 lines (100 loc) · 3.05 KB
/
Copy pathtest.php
File metadata and controls
122 lines (100 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
116
117
118
119
120
121
122
<?php
require_once('vendor/autoload.php');
$test_dir = 'test_output';
$test_image = 'assets/feed-example.png';
if (file_exists($test_dir)) {
// Clear out $test_dir
array_map('unlink', array_filter((array) glob("$test_dir/*")));
} else {
// Ensure $test_dir exists
mkdir($test_dir, 0777, true);
}
function resize_image($image, $width = null, $height = null, $destroy_og = true) {
$og_width = imagesx($image);
$og_height = imagesy($image);
$ratio = $og_width / $og_height;
if (!$width) $width = $og_width;
if (!$height) $height = $og_height;
if ($width / $height > $ratio) {
$width = $height * $ratio;
} else {
$height = $width / $ratio;
}
$img = imagecreatetruecolor($width, $height);
imagecopyresampled($img, $image, 0, 0, 0, 0, $width, $height, $og_width, $og_height);
if ($destroy_og) {
imagedestroy($image);
}
return $img;
}
function save_image($image, $file) {
switch(pathinfo($file, PATHINFO_EXTENSION)) {
case 'jpg':
case 'jpeg':
imagejpeg($image, $file, 0);
break;
case 'png':
imagepng($image, $file, 0);
break;
}
}
$contents = file_get_contents($test_image);
$ext = pathinfo($test_image, PATHINFO_EXTENSION);
$img = imagecreatefromstring($contents);
// $img = resize_image($img, imagesx($img) / 4, imagesy($img) / 4); // Optionally shrink image to speed things up
$items = img_items($img, array(
// 'background' => 0,
// 'background_threshold' => 5,
// 'gap_threshold' => 5,
// 'gap_y_threshold' => null,
// 'gap_x_threshold' => null,
// 'size_threshold' => 5,
// 'height_threshold' => null,
// 'width_threshold' => null
));
print_r($items);
if (count($items)) {
// Extract all items as images
foreach($items as $index => $item) {
$item_img = imagecrop($img, array(
'x' => $item['left'],
'y' => $item['top'],
'width' => $item['width'],
'height' => $item['height']
));
save_image($item_img, "$test_dir/$index.$ext");
imagedestroy($item_img);
}
// Extract the largest item as an image
$largest = array_reduce($items, function($c, $i) {
return (($i['width'] + $i['height']) > ($c['width'] + $c['height'])) ? $i : $c;
});
$largest_img = imagecrop($img, array(
'x' => $largest['left'],
'y' => $largest['top'],
'width' => $largest['width'],
'height' => $largest['height']
));
save_image($largest_img, "$test_dir/largest.$ext");
imagedestroy($largest_img);
// Fill in all items
$filled_img = resize_image($img, null, null, false);
foreach($items as $item) {
for ($y = $item['top']; $y < $item['bottom'] + 1; $y++) {
for ($x = $item['left']; $x < $item['right'] + 1; $x++) {
$color = imagecolorallocate($filled_img, 255, 0, 0);
imagesetpixel($filled_img, $x, $y, $color);
}
}
}
save_image($filled_img, "$test_dir/filled.$ext");
imagedestroy($filled_img);
}
imagedestroy($img);
// @TODO: Implement better testing
if (count($items) === 50) {
echo "Test passed.\n";
} else {
echo "Test failed.\n";
}
?>