Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PELock committed Jan 19, 2022
1 parent 42b40cf commit f1afc50
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ The generated output looks like this:

Use it to make your website loading times even faster.

## Automatic WebP generation for updated images (new in v1.2.0)

ImgOpt will set the modification date of the generated WebP image to match the modification date of the original image file.

If ImgOpt detects that a file modification date of the source image file is different than the date of the previously generated WebP image file - it will automatically re-create the new WebP image file!

## Installation

The preferred way to install the library is through the [composer](https://getcomposer.org/).
Expand Down Expand Up @@ -166,7 +172,4 @@ And it will generate this HTML code:

Hope you like it. For questions, bug & feature requests visit my site:

Bartosz Wójcik | https://www.pelock.com



Bartosz Wójcik | https://www.pelock.com
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pelock/yii2-imgopt",
"version": "1.1.2",
"version": "1.2.0",
"description": "Image optimization widget for Yii2 Framework with auto WebP image format generation from PNG/JPG files.",
"type": "yii2-extension",
"keywords": ["image", "optimization", "yii2", "webp", "jpg", "png", "generator"],
Expand Down
30 changes: 28 additions & 2 deletions src/ImgOpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ private function get_or_convert_to_webp($img, $recreate = false)
return null;
}

// modification time of the original image
$img_modification_time = filemtime($img_full_path);

$original_file_size = filesize($img_full_path);

if ($original_file_size === 0)
Expand All @@ -208,15 +211,29 @@ private function get_or_convert_to_webp($img, $recreate = false)
$webp_short_path = $short_file_info["dirname"] . "/" . $webp_filename_with_extension;
$webp_full_path = $file_info["dirname"] . "/" . $webp_filename_with_extension;


// if the WEBP file already exists check if we want to re-create it
if ($recreate === false && file_exists($webp_full_path))
{

// if the WEBP file is bigger than the original image
// use the original image
if (filesize($webp_full_path) >= $original_file_size)
{
return null;
}

return $webp_short_path;
$webp_modification_time = filemtime($webp_full_path);

// if the modification dates on the original image
// and WEBP image are the same = use the WEBP image
// in any other case - recreate the file
if ($img_modification_time !== false && $webp_modification_time !== false)
{
if ($img_modification_time === $webp_modification_time)
{
return $webp_short_path;
}
}
}

if ($ext === "png")
Expand Down Expand Up @@ -253,6 +270,15 @@ private function get_or_convert_to_webp($img, $recreate = false)
// release input image
imagedestroy($img);


// set modification time on the WEBP file to match the
// modification time of the original image
if ($img_modification_time !== false)
{
touch($webp_full_path, $img_modification_time);
}


// if the final WEBP image is bigger than the original file
// don't use it (use the original only)
if (filesize($webp_full_path) >= $original_file_size)
Expand Down

0 comments on commit f1afc50

Please sign in to comment.