Skip to content

Commit ab4f94d

Browse files
committed
Response and stream commads added.
1 parent bfc49d8 commit ab4f94d

5 files changed

Lines changed: 79 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
22
composer.lock
33
vendor/
4-
dev/
4+
dev/
5+
.idea

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "3.*",
20-
"mockery/mockery": "~0.9.2"
20+
"mockery/mockery": "~0.9.2",
21+
"guzzlehttp/psr7": "~1.1"
2122
},
2223
"suggest": {
2324
"ext-gd": "to use GD library based image processing.",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Intervention\Image\Commands;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
7+
class PsrResponseCommand extends AbstractCommand
8+
{
9+
/**
10+
* Builds PSR7 compatible response. May replace "response" command in
11+
* some future.
12+
*
13+
* Method will generate binary stream and put it inside PSR-7
14+
* ResponseInterface. Following code can be optimized using native php
15+
* streams and more "clean" streaming, however drivers has to be updated
16+
* first.
17+
*
18+
* @param \Intervention\Image\Image $image
19+
* @return boolean
20+
*/
21+
public function execute($image)
22+
{
23+
$format = $this->argument(0)->value();
24+
$quality = $this->argument(1)->between(0, 100)->value();
25+
26+
//Encoded property will be populated at this moment
27+
$stream = $image->stream($format, $quality);
28+
29+
$mimetype = finfo_buffer(
30+
finfo_open(FILEINFO_MIME_TYPE),
31+
$image->encoded
32+
);
33+
34+
$this->setOutput(new Response(
35+
200,
36+
array(
37+
'Content-Type' => $mimetype,
38+
'Content-Length' => strlen($image->encoded)
39+
),
40+
$stream
41+
));
42+
43+
return true;
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Intervention\Image\Commands;
4+
5+
class StreamCommand extends AbstractCommand
6+
{
7+
/**
8+
* Builds PSR7 stream based on image data. Method uses Guzzle PSR7
9+
* implementation as easiest choice.
10+
*
11+
* @param \Intervention\Image\Image $image
12+
* @return boolean
13+
*/
14+
public function execute($image)
15+
{
16+
$format = $this->argument(0)->value();
17+
$quality = $this->argument(1)->between(0, 100)->value();
18+
19+
$this->setOutput(\GuzzleHttp\Psr7\stream_for(
20+
$image->encode($format, $quality)->encoded
21+
));
22+
23+
return true;
24+
}
25+
}

src/Intervention/Image/Image.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Intervention\Image;
44

5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\StreamInterface;
7+
58
/**
69
* @method \Intervention\Image\Image backup(string $name = 'default') Backups current image state as fallback for reset method under an optional name. Overwrites older state on every call, unless a different name is passed.
710
* @method \Intervention\Image\Image blur(integer $amount = 1) Apply a gaussian blur filter with a optional amount on the current image. Use values between 0 and 100.
@@ -45,6 +48,8 @@
4548
* @method \Intervention\Image\Image text(string $text, integer $x = 0, integer $y = 0, \Closure $callback = null) Write a text string to the current image at an optional x,y basepoint position. You can define more details like font-size, font-file and alignment via a callback as the fourth parameter.
4649
* @method \Intervention\Image\Image trim(string $base = 'top-left', array $away = array('top', 'bottom', 'left', 'right'), integer $tolerance = 0, integer $feather = 0) Trim away image space in given color. Define an optional base to pick a color at a certain position and borders that should be trimmed away. You can also set an optional tolerance level, to trim similar colors and add a feathering border around the trimed image.
4750
* @method \Intervention\Image\Image widen(integer $width, \Closure $callback = null) Resizes the current image to new width, constraining aspect ratio. Pass an optional Closure callback as third parameter, to apply additional constraints like preventing possible upsizing.
51+
* @method StreamInterface stream(string $format = null, integer $quality = 90) Builds PSR-7 compatible StreamInterface with current image in given format and quality.
52+
* @method ResponseInterface psrResponse(string $format = null, integer $quality = 90) Builds PSR-7 compatible ResponseInterface with current image in given format and quality.
4853
*/
4954
class Image extends File
5055
{

0 commit comments

Comments
 (0)