-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathThumbExtension.php
More file actions
73 lines (61 loc) · 2.18 KB
/
Copy pathThumbExtension.php
File metadata and controls
73 lines (61 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Comur\ImageBundle\Twig;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFilter;
class ThumbExtension extends AbstractExtension implements GlobalsInterface
{
protected $croppedDir;
protected $thumbsDir;
protected $galleryDir;
protected $webDir;
protected $transDomain;
public function __construct($croppedDir, $thumbsDir, $webDir, $transDomain, $galleryDir)
{
$this->croppedDir = $croppedDir;
$this->thumbsDir = $thumbsDir;
$this->transDomain = $transDomain;
$this->webDir = $webDir;
$this->galleryDir = $galleryDir;
}
public function getFilters()
{
return array(
new TwigFilter('thumb', array($this, 'getThumb')),
new TwigFilter('gallery_thumb', array($this, 'getGalleryThumb')),
);
}
/**
* Returns thumb file if exists
* @param string $origFilePath web path to original file (relative, ex: uploads/members/cropped/azda4qs.jpg)
* @param integer $width desired thumb's width
* @param integer $height desired thumb's height
* @return string thumbnail path if thumbnail exists, if not returns original file path
*/
public function getThumb($origFilePath, $width, $height)
{
$pathInfo = pathinfo($origFilePath);
if(isset($pathInfo['dirname']) && isset($pathInfo['basename']))
{
$uploadDir = $pathInfo['dirname'] . '/';
$filename = $pathInfo['basename'];
$thumbSrc = $uploadDir . $this->thumbsDir . '/' . $width . 'x' . $height . '-' .$filename;
// return $this->webDir.'/'.$thumbSrc;
return $thumbSrc;
// return file_exists($this->webDir.'/'.$thumbSrc) ? $thumbSrc : $uploadDir . $filename;
}
return $origFilePath;
}
public function getGalleryThumb($origFilePath, $width, $height)
{
return $this->getThumb($this->galleryDir.'/'.$origFilePath, $width, $height);
}
public function getName()
{
return 'comur_thumb_extension';
}
public function getGlobals(): array
{
return array('comur_translation_domain' => $this->transDomain);
}
}