This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresize-images.php
170 lines (142 loc) · 5.3 KB
/
resize-images.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
require_once 'adapters/imagick.php';
require_once 'adapters/gd.php';
/**
* Class ResizeImagesPlugin
* @package Grav\Plugin
*/
class ResizeImagesPlugin extends Plugin
{
/**
* @var string
*/
protected $adapter;
/**
* @var array
*/
protected $sizes;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onAdminSave' => ['onAdminSave', 0]
];
}
/**
* Determine whether a particular dependency is installed.
* @param string $adapter Either 'gd' or 'imagick'
* @return bool
*/
protected function dependencyCheck($adapter = 'gd')
{
if ($adapter === 'gd') {
return extension_loaded('gd');
}
if ($adapter === 'imagick') {
return class_exists('\Imagick');
}
}
/**
* Determine which adapter is preferred and whether or not it's available.
* Construct an instance of that adapter and return it.
* @param string $source - Source image path
* @return mixed - Either an instance of ImagickAdapter, GDAdapter or false if none of the extensions were available
*/
protected function getImageAdapter($source)
{
$imagick_exists = $this->dependencyCheck('imagick');
$gd_exists = $this->dependencyCheck('gd');
if ($this->adapter === 'imagick') {
if ($imagick_exists) {
return new ImagickAdapter($source);
} else if ($gd_exists) {
return new GDAdapter($source);
}
} else if ($this->adapter === 'gd') {
if ($gd_exists) {
return new GDAdapter($source);
} else if ($imagick_exists) {
return new ImagickAdapter($source);
}
}
}
/**
* Resizes an image using either Imagick or GD
* @param string $source - Source image path
* @param string $target - Target image path
* @param float $width - Target width
* @param float $height - Target height
* @param int [$quality=95] - Compression quality for target image
* @return bool - Returns true on success, otherwise false
*/
protected function resizeImage($source, $target, $width, $height, $quality = 95)
{
$adapter = $this->getImageAdapter($source);
$adapter->resize($width, $height);
$adapter->setQuality($quality);
return $adapter->save($target);
}
/**
* Called when a page is saved from the admin plugin. Will generate
* responsive image alternatives for image that don't have any.
*/
public function onAdminSave($event)
{
$page = $event['object'];
if (!$page instanceof Page) {
return false;
}
if (!$this->dependencyCheck('imagick') && !$this->dependencyCheck('gd')) {
$this->grav['admin']->setMessage('Neither Imagick nor GD seem to be installed. The resize-images plugin needs one of them to work.', 'warning');
return;
}
$this->sizes = (array) $this->config->get('plugins.resize-images.sizes');
$this->adapter = $this->config->get('plugins.resize-images.adapter', 'imagick');
foreach ($page->media()->images() as $filename => $medium) {
$srcset = $medium->srcset(false);
if ($srcset != '') {
continue;
}
// We can't rely on the path returned from the image's own path
// method, since it points to the directory where the image is saved
// rather than where the original is stored. This means it could
// point to the global image cache directory.
$page_path = $page->path();
$source_path = "$page_path/$filename";
$info = pathinfo($source_path);
$count = 0;
foreach ($this->sizes as $i => $size) {
if ($size['width'] >= $medium->width) {
continue;
}
$count++;
$dest_path = "{$info['dirname']}/{$info['filename']}@{$count}x.{$info['extension']}";
$width = $size['width'];
$quality = $size['quality'];
$height = ($width / $medium->width) * $medium->height;
$this->resizeImage($source_path, $dest_path, $width, $height, $quality, $medium->width, $medium->height);
}
$remove_original = $this->config->get('plugins.resize-images.remove_original');
if ($count > 0) {
$original_index = $count + 1;
if ($remove_original) {
unlink($source_path);
} else {
rename($source_path, "{$info['dirname']}/{$info['filename']}@{$original_index}x.{$info['extension']}");
}
rename("{$info['dirname']}/{$info['filename']}@1x.{$info['extension']}", $source_path);
}
$message = "Resized $filename $count times";
if ($remove_original) {
$message .= ' (and removed the original image)';
}
$this->grav['admin']->setMessage($message, 'info');
}
}
}