Skip to content

Commit 1938544

Browse files
committed
Initial commit.
0 parents  commit 1938544

13 files changed

+515
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
composer.lock
3+
vendor

README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Glide
2+
3+
The purpose of this library is to offer extremely easy image manipulation in a similar fashion to cloud image processing services like [Imgix](http://www.imgix.com/) and [Cloudinary](http://cloudinary.com/).
4+
5+
## Highlights
6+
7+
- Adjust, resize and add effects to images using a simple URL based API.
8+
- Manipulated images are automatically cached and served with far-future expires headers.
9+
- Create your own image processing server or integrate directly into your app.
10+
- Supports the [GD Library](http://php.net/manual/en/book.image.php) and [Imagick PHP extension](http://php.net/manual/en/book.imagick.php).
11+
- Ability to secure image URLs using a private signing key.
12+
- Works with many different file systems, using the [Flysystem](http://flysystem.thephpleague.com/) library.
13+
- Powered by the [Intervention Image](http://image.intervention.io/) image handling and manipulation library.
14+
15+
## The API
16+
17+
- **Width** `w`
18+
- The width in pixels of the output image.
19+
- Example: `image.jpg?w=300`
20+
- **Height** `h`
21+
- The height in pixels of the output image.
22+
- Example: `image.jpg?h=300`
23+
- **Fit** `fit`
24+
- Controls how the output image is fitted to its target dimensions.
25+
- Accepts: `clip`, `scale`, `crop`
26+
- Example: `image.jpg?w=300&fit=crop`
27+
- **Rectangle** `rect`
28+
- Crops an image to specific dimensions.
29+
- Example: `image.jpg?rect=100,100,25,90`
30+
- **Crop Position** `crop`
31+
- Controls how the input image is aligned when the `fit` parameter is set to `crop`.
32+
- Accepts: `top-left`, `top`, `top-right`, `left`, `center`, `right`, `bottom-left`, `bottom`, `bottom-right`
33+
- Example: `image.jpg?rect=100,100,25,90`
34+
- **Orientation** `orient`
35+
- Rotates an image by supplied angle.
36+
- By default it uses Exif data to automatically orient images correctly.
37+
- Example: `image.jpg?orient=90`
38+
- **Brightness** `bri`
39+
- Adjusts the image brightness.
40+
- Use values between `-100` and `+100`.
41+
- Example: `image.jpg?bri=50`
42+
- **Contrast** `con`
43+
- Adjusts the image contrast.
44+
- Use values between `-100` for min. contrast, `0` for no change and `+100` for max. contrast.
45+
- Example: `image.jpg?con=50`
46+
- **Gamma** `gam`
47+
- Adjusts the image gamma.
48+
- Example: `image.jpg?gam=1.6`
49+
- **Blur** `blur`
50+
- Blurs an image by supplied blur strength.
51+
- Use values between `0` and `100`.
52+
- Example: `image.jpg?blur=15`
53+
54+
## Example
55+
56+
```php
57+
use Aws\S3\S3Client;
58+
use League\Flysystem\Adapter\AwsS3 as S3Adapter;
59+
use League\Flysystem\Adapter\Local as LocalAdapter;
60+
use League\Flysystem\Filesystem;
61+
62+
// Connect to S3 account
63+
$s3Client = S3Client::factory([
64+
'key' => 'your-key',
65+
'secret' => 'your-secret',
66+
]);
67+
68+
// Setup server and define source and cache
69+
$glide = new Glide\Server(
70+
new Filesystem(new S3Adapter($s3Client, 'bucket-name')),
71+
new Filesystem(new LocalAdapter('cache-folder'))
72+
);
73+
74+
// Enable private URLs
75+
$glide->setSignKey('your-signing-key');
76+
77+
// Output Image
78+
$glide->output(
79+
$request->getPathInfo(),
80+
$request->query->all()
81+
);
82+
```

composer.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"require": {
3+
"intervention/image": "~2.0",
4+
"league/flysystem": "~0.5"
5+
},
6+
"autoload":
7+
{
8+
"psr-4":
9+
{
10+
"Glide\\": "src/"
11+
}
12+
}
13+
}

src/API.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Glide;
4+
5+
use Intervention\Image\Image;
6+
7+
class API
8+
{
9+
private $manipulators;
10+
11+
public function __construct($params)
12+
{
13+
$this->manipulators = [
14+
'adjustments' => new Manipulators\Adjustments(),
15+
'size' => new Manipulators\Size(),
16+
'effects' => new Manipulators\Effects(),
17+
'encode' => new Manipulators\Encode(),
18+
];
19+
20+
foreach ($params as $name => $value) {
21+
$this->$name($value);
22+
}
23+
}
24+
25+
public function bri($value)
26+
{
27+
$this->manipulators['adjustments']->setBrightness($value);
28+
}
29+
30+
public function con($value)
31+
{
32+
$this->manipulators['adjustments']->setContrast($value);
33+
}
34+
35+
public function gam($value)
36+
{
37+
$this->manipulators['adjustments']->setGamma($value);
38+
}
39+
40+
public function orient($value)
41+
{
42+
$this->manipulators['size']->setOrientation($value);
43+
}
44+
45+
public function w($value)
46+
{
47+
$this->manipulators['size']->setWidth($value);
48+
}
49+
50+
public function h($value)
51+
{
52+
$this->manipulators['size']->setHeight($value);
53+
}
54+
55+
public function fit($value)
56+
{
57+
$this->manipulators['size']->setFit($value);
58+
}
59+
60+
public function blur($value)
61+
{
62+
$this->manipulators['effects']->setBlur($value);
63+
}
64+
65+
public function fm($value)
66+
{
67+
$this->manipulators['encode']->setFormat($value);
68+
}
69+
70+
public function q($value)
71+
{
72+
$this->manipulators['encode']->setQuality($value);
73+
}
74+
75+
public function run(Image $image)
76+
{
77+
foreach ($this->manipulators as $manipulator) {
78+
$image = $manipulator->run($image);
79+
}
80+
81+
return $image;
82+
}
83+
}

src/ImageNotFoundException.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Glide;
4+
5+
class ImageNotFoundException extends \Exception
6+
{
7+
}

src/Manipulators/Adjustments.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Glide\Manipulators;
4+
5+
use Intervention\Image\Image;
6+
7+
class Adjustments implements Manipulator
8+
{
9+
private $brightness;
10+
private $contrast;
11+
private $gamma;
12+
private $colorize;
13+
14+
public function setBrightness($brightness)
15+
{
16+
$this->brightness = $brightness;
17+
}
18+
19+
public function setContrast($contrast)
20+
{
21+
$this->contrast = $contrast;
22+
}
23+
24+
public function setGamma($gamma)
25+
{
26+
$this->gamma = $gamma;
27+
}
28+
29+
public function setColor($color)
30+
{
31+
$colors = explode(',', $color);
32+
33+
$this->colorize['r'] = (int) $colors[0];
34+
$this->colorize['g'] = (int) $colors[1];
35+
$this->colorize['b'] = (int) $colors[2];
36+
}
37+
38+
public function run(Image $image)
39+
{
40+
if ($this->brightness) {
41+
$image->brightness($this->brightness);
42+
}
43+
44+
if ($this->contrast) {
45+
$image->contrast($this->contrast);
46+
}
47+
48+
if ($this->gamma) {
49+
$image->gamma($this->gamma);
50+
}
51+
52+
return $image;
53+
}
54+
}

src/Manipulators/Effects.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Glide\Manipulators;
4+
5+
use Intervention\Image\Image;
6+
7+
class Effects implements Manipulator
8+
{
9+
private $blur;
10+
11+
public function setBlur($blur)
12+
{
13+
$this->blur = $blur;
14+
}
15+
16+
public function run(Image $image)
17+
{
18+
if ($this->blur) {
19+
$image->blur($this->blur);
20+
}
21+
22+
return $image;
23+
}
24+
}

src/Manipulators/Encode.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Glide\Manipulators;
4+
5+
use Intervention\Image\Image;
6+
7+
class Encode implements Manipulator
8+
{
9+
private $format = 'jpg';
10+
private $quality = 90;
11+
12+
public function setFormat($format)
13+
{
14+
$this->format = $format;
15+
}
16+
17+
public function setQuality($quality)
18+
{
19+
$this->quality = $quality;
20+
}
21+
22+
public function run(Image $image)
23+
{
24+
return $image->encode($this->format, $this->quality)->getEncoded();
25+
}
26+
}

src/Manipulators/Manipulator.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Glide\Manipulators;
4+
5+
use Intervention\Image\Image;
6+
7+
interface Manipulator
8+
{
9+
public function run(Image $image);
10+
}

src/Manipulators/Size.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Glide\Manipulators;
4+
5+
use Intervention\Image\Image;
6+
7+
class Size implements Manipulator
8+
{
9+
private $orientation;
10+
private $width;
11+
private $height;
12+
private $fit = 'clip';
13+
private $rect; // crop to specific dimensions
14+
private $crop; // crop position when set to fit = crop
15+
16+
public function setOrientation($orientation)
17+
{
18+
$parts = explode(',', $orientation);
19+
$this->orientation['angle'] = $parts[0];
20+
$this->orientation['color'] = isset($parts[1]) ? '#' . $parts[1] : '#000000';
21+
}
22+
23+
public function setWidth($width)
24+
{
25+
$this->width = $width;
26+
}
27+
28+
public function setHeight($height)
29+
{
30+
$this->height = $height;
31+
}
32+
33+
public function setFit($fit)
34+
{
35+
$this->fit = $fit;
36+
}
37+
38+
public function run(Image $image)
39+
{
40+
if (is_array($this->orientation)) {
41+
$image->rotate(
42+
$this->orientation['angle'],
43+
$this->orientation['color']
44+
);
45+
} else {
46+
$image->orientate();
47+
}
48+
49+
if ($this->fit === 'clip') {
50+
$image->resize($this->width, $this->height, function ($constraint) {
51+
$constraint->aspectRatio();
52+
});
53+
} else if ($this->fit === 'scale') {
54+
$image->resize($this->width, $this->height);
55+
} else if ($this->fit === 'crop') {
56+
$image->fit($this->width, $this->height);
57+
}
58+
59+
return $image;
60+
}
61+
}

0 commit comments

Comments
 (0)