Skip to content

Commit

Permalink
Allow slight compression on held images
Browse files Browse the repository at this point in the history
  • Loading branch information
dnabeast committed Nov 7, 2023
1 parent 33cd4c5 commit f37ea8e
Show file tree
Hide file tree
Showing 39 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ The inline backgrounds can be turned off. If you need to add style tags on your
If you need to test to make sure the correct image is being displayed turning this to true will write the filename onto the image itself.
**Beware:** Any files created with this flag on will keep this label once you turn it off again. The images should be deleted so that they can be recreated.

### 'compress_held_image' => env('BLADE_CROP_COMPRESS_HELD_IMAGE', true)

The package creates and stores a Held Image that it uses for reference. If the original image vanishes for some reason there is always a source of truth. If this image is not compressed at all we can ensure that it's at least stored as slightly lossy else your storage fills up with lossless images.

## 'render_source_tag_if_unavailable' => env('BLADE_CROP_RENDER_SOURCE', false),

When developing if can be useful to see the source tags even when an error has occurred. In production you can turn this off. If you rely on JS to detect image errors turn this off.
Expand Down
1 change: 1 addition & 0 deletions config/bladeimagecrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'pixel_device_ratios' => ['1x', '2x'], // add multipliers here for ultra high def screens
'backgrounds' => true,
'text_labels' => env('BLADE_CROP_TEST_LABELS', false), // These labels get written to the created images if they're not yet created.
'compress_held_image' => env('BLADE_CROP_COMPRESS_HELD_IMAGE', true), // converts lossless photos to slightly compressed images to save space
'render_source_tag_if_unavailable' => env('BLADE_CROP_RENDER_SOURCE', false),
'build_classes' => [
// 'avif' => 'DNABeast\BladeImageCrop\Builder\IM_AVIFBuilder',
Expand Down
23 changes: 21 additions & 2 deletions src/HoldImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function path(){
}

public function file(){
$formattedFileName = $this->src->slug.'.'.$this->src->explode('.')->last();

$extension = $this->src->explode('.')->last();
$formattedFileName = $this->src->slug.'.'.$extension;
// if file exists then return it
if ( $this->storageDisk->has( 'blade_image_crop_holding/'.$formattedFileName ) ){
return 'blade_image_crop_holding/'.$formattedFileName;
Expand All @@ -40,6 +40,25 @@ public function file(){
return 'FILE NOT FOUND';
}

if ( config('bladeimagecrop.compress_held_image')??false ) {
$glob = imagecreatefromstring($file);
ob_start();
if ($extension == 'jpg' || $extension == 'jpeg') {
imagejpeg($glob, null, 95);
}
if($extension == 'png') {
imagepng($glob);
}
if($extension == 'webp') {
imagewebp($glob, null, 95);
}
$newFile = ob_get_contents();
if(strlen($newFile) < strlen($file)){
$file = $newFile;
}
ob_end_clean();
}

$this->storageDisk->put('blade_image_crop_holding/'.$formattedFileName, $file);

return 'blade_image_crop_holding/'.$formattedFileName;
Expand Down
27 changes: 26 additions & 1 deletion tests/Unit/HoldImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -110,4 +111,28 @@ function give_it_an_online_image_that_doesn_t_exists_returns_fail_message(){
$holdImage->file()
);
}
}

// if the config option to compress the hold image is set the held image should be smaller than the original image
/** @test */
function if_the_config_option_to_compress_the_hold_image_is_set_the_held_image_should_be_smaller_than_the_original_image(){
Config::set(['bladeimagecrop.compress_held_image' => true]);

$file = 'uploads/banners/page/shea.jpg';
$image = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.$file);

Storage::fake('public');
Storage::disk('public')->put($file, $image);

$originalSize = Storage::disk('public')->fileSize($file);

$holdImage = (new HoldImage($file))->file();

$heldSize = Storage::disk('public')->fileSize($holdImage);

$this->assertLessThan(
$originalSize,
$heldSize
);

}
}
Binary file added tests/Unit/blade_image_crop_holding/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/Unit/uploads/banners/page/shea.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f37ea8e

Please sign in to comment.