1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenrizbes\CropBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Fenrizbes\UploadableBundle\File\UploadableFile; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
7
|
|
|
|
8
|
|
|
class CropTwigExtension extends \Twig_Extension |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $root_path; |
14
|
|
|
|
15
|
|
|
public function __construct($root_path) |
16
|
|
|
{ |
17
|
|
|
$this->root_path = $root_path; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function getName() |
24
|
|
|
{ |
25
|
|
|
return 'CropTwigExtension'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getFilters() |
32
|
|
|
{ |
33
|
|
|
return array( |
34
|
|
|
new \Twig_SimpleFilter('crop', array($this, 'crop')) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function getFunctions() |
42
|
|
|
{ |
43
|
|
|
return array( |
44
|
|
|
new \Twig_SimpleFunction('image_size', array($this, 'getSize')) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Crops a file and returns crop's web path |
50
|
|
|
* |
51
|
|
|
* @param array $croppable |
52
|
|
|
* @param int $index |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function crop(array $croppable, $index = 0) |
56
|
|
|
{ |
57
|
|
|
$coordinates = $croppable['coordinates'][$index]; |
58
|
|
|
|
59
|
|
|
$file = new UploadableFile($this->root_path, $croppable['image']); |
60
|
|
|
$name = $this->makeCropName($file, $coordinates); |
61
|
|
|
$path = $file->getRootPath() . $name; |
62
|
|
|
|
63
|
|
|
if (!file_exists($path)) { |
64
|
|
|
$this->doCrop($file, $coordinates, $path); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the name for a cropped file |
72
|
|
|
* |
73
|
|
|
* @param UploadableFile $file |
74
|
|
|
* @param $coordinates |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
protected function makeCropName(UploadableFile $file, $coordinates) |
78
|
|
|
{ |
79
|
|
|
$ext = $file->getExtension(); |
80
|
|
|
$suffix = implode('_', $coordinates); |
81
|
|
|
|
82
|
|
|
return preg_replace('/\.'. $ext .'$/ui', '_crop_'. $suffix .'.'. $ext, $file->getWebPath()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Crops a file |
87
|
|
|
* |
88
|
|
|
* @param UploadableFile $file |
89
|
|
|
* @param $coordinates |
90
|
|
|
* @param $path |
91
|
|
|
*/ |
92
|
|
|
protected function doCrop(UploadableFile $file, $coordinates, $path) |
93
|
|
|
{ |
94
|
|
|
$crop = imagecreatetruecolor($coordinates['min_width'], $coordinates['min_height']); |
95
|
|
|
|
96
|
|
|
switch ($file->getExtension()) { |
97
|
|
|
case 'gif': |
98
|
|
|
$source = imagecreatefromgif($file); break; |
|
|
|
|
99
|
|
|
|
100
|
|
|
case 'png': |
101
|
|
|
$source = imagecreatefrompng($file); break; |
|
|
|
|
102
|
|
|
|
103
|
|
|
default: |
104
|
|
|
$source = imagecreatefromjpeg($file); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (preg_match('/^(gif|png)$/', $file->getExtension())) { |
108
|
|
|
imagecolortransparent($crop, imagecolorallocatealpha($crop, 0, 0, 0, 127)); |
109
|
|
|
imagealphablending($crop, false); |
110
|
|
|
imagesavealpha($crop, true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
imagecopyresampled( |
114
|
|
|
$crop, $source, |
115
|
|
|
0, 0, |
116
|
|
|
$coordinates['left'], $coordinates['top'], |
117
|
|
|
$coordinates['min_width'], $coordinates['min_height'], |
118
|
|
|
$coordinates['width'], $coordinates['height'] |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
switch ($file->getExtension()) { |
122
|
|
|
case 'gif': |
123
|
|
|
imagegif($crop, $path); break; |
|
|
|
|
124
|
|
|
|
125
|
|
|
case 'png': |
126
|
|
|
imagepng($crop, $path); break; |
|
|
|
|
127
|
|
|
|
128
|
|
|
default: |
129
|
|
|
imagejpeg($crop, $path, 100); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns image size info |
135
|
|
|
* |
136
|
|
|
* @param $image |
137
|
|
|
* @param bool $relative |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getSize($image, $relative = false) |
141
|
|
|
{ |
142
|
|
|
if (!$image instanceof File) { |
143
|
|
|
if ($relative) { |
144
|
|
|
$image = $this->root_path . $image; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$image = new File($image); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return getimagesize($image->getRealPath()); |
151
|
|
|
} |
152
|
|
|
} |
As per the PSR-2 coding standard, the
break
(or other terminating) statement must be on a line of its own.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.