|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Cecil/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil\Assets; |
|
12
|
|
|
|
|
13
|
|
|
use Cecil\Builder; |
|
14
|
|
|
use Cecil\Config; |
|
15
|
|
|
use Cecil\Exception\Exception; |
|
16
|
|
|
use Cecil\Util; |
|
17
|
|
|
use Intervention\Image\Exception\NotReadableException; |
|
18
|
|
|
use Intervention\Image\ImageManagerStatic as ImageManager; |
|
19
|
|
|
|
|
20
|
|
|
class Image |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Config */ |
|
23
|
|
|
private $config; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Builder $builder) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->config = $builder->getConfig(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Resize an image. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $path Image path (relative from static/ dir or external) |
|
34
|
|
|
* @param int $size Image new size (width) |
|
35
|
|
|
* |
|
36
|
|
|
* @return string Path to image thumbnail |
|
37
|
|
|
*/ |
|
38
|
|
|
public function resize(string $path, int $size): string |
|
39
|
|
|
{ |
|
40
|
|
|
$external = false; |
|
41
|
|
|
|
|
42
|
|
|
// is external image? |
|
43
|
|
|
if (Util::isExternalUrl($path)) { |
|
44
|
|
|
$external = true; |
|
45
|
|
|
$source = $path; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!$external) { |
|
49
|
|
|
// source |
|
50
|
|
|
$source = $this->config->getStaticPath().'/'.$path; |
|
51
|
|
|
if (!Util::getFS()->exists($source)) { |
|
52
|
|
|
throw new Exception(sprintf('Can\'t resize "%s": file doesn\'t exits.', $path)); |
|
53
|
|
|
} |
|
54
|
|
|
// destination |
|
55
|
|
|
// ie: .cache/images/thumbs |
|
56
|
|
|
$thumbsDir = (string) $this->config->get('cache.dir').'/'.$this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size; |
|
|
|
|
|
|
57
|
|
|
// ie: .cache/images/thumbs/img/logo.png |
|
58
|
|
|
$imageRelPath = $thumbsDir.'/'.ltrim($path, '/'); |
|
59
|
|
|
// full absolute path |
|
60
|
|
|
$destination = $this->config->getDestinationDir().'/'.$imageRelPath; |
|
61
|
|
|
if ((bool) $this->config->get('cache.external')) { |
|
62
|
|
|
$destination = $imageRelPath; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// is size is already OK? |
|
67
|
|
|
list($width, $height) = getimagesize($external ? $path : $source); |
|
|
|
|
|
|
68
|
|
|
if ($width <= $size && $height <= $size) { |
|
69
|
|
|
return $path; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// if GD extension is not installed: can't process |
|
73
|
|
|
if (!extension_loaded('gd')) { |
|
74
|
|
|
return $path; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// external image: return data URL |
|
78
|
|
|
if ($external) { |
|
79
|
|
|
try { |
|
80
|
|
|
$img = ImageManager::make($path); |
|
81
|
|
|
} catch (NotReadableException $e) { |
|
82
|
|
|
throw new Exception(sprintf('Cannot get image "%s"', $path)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return (string) $img->encode('data-url'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// resize |
|
89
|
|
|
if (!Util::getFS()->exists($destination)) { |
|
|
|
|
|
|
90
|
|
|
$img = ImageManager::make($source); |
|
91
|
|
|
$img->resize($size, null, function (\Intervention\Image\Constraint $constraint) { |
|
92
|
|
|
$constraint->aspectRatio(); |
|
93
|
|
|
$constraint->upsize(); |
|
94
|
|
|
}); |
|
95
|
|
|
// is a sub dir is necessary? |
|
96
|
|
|
$imageSubDir = Util::getFS()->makePathRelative('/'.dirname($imageRelPath), '/'.$thumbsDir.'/'); |
|
|
|
|
|
|
97
|
|
|
if (!empty($imageSubDir)) { |
|
98
|
|
|
$destDir = $this->config->getCacheImagesThumbsPath().'/'.$size.'/'.$imageSubDir; |
|
99
|
|
|
Util::getFS()->mkdir($destDir); |
|
100
|
|
|
} |
|
101
|
|
|
$img->save($destination); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// return relative path |
|
105
|
|
|
return '/'.$this->config->get('cache.images.dir').'/'.$this->config->get('cache.images.thumbs.dir').'/'.$size.'/'.ltrim($path, '/'); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|