Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
- Memory leak is fixed in vendor, see:
Intervention/image#426
- Loading time is still very long (previous commit did not fix it)
  • Loading branch information
kleisauke committed Dec 29, 2015
1 parent 200c7a7 commit 55a327e
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 101 deletions.
140 changes: 78 additions & 62 deletions public/index.php

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion public/js/imagesweserv.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ $(function() {
$('a[rel="lightbox"]').featherlight({
root: 'section#body'
});
$('.lazy').lazy();
});

jQuery.extend({
Expand Down
14 changes: 12 additions & 2 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AndriesLouw\imagesweserv\Exception\ImageTooLargeException;
use AndriesLouw\imagesweserv\Manipulators\ManipulatorInterface;
use GuzzleHttp\Exception\RequestException;
use ImagickException;
use Intervention\Image\Exception\InvalidArgumentException;
use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Exception\RuntimeException;
Expand Down Expand Up @@ -117,7 +118,8 @@ public function setManipulators(array $manipulators)
* @throws NotReadableException if the provided file can not be read
* @throws ImageTooLargeException if the provided image is too large for processing.
* @throws RequestException for errors that occur during a transfer or during the on_headers event
* @return \Intervention\Image\Image
* @throws ImagickException for errors that occur during image manipulation
* @return string Manipulated image binary data.
*/
public function run($url, array $params)
{
Expand All @@ -144,9 +146,17 @@ public function run($url, array $params)
trigger_error($e->getMessage() . ' URL: ' . $url . ' Params: ' . implode(', ', $params),
E_USER_WARNING);
throw $e;
} catch (ImagickException $e) {
trigger_error($e->getMessage() . ' URL: ' . $url . ' Params: ' . implode(', ', $params),
E_USER_WARNING);
throw $e;
}

//gc_collect_cycles();
}

return $image;
$image->destroy();

return $image->getEncoded();
}
}
4 changes: 2 additions & 2 deletions src/Api/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ interface ApiInterface
{
/**
* Perform image manipulations.
* @param string $source Source image binary data.
* @param string $url Source URL
* @param array $params The manipulation params.
* @return string Manipulated image binary data.
*/
public function run($source, array $params);
public function run($url, array $params);
}
1 change: 0 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function getFileName()
/**
* @param string $url
* @throws RequestException for errors that occur during a transfer or during the on_headers event
* @internal param \GuzzleHttp\Client
*
* @return string File name
*/
Expand Down
4 changes: 1 addition & 3 deletions src/Manipulators/BaseManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ abstract class BaseManipulator implements ManipulatorInterface
public function setParams(array $params)
{
$this->params = $params;

return $this;
}

/**
Expand All @@ -31,7 +29,7 @@ public function setParams(array $params)
public function __get($name)
{
if (array_key_exists($name, $this->params)) {
return ($this->params[$name] != null) ? $this->params[$name] : 'null';
return ($this->params[$name] != null) ? $this->params[$name]: '';
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Intervention\Image\Image;

/**
* @property string $fm
* @property string $output
* @property string $q
*/
class Encode extends BaseManipulator
Expand All @@ -30,6 +30,7 @@ public function run(Image $image)
$image->interlace();
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/Manipulators/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

/**
* @property string $dpr
* @property string $fit
* @property string $t
* @property string $a
* @property string $h
* @property string $w
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Manipulators/Trim.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function run(Image $image)
*/
public function getTrim()
{
if ($this->trim === 'null') {
if ($this->trim === '') {
return 10;
}

Expand Down
59 changes: 32 additions & 27 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AndriesLouw\imagesweserv\Api\ApiInterface;
use AndriesLouw\imagesweserv\Exception\ImageTooLargeException;
use GuzzleHttp\Exception\RequestException;
use ImagickException;
use Intervention\Image\Exception\NotReadableException;

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

/**
* Get all image manipulations params, including defaults and presets.
* @param array $params Image manipulation params.
* @return array All image manipulation params.
*/
public function getAllParams(array $params)
{
$all = $this->defaults;

if (isset($params['p'])) {
foreach (explode(',', $params['p']) as $preset) {
if (isset($this->presets[$preset])) {
$all = array_merge($all, $this->presets[$preset]);
}
}
}

return array_merge($all, $params);
}

/**
* Get the current mime type after encoding
* we cannot use directly $image->mime() because
Expand All @@ -112,15 +134,18 @@ public function outputImage($url, array $params)
*
* @param string $mimeType the mime which the user wants to format to
* @param string $mimeTypeImage the initial mime type before encoding
* @param array $allowed allowed extensions
* @return string
*/
public function getCurrentMimeType($mimeType, $mimeTypeImage)
public function getCurrentMimeType($mimeType, $mimeTypeImage, $allowed = null)
{
$allowed = [
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'png' => 'image/png',
];
if (is_null($allowed)) {
$allowed = [
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'png' => 'image/png',
];
}

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

return 'image/jpeg';
}

/**
* Get all image manipulations params, including defaults and presets.
* @param array $params Image manipulation params.
* @return array All image manipulation params.
*/
public function getAllParams(array $params)
{
$all = $this->defaults;

if (isset($params['p'])) {
foreach (explode(',', $params['p']) as $preset) {
if (isset($this->presets[$preset])) {
$all = array_merge($all, $this->presets[$preset]);
}
}
}

return array_merge($all, $params);
}
}

0 comments on commit 55a327e

Please sign in to comment.