forked from Vinai/nicer-image-names
/
Image.php
197 lines (173 loc) · 6.18 KB
/
Image.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Netzarbeiter
* @package Netzarbeiter_NicerImageNames
* @copyright Copyright (c) 2014 Vinai Kopp http://netzarbeiter.com/
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Catalog product link model overwrite, make the cache image names nicer
*
* @category Netzarbeiter
* @package Netzarbeiter_NicerImageNames
* @author Vinai Kopp <vinai@netzarbeiter.com>
*/
class Netzarbeiter_NicerImageNames_Model_Image extends Mage_Catalog_Model_Product_Image
{
/**
* This will be used as the file name in the cache
*
* @var string
*/
protected $_niceCacheName = '';
/**
* Set the cache file base name
*
* @param string $name
* @see Netzarbeiter_NicerImageNames_Helper_Image::init()
*/
public function setNiceCacheName($name)
{
$this->_niceCacheName = $name;
}
/**
* Return the cache file base name
*
* @return string
* @see Netzarbeiter_NicerImageNames_Helper_Image::init()
*/
public function getNiceCacheName()
{
return $this->_niceCacheName;
}
/**
* Set filenames for base file and new file
*
* @param string $file
* @return Mage_Catalog_Model_Product_Image
* @throws Mage_Core_Exception
*/
public function setBaseFile($file)
{
parent::setBaseFile($file);
if (!Mage::getStoreConfig("catalog/nicerimagenames/disable_ext") && !$this->_isBaseFilePlaceholder) {
// The $_newFile property is set during parent::setBaseFile()
list($path, $file) = $this->_getFilePathAndName($this->_newFile);
$file = $this->_getNiceFileName($file);
if (Mage::getStoreConfig("catalog/nicerimagenames/lowercase")) {
$file = strtolower($file);
}
list($pathExt, $extension) = $this->_getFileNameParts($file);
// Check that generated filename is not longer than 255
$maxfilelen = 255;
if (strlen(basename($file) . $extension) > $maxfilelen) {
$file = substr($file, 0, ($maxfilelen - strlen($extension) + strlen(dirname($file))));
$file = $file . '.' . $extension;
}
$this->_newFile = $path . $file; // the $file contains heading slash
if (defined('PHP_MAXPATHLEN')) {
$maxlen = PHP_MAXPATHLEN;
} else {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$maxlen = 255; // NTFS sucks
} else {
$maxlen = 1024; // Safe assumption, probably more
}
}
if (strlen($this->_newFile) > $maxlen) {
// hack off as much as necessary to make it fit. Urg.
// See https://github.com/Vinai/nicer-image-names/issues/14 for more info
// This is no real solution, as there is no check if the resulting image
// still is in the right directory.
// The proper thing to do is to not use Windows for hosting,
// or, not use attributes with looog values in the name template.
$pathExt = substr($path . rtrim($pathExt, '/\\') . $pathExt, 0, ($maxlen - strlen($extension) - 2 ));
$this->_newFile = rtrim($pathExt, '/\\') . '.' . $extension;
}
}
return $this;
}
/**
* Return the file path and file name as an array
*
* @param $file
* @return array
*/
protected function _getFilePathAndName($file)
{
$path = dirname($file);
$file = '/' . basename($file);
return array($path, $file);
}
/**
* Return the filename with the correct number
*
* @param string $file
* @return string
*/
protected function _getNiceFileName($file)
{
// add the image name without the file type extension to the image cache path
list($pathExt, $extension) = $this->_getFileNameParts($file);
if (Mage::getStoreConfig("catalog/nicerimagenames/hash_image_folder_name")) {
$pathExt = md5($pathExt);
}
$file = $this->getNiceCacheName();
return sprintf('/%s/%s.%s', $pathExt, $file, $extension);
}
/**
* Return the extended file path and extension
*
* @param string $file
* @return array
*/
protected function _getFileNameParts($file)
{
$pos = strrpos($file, '.');
$pathExt = substr($file, 1, $pos - 1);
$extension = substr($file, $pos + 1);
return array($pathExt, $extension);
}
/**
* Overrides the parent method. It checks the base file change time,
* and if cached file is older - we concider an image is not cached
*
* @return bool
*/
public function isCached()
{
if (Mage::getStoreConfig("catalog/nicerimagenames/disable_ext")) {
// use parent method if nicerimagenames disabled
return parent::isCached();
}
if (!$this->_fileExists($this->_newFile)) {
return false;
}
if (!file_exists($this->_baseFile)) {
return true;
}
if (filemtime($this->_newFile) < filemtime($this->_baseFile)) {
// checks the base file change time, and if cached file is older - we concider an image is not cached
return false;
}
return true;
}
}