Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
imageapi_optimize_avif_webp
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_avif_webp
Commits
a7802f49
Commit
a7802f49
authored
10 months ago
by
hoangd
Browse files
Options
Downloads
Patches
Plain Diff
version 1.1.4
parent
9ad7296b
No related branches found
No related tags found
No related merge requests found
Pipeline
#200791
failed
10 months ago
Stage: build
Stage: test
Stage: dast
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/ImageapiOptimizeAvifWebpHelper.php
+2
-2
2 additions, 2 deletions
src/ImageapiOptimizeAvifWebpHelper.php
src/Plugin/ImageAPIOptimizeProcessor/AvifWebp.php
+28
-38
28 additions, 38 deletions
src/Plugin/ImageAPIOptimizeProcessor/AvifWebp.php
with
30 additions
and
40 deletions
src/ImageapiOptimizeAvifWebpHelper.php
+
2
−
2
View file @
a7802f49
...
...
@@ -16,8 +16,8 @@ class ImageapiOptimizeAvifWebpHelper {
* @return string
* URI of the image without extension part.
*/
public
function
extensionlessUri
(
string
$original_uri
):
string
{
return
substr
(
$original_uri
,
0
,
strrpos
(
$original_uri
,
'.'
))
;
public
function
extensionlessUri
(
string
$original_uri
):
string
{
return
$original_uri
;
}
}
This diff is collapsed.
Click to expand it.
src/Plugin/ImageAPIOptimizeProcessor/AvifWebp.php
+
28
−
38
View file @
a7802f49
...
...
@@ -28,16 +28,16 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
if
(
$path_info
[
'extension'
]
==
'avif'
||
$path_info
[
'extension'
]
==
'webp'
)
{
return
FALSE
;
}
$toolkit_id
=
$this
->
imageFactory
->
getToolkitId
();
$toolkit_id
=
$this
->
imageFactory
->
getToolkitId
();
$source_image
=
$this
->
imageFactory
->
get
(
$image_uri
,
$toolkit_id
);
$avif_quality
=
$this
->
configuration
[
'avif_quality'
];
$webp_quality
=
$this
->
configuration
[
'webp_quality'
];
$extensionless_image_uri
=
\Drupal
::
service
(
'imageapi_optimize_avif_webp.helper'
)
->
extensionlessUri
(
$image_uri
);
/*********** Webp image ****************/
$source_image
=
$this
->
imageFactory
->
get
(
$image_uri
,
$toolkit_id
);
if
(
$source_image
->
isValid
())
{
$destination
=
$extensionless_image_uri
.
'.webp'
;
if
(
$source_image
)
{
/*********** Webp image ****************/
$destination
=
$extensionless_image_uri
.
'.webp'
;
// Check if ImageMagick supports webp.
if
(
$toolkit_id
==
'imagemagick'
&&
in_array
(
'webp'
,
$this
->
imageFactory
->
getSupportedExtensions
()))
{
try
{
...
...
@@ -45,10 +45,11 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
$source_image
->
save
(
$destination
);
// Fix issue where sometimes image fails to generate.
if
(
file_exists
(
$destination
)
&&
filesize
(
$destination
)
%
2
==
1
)
{
if
(
filesize
(
$destination
)
%
2
==
1
)
{
file_put_contents
(
$destination
,
"
\0
"
,
FILE_APPEND
);
}
//return TRUE;
}
catch
(
\Exception
$error
)
{
// Something else went wrong, unrelated to the Tinify API.
...
...
@@ -59,35 +60,26 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
else
{
imagewebp
(
$source_image
->
getToolkit
()
->
getResource
(),
$destination
,
$webp_quality
);
// Fix issue where sometimes image fails to generate.
if
(
file_exists
(
$destination
)
&&
filesize
(
$destination
)
%
2
==
1
)
{
if
(
filesize
(
$destination
)
%
2
==
1
)
{
file_put_contents
(
$destination
,
"
\0
"
,
FILE_APPEND
);
}
}
}
/*********** Avif image ****************/
$file_extension
=
strtolower
(
substr
(
strrchr
(
$image_uri
,
'.'
),
1
));
$temp_image_uri
=
'temporary://image_api_optimize_'
.
Crypt
::
randomBytesBase64
(
8
)
.
'.'
.
$file_extension
;
$temp_image_uri
=
\Drupal
::
service
(
'file_system'
)
->
copy
(
$image_uri
,
$temp_image_uri
,
FileSystemInterface
::
EXISTS_RENAME
);
$source_avif
=
$this
->
imageFactory
->
get
(
$temp_image_uri
,
$toolkit_id
);
if
(
$source_avif
->
isValid
())
{
/*********** Avif image ****************/
$destination
=
$extensionless_image_uri
.
'.avif'
;
// Check if ImageMagick supports avif.
if
(
$toolkit_id
==
'imagemagick'
&&
in_array
(
'avif'
,
$this
->
imageFactory
->
getSupportedExtensions
()))
{
try
{
$abs_image_uri
=
\Drupal
::
service
(
'file_system'
)
->
realpath
(
$image_uri
);
$abs_destination
=
\Drupal
::
service
(
'file_system'
)
->
realpath
(
$destination
);
exec
(
'convert '
.
$abs_image_uri
.
' -density 72 -units PixelsPerInch -quality '
.
$avif_quality
.
' '
.
$abs_destination
);
$source_image
->
apply
(
'convert'
,
[
'extension'
=>
'avif'
,
'quality'
=>
$avif_quality
]);
$source_image
->
save
(
$destination
);
// Fix issue where sometimes image fails to generate.
if
(
file_exists
(
$abs_destination
)
&&
filesize
(
$
abs_
destination
)
%
2
==
1
)
{
file_put_contents
(
$
abs_
destination
,
"
\0
"
,
FILE_APPEND
);
if
(
filesize
(
$destination
)
%
2
==
1
)
{
file_put_contents
(
$destination
,
"
\0
"
,
FILE_APPEND
);
}
//return TRUE;
}
catch
(
\Exception
$error
)
{
// Something else went wrong, unrelated to the Tinify API.
$this
->
logger
->
error
(
'AVIF: Failed to optimize image using ImageMagick due to "%error".'
,
[
'%error'
=>
$error
->
getMessage
()]);
}
...
...
@@ -95,23 +87,21 @@ class AvifWebp extends ConfigurableImageAPIOptimizeProcessorBase {
// Go with GD.
else
{
try
{
if
(
function_exists
(
'imageavif'
)
)
{
imageavif
(
$source_avif
->
getToolkit
()
->
getResource
(),
$destination
,
$avif_quality
);
// Fix issue where sometimes image fails to generate.
if
(
file_exists
(
$destination
)
&&
filesize
(
$destination
)
%
2
==
1
)
{
file_put_contents
(
$destination
,
"
\0
"
,
FILE_APPEND
);
}
}
else
{
$this
->
logger
->
error
(
'AVIF: function imageavif does not exist. https://www.php.net/manual/en/function.imageavif.php'
);
imageavif
(
$source_image
->
getToolkit
()
->
getResource
(),
$destination
,
$avif_quality
);
// Fix issue where sometimes image fails to generate.
if
(
filesize
(
$destination
)
%
2
==
1
)
{
file_put_contents
(
$destination
,
"
\0
"
,
FILE_APPEND
);
}
//return TRUE;
}
catch
(
\Exception
$error
)
{
// Something else went wrong, unrelated to the Tinify API.
$this
->
logger
->
error
(
'AVIF: Failed to optimize image using GD due to "%error".'
,
[
'%error'
=>
$error
->
getMessage
()]);
}
}
...
...
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