-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollage.php
108 lines (93 loc) · 3.35 KB
/
collage.php
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
<?php
class Collage
{
protected $images;
protected $width;
protected $height;
protected $grid_width;
protected $grid_height;
protected $image;
public function __construct($images, $width = 500, $height = 500, $grid_width = 10, $grid_height = 10)
{
$this->images = $images;
$this->width = $width;
$this->height = $height;
$this->grid_width = $grid_width;
$this->grid_height = $grid_height;
$this->image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($this->image, 255, 255, 255);
imagefill($this->image, 0, 0, $white);
}
public function __destruct()
{
imagedestroy($this->image);
}
public function display()
{
$this->grid();
$this->box(3, 4, 2, 5);
foreach ($this->images as $image) {
$img = imagecreatefromjpeg($image);
$this->image_box($img, 3, 4, 2, 5);
break;
}
header("Content-type: image/jpeg");
imagejpeg($this->image);
}
private function grid()
{
$black = imagecolorallocate($this->image, 0, 0, 0);
imagesetthickness($this->image, 3);
$cell_width = ($this->width - 1) / $this->grid_width; // note: -1 to avoid writting
$cell_height = ($this->height - 1) / $this->grid_height; // a pixel outside the image
for ($x = 0; $x <= $this->grid_width; $x++) {
for ($y = 0; $y <= $this->grid_height; $y++) {
imageline($this->image, ($x * $cell_width), 0, ($x * $cell_width), $this->height, $black);
imageline($this->image, 0, ($y * $cell_height), $this->width, ($y * $cell_height), $black);
}
}
}
private function box($w, $h, $x, $y)
{
// Cell width
$cell_width = $this->width / $this->grid_width;
$cell_height = $this->height / $this->grid_height;
// Conversion of our virtual sizes/positions to real ones
$size_w = ($cell_width * $w);
$size_h = ($cell_height * $h);
$pos_x = ($cell_width * $x);
$pos_y = ($cell_height * $y);
// Getting top left and bottom right of our rectangle
$top_left_x = $pos_x;
$top_left_y = $pos_y;
$bottom_right_x = $pos_x + $size_w;
$bottom_right_y = $pos_y + $size_h;
// Displaying rectangle
$red = imagecolorallocate($this->image, 100, 0, 0);
imagefilledrectangle($this->image, $top_left_x, $top_left_y, $bottom_right_x, $bottom_right_y, $red);
}
public function image_box($img, $w, $h, $x, $y)
{
// Cell width
$cell_width = $this->width / $this->grid_width;
$cell_height = $this->height / $this->grid_height;
// Conversion of our virtual sizes/positions to real ones
$size_w = ceil($cell_width * $w);
$size_h = ceil($cell_height * $h);
$pos_x = ($cell_width * $x);
$pos_y = ($cell_height * $y);
// Copying the image
imagecopyresampled($this->image, $img, $pos_x, $pos_y, 0, 0, $size_w, $size_h, imagesx($img), imagesy($img));
}
}
$images = [];
$path = '5';
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$images[] = $path . DIRECTORY_SEPARATOR . $fileinfo->getFilename();
}
}
$collage = new Collage($images);
$collage->display();
?>