Commit 4743d27c authored by Tino Goratsch's avatar Tino Goratsch

use the fast-image-size library to get the dimensions of a picture

parent 802fbfea
......@@ -2,6 +2,7 @@
namespace ACP3\Core;
use ACP3\Core\Environment\ApplicationPath;
use FastImageSize\FastImageSize;
use Symfony\Component\HttpFoundation\Response;
/**
......@@ -50,6 +51,10 @@ class Picture
*/
protected $forceResample = false;
/**
* @var FastImageSize
*/
private $fastImageSize;
/**
* @var \Symfony\Component\HttpFoundation\Response
*/
......@@ -69,15 +74,18 @@ class Picture
protected $image;
/**
* @param FastImageSize $fastImageSize
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \ACP3\Core\Environment\ApplicationPath $appPath
* @param string $environment
* @param \ACP3\Core\Environment\ApplicationPath $appPath
* @param string $environment
*/
public function __construct(
FastImageSize $fastImageSize,
Response $response,
ApplicationPath $appPath,
$environment
) {
$this->fastImageSize = $fastImageSize;
$this->response = $response;
$this->appPath = $appPath;
$this->environment = $environment;
......@@ -216,12 +224,13 @@ class Picture
{
if (is_file($this->file) === true) {
$cacheFile = $this->getCacheFileName();
$picInfo = getimagesize($this->file);
$width = $picInfo[0];
$height = $picInfo[1];
$type = $picInfo[2];
$this->setHeaders($picInfo['mime']);
$picInfo = $this->fastImageSize->getImageSize($this->file);
$width = $picInfo['width'];
$height = $picInfo['height'];
$type = $picInfo['type'];
$this->setHeaders($this->getMimeType($type));
// Direct output of the picture, if it is already cached
if ($this->enableCache === true && is_file($cacheFile) === true) {
......@@ -250,6 +259,24 @@ class Picture
return false;
}
/**
* @param int $pictureType
* @return string
*/
private function getMimeType($pictureType)
{
switch($pictureType) {
case IMAGETYPE_GIF:
return 'image/gif';
case IMAGETYPE_JPEG:
return 'image/jpeg';
case IMAGETYPE_PNG:
return 'image/png';
}
return '';
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
......@@ -325,17 +352,17 @@ class Picture
{
$this->image = imagecreatetruecolor($newWidth, $newHeight);
switch ($type) {
case 1:
case IMAGETYPE_GIF:
$origPicture = imagecreatefromgif($this->file);
$this->scalePicture($newWidth, $newHeight, $width, $height, $origPicture);
imagegif($this->image, $cacheFile);
break;
case 2:
case IMAGETYPE_JPEG:
$origPicture = imagecreatefromjpeg($this->file);
$this->scalePicture($newWidth, $newHeight, $width, $height, $origPicture);
imagejpeg($this->image, $cacheFile, $this->jpgQuality);
break;
case 3:
case IMAGETYPE_PNG:
imagealphablending($this->image, false);
$origPicture = imagecreatefrompng($this->file);
$this->scalePicture($newWidth, $newHeight, $width, $height, $origPicture);
......@@ -382,7 +409,8 @@ class Picture
*/
protected function resamplingIsNecessary($width, $height, $type)
{
return ($this->forceResample === true || ($width > $this->maxWidth || $height > $this->maxHeight)) && ($type === 1 || $type === 2 || $type === 3);
return ($this->forceResample === true || ($width > $this->maxWidth || $height > $this->maxHeight))
&& in_array($type, [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG]);
}
/**
......
......@@ -21,6 +21,7 @@
"fisharebest/localization": "^1.10",
"giggsey/locale": "^1.2",
"inlinestyle/inlinestyle": "^1.2",
"marc1706/fast-image-size": "^1.1",
"monolog/monolog": "^1.21",
"mrclay/minify": "^3.0",
"patchwork/utf8": "^1.3",
......
......@@ -45,9 +45,13 @@ services:
core.environment.application_path:
synthetic: true
fast_image_size:
class: FastImageSize\FastImageSize
core.image:
class: ACP3\Core\Picture
arguments:
- '@fast_image_size'
- '@core.http.response'
- '@core.environment.application_path'
- '%core.environment%'
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "74a9b50cd384fc42e70c683f3440d640",
"content-hash": "5c8ec35ca079fe8f2d4ab44841e63e5d",
"packages": [
{
"name": "ckeditor/ckeditor",
......@@ -1077,6 +1077,57 @@
],
"time": "2017-01-29T12:04:52+00:00"
},
{
"name": "marc1706/fast-image-size",
"version": "v1.1.3",
"source": {
"type": "git",
"url": "https://github.com/marc1706/fast-image-size.git",
"reference": "5f7e8377746524e2b8a49a631c1fc9afeb9d8bee"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/marc1706/fast-image-size/zipball/5f7e8377746524e2b8a49a631c1fc9afeb9d8bee",
"reference": "5f7e8377746524e2b8a49a631c1fc9afeb9d8bee",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "4.*"
},
"type": "library",
"autoload": {
"psr-4": {
"FastImageSize\\": "lib",
"FastImageSize\\tests\\": "tests"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Marc Alexander",
"email": "admin@m-a-styles.de",
"homepage": "https://www.m-a-styles.de",
"role": "Developer"
}
],
"description": "fast-image-size is a PHP library that does almost everything PHP's getimagesize() does but without the large overhead of downloading the complete file.",
"homepage": "https://www.m-a-styles.de",
"keywords": [
"fast",
"getimagesize",
"image",
"imagesize",
"php",
"size"
],
"time": "2017-03-26T12:48:28+00:00"
},
{
"name": "mibe/feedwriter",
"version": "v1.1.1",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment