Skip to content

Commit 55a327e

Browse files
committed
Small improvements
- Memory leak is fixed in vendor, see: Intervention/image#426 - Loading time is still very long (previous commit did not fix it)
1 parent 200c7a7 commit 55a327e

File tree

10 files changed

+130
-101
lines changed

10 files changed

+130
-101
lines changed

Diff for: public/index.php

+78-62
Large diffs are not rendered by default.

Diff for: public/js/imagesweserv.js

-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ $(function() {
222222
$('a[rel="lightbox"]').featherlight({
223223
root: 'section#body'
224224
});
225-
$('.lazy').lazy();
226225
});
227226

228227
jQuery.extend({

Diff for: src/Api/Api.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AndriesLouw\imagesweserv\Exception\ImageTooLargeException;
77
use AndriesLouw\imagesweserv\Manipulators\ManipulatorInterface;
88
use GuzzleHttp\Exception\RequestException;
9+
use ImagickException;
910
use Intervention\Image\Exception\InvalidArgumentException;
1011
use Intervention\Image\Exception\NotReadableException;
1112
use Intervention\Image\Exception\RuntimeException;
@@ -117,7 +118,8 @@ public function setManipulators(array $manipulators)
117118
* @throws NotReadableException if the provided file can not be read
118119
* @throws ImageTooLargeException if the provided image is too large for processing.
119120
* @throws RequestException for errors that occur during a transfer or during the on_headers event
120-
* @return \Intervention\Image\Image
121+
* @throws ImagickException for errors that occur during image manipulation
122+
* @return string Manipulated image binary data.
121123
*/
122124
public function run($url, array $params)
123125
{
@@ -144,9 +146,17 @@ public function run($url, array $params)
144146
trigger_error($e->getMessage() . ' URL: ' . $url . ' Params: ' . implode(', ', $params),
145147
E_USER_WARNING);
146148
throw $e;
149+
} catch (ImagickException $e) {
150+
trigger_error($e->getMessage() . ' URL: ' . $url . ' Params: ' . implode(', ', $params),
151+
E_USER_WARNING);
152+
throw $e;
147153
}
154+
155+
//gc_collect_cycles();
148156
}
149157

150-
return $image;
158+
$image->destroy();
159+
160+
return $image->getEncoded();
151161
}
152162
}

Diff for: src/Api/ApiInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ interface ApiInterface
66
{
77
/**
88
* Perform image manipulations.
9-
* @param string $source Source image binary data.
9+
* @param string $url Source URL
1010
* @param array $params The manipulation params.
1111
* @return string Manipulated image binary data.
1212
*/
13-
public function run($source, array $params);
13+
public function run($url, array $params);
1414
}

Diff for: src/Client.php

-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public function getFileName()
126126
/**
127127
* @param string $url
128128
* @throws RequestException for errors that occur during a transfer or during the on_headers event
129-
* @internal param \GuzzleHttp\Client
130129
*
131130
* @return string File name
132131
*/

Diff for: src/Manipulators/BaseManipulator.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ abstract class BaseManipulator implements ManipulatorInterface
1919
public function setParams(array $params)
2020
{
2121
$this->params = $params;
22-
23-
return $this;
2422
}
2523

2624
/**
@@ -31,7 +29,7 @@ public function setParams(array $params)
3129
public function __get($name)
3230
{
3331
if (array_key_exists($name, $this->params)) {
34-
return ($this->params[$name] != null) ? $this->params[$name] : 'null';
32+
return ($this->params[$name] != null) ? $this->params[$name]: '';
3533
}
3634
}
3735

Diff for: src/Manipulators/Encode.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Intervention\Image\Image;
66

77
/**
8-
* @property string $fm
8+
* @property string $output
99
* @property string $q
1010
*/
1111
class Encode extends BaseManipulator
@@ -30,6 +30,7 @@ public function run(Image $image)
3030
$image->interlace();
3131
}
3232

