Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
imageapi_optimize
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
imageapi_optimize
Commits
60c8dd1b
Commit
60c8dd1b
authored
8 years ago
by
Steven Jones
Committed by
Steven Jones
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2805029
by Steven Jones: Bugfix reSmush.it implementation
parent
68fa1bc7
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/ImageAPIOptimizeProcessor/reSmushit.php
+57
-18
57 additions, 18 deletions
src/Plugin/ImageAPIOptimizeProcessor/reSmushit.php
with
57 additions
and
18 deletions
src/Plugin/ImageAPIOptimizeProcessor/reSmushit.php
+
57
−
18
View file @
60c8dd1b
...
...
@@ -6,9 +6,12 @@ use Drupal\Core\Form\FormStateInterface;
use
Drupal\Core\Image\ImageInterface
;
use
Drupal\imageapi_optimize
\ConfigurableImageAPIOptimizeProcessorBase
;
use
Drupal\imageapi_optimize
\ImageAPIOptimizeProcessorBase
;
use
GuzzleHttp\ClientInterface
;
use
Psr\Log\LoggerInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
*
Desaturates (grayscale) an image resourc
e.
*
Uses the resmush.it webservice to optimize an imag
e.
*
* @ImageAPIOptimizeProcessor(
* id = "resmushit",
...
...
@@ -18,13 +21,42 @@ use Drupal\imageapi_optimize\ImageAPIOptimizeProcessorBase;
*/
class
reSmushit
extends
ConfigurableImageAPIOptimizeProcessorBase
{
public
function
applyToImage
(
$image_uri
)
{
// @TODO: Catch exceptions, better error handling etc.
/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\ClientInterface
*/
protected
$httpClient
;
/**
* {@inheritdoc}
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
LoggerInterface
$logger
,
ClientInterface
$http_client
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$logger
);
$this
->
httpClient
=
$http_client
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
$p
=
$container
->
get
(
'http_client'
);
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'logger.factory'
)
->
get
(
'imageapi_optimize'
),
$container
->
get
(
'http_client'
)
);
}
/**
* {@inheritdoc}
*/
public
function
applyToImage
(
$image_uri
)
{
// Need to send the file off to the remote service and await a response.
$client
=
\Drupal
::
httpClient
();
$fields
=
[];
$fields
[]
=
[
'name'
=>
'files'
,
'contents'
=>
fopen
(
$image_uri
,
'r'
),
...
...
@@ -36,17 +68,23 @@ class reSmushit extends ConfigurableImageAPIOptimizeProcessorBase {
];
}
$response
=
$client
->
post
(
'http://api.resmush.it/ws.php'
,
[
'multipart'
=>
$fields
]);
$body
=
$response
->
getBody
();
$json
=
json_decode
(
$body
);
// If this has worked, we should get a dest entry in the JSON returned.
if
(
isset
(
$json
->
dest
))
{
// Now go fetch that, and save it locally.
$smushedFile
=
$client
->
get
(
$json
->
dest
);
if
(
$smushedFile
->
getStatusCode
()
==
200
)
{
file_unmanaged_save_data
(
$smushedFile
->
getBody
(),
$image_uri
,
FILE_EXISTS_REPLACE
);
try
{
$response
=
$this
->
httpClient
->
post
(
'http://api.resmush.it/ws.php'
,
[
'multipart'
=>
$fields
]);
$body
=
$response
->
getBody
();
$json
=
json_decode
(
$body
);
// If this has worked, we should get a dest entry in the JSON returned.
if
(
isset
(
$json
->
dest
))
{
// Now go fetch that, and save it locally.
$smushedFile
=
$this
->
httpClient
->
get
(
$json
->
dest
);
if
(
$smushedFile
->
getStatusCode
()
==
200
)
{
file_unmanaged_save_data
(
$smushedFile
->
getBody
(),
$image_uri
,
FILE_EXISTS_REPLACE
);
return
TRUE
;
}
}
return
TRUE
;
}
catch
(
RequestException
$e
)
{
$this
->
logger
->
error
(
'Failed to download optimize image using reSmush.it due to "%error".'
,
array
(
'%error'
=>
$e
->
getMessage
()));
}
return
FALSE
;
...
...
@@ -56,9 +94,9 @@ class reSmushit extends ConfigurableImageAPIOptimizeProcessorBase {
* {@inheritdoc}
*/
public
function
defaultConfiguration
()
{
return
array
(
return
[
'quality'
=>
NULL
,
)
;
]
;
}
/**
...
...
@@ -67,7 +105,8 @@ class reSmushit extends ConfigurableImageAPIOptimizeProcessorBase {
public
function
buildConfigurationForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$form
[
'quality'
]
=
array
(
'#type'
=>
'number'
,
'#title'
=>
t
(
'JPG image quality'
),
'#title'
=>
$this
->
t
(
'JPEG image quality'
),
'#description'
=>
$this
->
t
(
'Optionally specify a quality setting when optimizing JPEG images.'
),
'#default_value'
=>
$this
->
configuration
[
'quality'
],
'#min'
=>
1
,
'#max'
=>
100
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment