Skip to content

Commit

Permalink
Merge pull request #204 from TypiCMS/master
Browse files Browse the repository at this point in the history
PHPThumb replaced by Intervention Image
  • Loading branch information
sdebacker authored May 15, 2022
2 parents 67febc1 + 618964f commit 51892e4
Show file tree
Hide file tree
Showing 24 changed files with 376 additions and 612 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 BKWLD
Copyright (c) BKWLD

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Since 4.0, Croppa lets images be stored on remote disks like S3, Dropbox, FTP an

## Server Requirements:

- [gd](http://php.net/manual/en/book.image.php)
- [exif](http://php.net/manual/en/book.exif.php) - Required if you want to have Croppa auto-rotate images from devices like mobile phones based on exif meta data.
- [gd](http://php.net/manual/en/book.image.php)
- [exif](http://php.net/manual/en/book.exif.php) - Required if you want to have Croppa auto-rotate images from devices like mobile phones based on exif meta data.

### Nginx

Expand All @@ -46,7 +46,7 @@ Read the [source of the config file](https://github.com/BKWLD/croppa/blob/master
You can publish the config file into your app’s config directory, by running the following command:

```php
php artisan vendor:publish --tag=croppa
php artisan vendor:publish --tag=croppa-config
```

#### Local src and crops directories
Expand All @@ -55,8 +55,8 @@ The most common scenario, the src images and their crops are created in the defa

```php
return [
'src_dir' => 'public',
'crops_dir' => 'public',
'src_disk' => 'public',
'crops_disk' => 'public',
'path' => 'storage/(.*)$',
];
```
Expand All @@ -70,8 +70,8 @@ This is a good solution for a load balanced enviornment. Each app server will en
```php
// Croppa config.php
return [
'src_dir' => 's3',
'crops_dir' => 'public',
'src_disk' => 's3',
'crops_disk' => 'public',
'path' => 'storage/(.*)$',
];
```
Expand All @@ -86,11 +86,11 @@ The URL schema that Croppa uses is:

So these are all valid:

/uploads/image-300x200.png // Crop to fit in 300x200
/uploads/image-_x200.png // Resize to height of 200px
/uploads/image-300x_.png // Resize to width of 300px
/uploads/image-300x200-resize.png // Resize to fit within 300x200
/uploads/image-300x200-quadrant(T).png // See the quadrant description below
/storage/image-300x200.webp // Crop to fit in 300x200
/storage/image-_x200.webp // Resize to height of 200px
/storage/image-300x_.webp // Resize to width of 300px
/storage/image-300x200-resize.webp // Resize to fit within 300x200
/storage/image-300x200-quadrant(T).webp // See the quadrant description below

#### Croppa::url($url, $width, $height, $options)

Expand All @@ -99,31 +99,31 @@ To make preparing the URLs that Croppa expects an easier job, you can use the fo
```php
<img src="{{ Croppa::url($url, $width, $height, $options) }}" />
<!-- Examples (that would produce the URLs above) -->
<img src="{{ Croppa::url('/uploads/image.png', 300, 200) }}" />
<img src="{{ Croppa::url('/uploads/image.png', null, 200) }}" />
<img src="{{ Croppa::url('/uploads/image.png', 300) }}" />
<img src="{{ Croppa::url('/uploads/image.png', 300, 200, ['resize']) }}" />
<img src="{{ Croppa::url('/uploads/image.png', 300, 200, ['pad']) }}" />
<img src="{{ Croppa::url('/uploads/image.png', 300, 200, ['pad' => [45,168,147]]) }}" />
<img src="{{ Croppa::url('/uploads/image.png', 300, 200, ['quadrant' => 'T']) }}" />
<img src="{{ Croppa::url('storage/image.webp', 300, 200) }}" />
<img src="{{ Croppa::url('storage/image.webp', null, 200) }}" />
<img src="{{ Croppa::url('storage/image.webp', 300) }}" />
<img src="{{ Croppa::url('storage/image.webp', 300, 200, ['resize']) }}" />
<img src="{{ Croppa::url('storage/image.webp', 300, 200, ['pad']) }}" />
<img src="{{ Croppa::url('storage/image.webp', 300, 200, ['pad' => [45,168,147]]) }}" />
<img src="{{ Croppa::url('storage/image.webp', 300, 200, ['quadrant' => 'T']) }}" />
<!-- Or, if there were multiple arguments for the last example -->
<img src="{{ Croppa::url('/uploads/image.png', 300, 200, ['quadrant' => ['T']]) }}" />
```

These are the arguments that Croppa::url() takes:

- $url : The URL of your source image. The path to the image relative to the `src_dir` will be extracted using the `path` config regex.
- $width : A number or null for wildcard
- $height : A number or null for wildcard
- $options - An array of key value pairs, where the value is an optional array of arguments for the option. Supported option are:
- `resize` - Make the image fit in the provided width and height through resizing. When omitted, the default is to crop to fit in the bounds (unless one of sides is a wildcard).
- `pad` - Pad an image to desired dimensions. Moves the image into the center and fills the rest with given color. If no color is given, it will use white [255,255,255]
- `quadrant($quadrant)` - Crop the remaining overflow of an image using the passed quadrant heading. The supported `$quadrant` values are: `T` - Top (good for headshots), `B` - Bottom, `L` - Left, `R` - Right, `C` - Center (default). See the [PHPThumb documentation](https://github.com/masterexploder/PHPThumb/blob/master/src/PHPThumb/GD.php#L485) for more info.
- `trim($x1, $y1, $x2, $y2)` - Crop the source image to the size defined by the two sets of coordinates ($x1, $y1, ...) BEFORE applying the $width and $height parameters. This is designed to be used with a frontend cropping UI like [jcrop](http://deepliquid.com/content/Jcrop.html) so that you can respect a cropping selection that the user has defined but then output thumbnails or sized down versions of that selection with Croppa.
- `trim_perc($x1_perc, $y1_perc, $x2_perc, $y2_perc)` - Has the same effect as `trim()` but accepts coordinates as percentages. Thus, the the upper left of the image is "0" and the bottom right of the image is "1". So if you wanted to trim the image to half the size around the center, you would add an option of `trim_perc(0.25,0.25,0.75,0.75)`
- `quality($int)` - Set the jpeg compression quality from 0 to 100.
- `interlace($bool)` - Set to `1` or `0` to turn interlacing on or off
- `upscale($bool)` - Set to `1` or `0` to allow images to be upscaled. If falsey and you ask for a size bigger than the source, it will **only** create an image as big as the original source.
- $url : The URL of your source image. The path to the image relative to the `src_disk` will be extracted using the `path` config regex.
- $width : A number or null for wildcard
- $height : A number or null for wildcard
- $options - An array of key value pairs, where the value is an optional array of arguments for the option. Supported option are:
- `resize` - Make the image fit in the provided width and height through resizing. When omitted, the default is to crop to fit in the bounds (unless one of sides is a wildcard).
- `pad` - Pad an image to desired dimensions. Moves the image into the center and fills the rest with given color. If no color is given, it will use white [255,255,255]
- `quadrant($quadrant)` - Crop the remaining overflow of an image using the passed quadrant heading. The supported `$quadrant` values are: `T` - Top (good for headshots), `B` - Bottom, `L` - Left, `R` - Right, `C` - Center (default).
- `trim($x1, $y1, $x2, $y2)` - Crop the source image to the size defined by the two sets of coordinates ($x1, $y1, ...) BEFORE applying the $width and $height parameters. This is designed to be used with a frontend cropping UI like [jcrop](http://deepliquid.com/content/Jcrop.html) so that you can respect a cropping selection that the user has defined but then output thumbnails or sized down versions of that selection with Croppa.
- `trim_perc($x1_perc, $y1_perc, $x2_perc, $y2_perc)` - Has the same effect as `trim()` but accepts coordinates as percentages. Thus, the the upper left of the image is "0" and the bottom right of the image is "1". So if you wanted to trim the image to half the size around the center, you would add an option of `trim_perc(0.25,0.25,0.75,0.75)`
- `quality($int)` - Set the jpeg compression quality from 0 to 100.
- `interlace($bool)` - Set to `1` or `0` to turn interlacing on or off
- `upsize($bool)` - Set to `1` or `0` to allow images to be upsized. If falsey and you ask for a size bigger than the source, it will **only** create an image as big as the original source.

#### Croppa::render($cropurl)

Expand All @@ -142,7 +142,7 @@ Croppa::render(Croppa::url('image.png', 300, 200));

#### Croppa::delete($url)

You can delete a source image and all of it’s crops (like if a related DB row was deleted) by running:
You can delete a source image and all of its crops by running:

```php
Croppa::delete('/path/to/src.png');
Expand All @@ -160,10 +160,10 @@ Croppa::reset('/path/to/src.png');

#### `croppa:purge`

Deletes **ALL** crops. This works by scanning the `crops_dir` recursively and matching all files that have the Croppa naming convention where a corresponding `src` file can be found. Accepts the following options:
Deletes **all** crops. This works by scanning the `crops_disk` recursively and matching all files that have the Croppa naming convention where a corresponding `src` file can be found. Accepts the following options:

- `--filter` - Applies a whitelisting regex filter to the crops. For example: `--filter=^01/` matches all crops in the "./public/uploads/01/" directory
- `--dry-run` - Ouputs the files that would be deleted to the console, but doesn’t actually remove
- `--filter` - Applies a whitelisting regex filter to the crops. For example: `--filter=^01/` matches all crops in the "./public/uploads/01/" directory
- `--dry-run` - Ouputs the files that would be deleted to the console, but doesn’t actually remove

## croppa.js

Expand All @@ -185,4 +185,4 @@ Run `php artisan asset:publish bkwld/croppa` to have Laravel copy the JS to your

Read the Github [project releases](https://github.com/BKWLD/croppa/releases) for release notes.

This bundle uses [PHPThumb](https://github.com/masterexploder/PHPThumb) to do all the [image resizing](https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage). "Crop" is equivalent to it’s adaptiveResize() and "resize" is … resize(). Support for interacting with non-local disks provided by [Flysystem](http://flysystem.thephpleague.com/).
This package uses [Intervention Image](https://image.intervention.io/) to do all the image resizing. "Crop" is equivalent to it’s fit() and "resize" is … resize(). Support for interacting with non-local disks provided by [Flysystem](http://flysystem.thephpleague.com/).
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"authors": [
{
"name": "Robert Reinhard"
},
{
"name": "Samuel De Backer"
}
],
"license": "MIT",
Expand All @@ -12,10 +15,10 @@
"illuminate/console": "^9.0",
"illuminate/support": "^9.0",
"illuminate/routing": "^9.0",
"intervention/image": "^2.7",
"league/flysystem": "^3.0",
"symfony/http-foundation": "^6.0",
"symfony/http-kernel": "^6.0",
"weotch/phpthumb": "^1.0.5",
"ext-gd": "*"
},
"require-dev": {
Expand All @@ -25,9 +28,6 @@
"suggest": {
"ext-exif": "Required if you want to have Croppa auto-rotate images from devices like mobile phones based on exif meta data."
},
"conflict": {
"league/flysystem-cached-adapter": "<1.0.2"
},
"autoload": {
"psr-0": {
"Bkwld\\Croppa": "src/"
Expand All @@ -38,7 +38,7 @@
"extra": {
"laravel": {
"providers": [
"Bkwld\\Croppa\\ServiceProvider"
"Bkwld\\Croppa\\CroppaServiceProvider"
],
"aliases": {
"Croppa": "Bkwld\\Croppa\\Facade"
Expand Down
34 changes: 0 additions & 34 deletions helpers/Croppa.as

This file was deleted.

12 changes: 2 additions & 10 deletions src/Bkwld/Croppa/Commands/Purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Console\Input\InputOption;

/**
* Delete ALL crops from the crops_dir.
* Delete all crops from the crops_disk.
*/
class Purge extends Command
{
Expand All @@ -23,7 +23,7 @@ class Purge extends Command
*
* @var string
*/
protected $description = 'Delete ALL crops';
protected $description = 'Delete all crops';

/**
* @var Bkwld\Croppa\Storage
Expand All @@ -50,14 +50,6 @@ public function handle()
}
}

/**
* Backwards compatability with Laravel 4.
*/
public function fire()
{
$this->handle();
}

/**
* Get the console command options.
*
Expand Down
71 changes: 71 additions & 0 deletions src/Bkwld/Croppa/CroppaServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Bkwld\Croppa;

use Bkwld\Croppa\Commands\Purge;
use Illuminate\Support\ServiceProvider;

class CroppaServiceProvider extends ServiceProvider
{
public function register()
{
// Bind the Croppa URL generator and parser.
$this->app->singleton(URL::class, function ($app) {
return new URL($this->getConfig());
});

// Handle the request for an image, this coordinates the main logic.
$this->app->singleton(Handler::class, function ($app) {
return new Handler(
$app[URL::class],
$app[Storage::class],
$app['request'],
$this->getConfig()
);
});

// Interact with the disk.
$this->app->singleton(Storage::class, function ($app) {
return new Storage($app, $this->getConfig());
});

// API for use in apps.
$this->app->singleton(Helpers::class, function ($app) {
return new Helpers($app[URL::class], $app[Storage::class], $app[Handler::class]);
});

// Register command to delete all crops.
$this->app->singleton(Purge::class, function ($app) {
return new Purge($app[Storage::class]);
});

$this->commands(Purge::class);
}

public function boot()
{
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'croppa');
$this->publishes([__DIR__.'/../../config/config.php' => config_path('croppa.php')], 'croppa-config');

$this->app['router']
->get('{path}', 'Bkwld\Croppa\Handler@handle')
->where('path', $this->app[URL::class]->routePattern());
}

/**
* Get the configuration.
*
* @return array
*/
public function getConfig()
{
$config = $this->app->make('config')->get('croppa');

// Use Laravel’s encryption key if instructed to.
if (isset($config['signing_key']) && $config['signing_key'] === 'app.key') {
$config['signing_key'] = $this->app->make('config')->get('app.key');
}

return $config;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Bkwld\Croppa;
namespace Bkwld\Croppa\Facades;

class Facade extends \Illuminate\Support\Facades\Facade
use Illuminate\Support\Facades\Facade;

class Croppa extends Facade
{
protected static function getFacadeAccessor()
{
Expand Down
11 changes: 3 additions & 8 deletions src/Bkwld/Croppa/Filters/BlackWhite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace Bkwld\Croppa\Filters;

use GdThumb;
use Intervention\Image\Image;

class BlackWhite implements FilterInterface
{
/**
* Applies filter to given thumbnail object.
*
* @return \Intervention\Image\Image
*/
public function applyFilter(GdThumb $thumb)
public function applyFilter(Image $thumb): Image
{
return $thumb->imageFilter(IMG_FILTER_GRAYSCALE);
return $thumb->greyscale();
}
}
11 changes: 3 additions & 8 deletions src/Bkwld/Croppa/Filters/Blur.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace Bkwld\Croppa\Filters;

use GdThumb;
use Intervention\Image\Image;

class Blur implements FilterInterface
{
/**
* Applies filter to given thumbnail object.
*
* @return \Intervention\Image\Image
*/
public function applyFilter(GdThumb $thumb)
public function applyFilter(Image $thumb): Image
{
return $thumb->imageFilter(IMG_FILTER_GAUSSIAN_BLUR);
return $thumb->blur();
}
}
Loading

0 comments on commit 51892e4

Please sign in to comment.