33+
// Memory leak, see: https://github.com/Intervention/image/issues/426
3334
return $image->encode($format, $quality);
3435
}
3536

Diff for: src/Manipulators/Size.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
/**
99
* @property string $dpr
10-
* @property string $fit
10+
* @property string $t
11+
* @property string $a
1112
* @property string $h
1213
* @property string $w
1314
*/

Diff for: src/Manipulators/Trim.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function run(Image $image)
3131
*/
3232
public function getTrim()
3333
{
34-
if ($this->trim === 'null') {
34+
if ($this->trim === '') {
3535
return 10;
3636
}
3737

Diff for: src/Server.php

+32-27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AndriesLouw\imagesweserv\Api\ApiInterface;
66
use AndriesLouw\imagesweserv\Exception\ImageTooLargeException;
77
use GuzzleHttp\Exception\RequestException;
8+
use ImagickException;
89
use Intervention\Image\Exception\NotReadableException;
910

1011
class Server
@@ -97,13 +98,34 @@ public function setPresets(array $presets)
9798
* @throws NotReadableException if the provided file can not be read
9899
* @throws ImageTooLargeException if the provided image is too large for processing.
99100
* @throws RequestException for errors that occur during a transfer or during the on_headers event
100-
* @return \Intervention\Image\Image
101+
* @throws ImagickException for errors that occur during image manipulation
102+
* @return string Manipulated image binary data.
101103
*/
102104
public function outputImage($url, array $params)
103105
{
104106
return $this->api->run($url, $this->getAllParams($params));
105107
}
106108

109+
/**
110+
* Get all image manipulations params, including defaults and presets.
111+
* @param array $params Image manipulation params.
112+
* @return array All image manipulation params.
113+
*/
114+
public function getAllParams(array $params)
115+
{
116+
$all = $this->defaults;
117+
118+
if (isset($params['p'])) {
119+
foreach (explode(',', $params['p']) as $preset) {
120+
if (isset($this->presets[$preset])) {
121+
$all = array_merge($all, $this->presets[$preset]);
122+
}
123+
}
124+
}
125+
126+
return array_merge($all, $params);
127+
}
128+
107129
/**
108130
* Get the current mime type after encoding
109131
* we cannot use directly $image->mime() because
@@ -112,15 +134,18 @@ public function outputImage($url, array $params)
112134
*
113135
* @param string $mimeType the mime which the user wants to format to
114136
* @param string $mimeTypeImage the initial mime type before encoding
137+
* @param array $allowed allowed extensions
115138
* @return string
116139
*/
117-
public function getCurrentMimeType($mimeType, $mimeTypeImage)
140+
public function getCurrentMimeType($mimeType, $mimeTypeImage, $allowed = null)
118141
{
119-
$allowed = [
120-
'gif' => 'image/gif',
121-
'jpg' => 'image/jpeg',
122-
'png' => 'image/png',
123-
];
142+
if (is_null($allowed)) {
143+
$allowed = [
144+
'gif' => 'image/gif',
145+
'jpg' => 'image/jpeg',
146+
'png' => 'image/png',
147+
];
148+
}
124149

125150
if (array_key_exists($mimeType, $allowed)) {
126151
return $allowed[$mimeType];
@@ -132,24 +157,4 @@ public function getCurrentMimeType($mimeType, $mimeTypeImage)
132157

133158
return 'image/jpeg';
134159
}
135-
136-
/**
137-
* Get all image manipulations params, including defaults and presets.
138-
* @param array $params Image manipulation params.
139-
* @return array All image manipulation params.
140-
*/
141-
public function getAllParams(array $params)
142-
{
143-
$all = $this->defaults;
144-
145-
if (isset($params['p'])) {
146-
foreach (explode(',', $params['p']) as $preset) {
147-
if (isset($this->presets[$preset])) {
148-
$all = array_merge($all, $this->presets[$preset]);
149-
}
150-
}
151-
}
152-
153-
return array_merge($all, $params);
154-
}
155160
}

0 commit comments

Comments
 (0)