1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ImageController |
19
|
|
|
* |
20
|
|
|
* @package ResponsiveImageBundle\Controller |
21
|
|
|
*/ |
22
|
|
|
class ImageController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Generates a derivative image as a response |
26
|
|
|
* |
27
|
|
|
* @param $stylename |
28
|
|
|
* @param $filename |
29
|
|
|
* |
30
|
|
|
* @return StreamedResponse |
31
|
|
|
*/ |
32
|
|
|
public function indexAction($stylename, $filename) |
33
|
|
|
{ |
34
|
|
|
// Get image style information. |
35
|
|
|
if (empty($this->get('responsive_image.style_manager')->styleExists($stylename))) { |
36
|
|
|
// @TODO: Custom exception would better. |
37
|
|
|
throw $this->createNotFoundException('The style does not exist'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Create image if the file exists. |
41
|
|
|
$imageObject = $this->get('responsive_image.file_to_object')->getObjectFromFilename($filename); |
42
|
|
|
if (!empty($imageObject)) { |
43
|
|
|
$generatedImageArray = $this->get('responsive_image.image_manager')->createStyledImages( |
44
|
|
|
$imageObject, |
45
|
|
|
[$stylename] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
if (!empty($generatedImageArray[$stylename])) { |
49
|
|
|
$path = $generatedImageArray[$stylename]; |
50
|
|
|
|
51
|
|
|
$cache = $this->get('responsive_image.file_system_wrapper')->getAdapter(); |
52
|
|
|
$stream = $cache->readStream($path); |
53
|
|
|
|
54
|
|
|
$response = new StreamedResponse(); |
55
|
|
|
$response->headers->set('Content-Type', $cache->getMimetype($path)); |
56
|
|
|
$response->headers->set('Content-Length', $cache->getSize($path)); |
57
|
|
|
$response->setPublic(); |
58
|
|
|
$response->setMaxAge(31536000); |
59
|
|
|
$response->setExpires(date_create()->modify('+1 years')); |
60
|
|
|
|
61
|
|
|
$response->setCallback( |
62
|
|
|
function () use ($stream) { |
63
|
|
|
if (ftell($stream['stream']) !== 0) { |
64
|
|
|
rewind($stream['stream']); |
65
|
|
|
} |
66
|
|
|
fpassthru($stream['stream']); |
67
|
|
|
fclose($stream['stream']); |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return $response; |
72
|
|
|
} |
73
|
|
|
else { |
74
|
|
|
throw $this->createNotFoundException('Derived image could not be created'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
else { |
78
|
|
|
throw $this->createNotFoundException('The file does not exist'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|