-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageGenerator.php
108 lines (94 loc) · 3.18 KB
/
ImageGenerator.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
namespace BVW\UploadBundle;
use Imagine\Image\Point;
use Imagine\Image\Color;
use Imagine\Image\Box;
class ImageGenerator {
private $imagine;
public function __construct($imagine, $settings)
{
$this->imagine = $imagine;
$this->temp_dir = $settings['temp_dir'];
$this->formats = $settings['formats'];
}
public function getImageSize($file)
{
$image = $this->imagine->open($file->getPathname());
return array(
'width' => $image->getSize()->getWidth(),
'height' => $image->getSize()->getHeight()
);
}
public function generateFromFile($path, $format, $crop = false)
{
$image = $this->imagine->open($path);
if (is_array($crop) && $crop['w'] > 0 && $crop['h'] > 0) {
$image->crop(new Point($crop['x'], $crop['y']), new Box($crop['w'], $crop['h']));
}
$width = $image->getSize()->getWidth();
$height = $image->getSize()->getHeight();
$result = array();
$formatSpec = $this->formats[$format];
foreach($formatSpec as $name => $settings)
{
if (!$settings['width'] && !$settings['height']) {
$thumb = $image;
} else {
$ratios = array(
$settings['width'] / $width,
$settings['height'] / $height
);
$targetSize = new Box($settings['width'], $settings['height']);
switch($settings['mode']) {
case 'fit':
case 'scale':
case 'limit':
if ($settings['mode'] == 'limit' && $targetSize->contains($image->getSize())) {
$thumb = $image;
break;
}
$img = $image->copy();
$ratio = min($ratios);
$scaledSize = $image->getSize()->scale($ratio);
$img->resize($scaledSize);
if ($settings['mode'] != 'fit') {
$thumb = $img;
break;
}
$color = new Color($settings['background_color'], 100);
$thumb = $this->imagine->create($targetSize, $color);
$thumb->paste($img, new Point(
max(0, ($targetSize->getWidth() - $scaledSize->getWidth()) / 2.0),
max(0, ($targetSize->getHeight() - $scaledSize->getHeight()) / 2.0)));
break;
case 'inflate':
case 'deflate':
$thumb = $image->copy();
$thumb->resize($targetSize);
break;
case 'center':
$thumb = $image->copy();
$ratio = max($ratios);
$scaledSize = $image->getSize()->scale($ratio);
$thumb->resize($scaledSize);
$thumb->crop(new Point(
max(0, round(($scaledSize->getWidth() - $targetSize->getWidth()) / 2)),
max(0, round(($scaledSize->getHeight() - $targetSize->getHeight()) / 2))
), $targetSize);
break;
}
}
$outputPath = tempnam($this->temp_dir, 'upload');
$thumb->save($outputPath, array('format' => 'jpg'));
$result[$name] = $outputPath;
}
return $result;
}
public function getFormat($format)
{
if (!array_key_exists($format, $this->formats)) {
throw new \InvalidArgumentException("no such format: " . $format);
}
return $this->formats[$format];
}
}