12
12
*/
13
13
class Image extends AbstractHelper
14
14
{
15
- protected $ _width ;
16
- protected $ _height ;
17
-
18
15
/**
19
16
* Default quality value (for JPEG images only).
20
17
*
21
18
* @var int
22
19
*/
23
20
protected $ _quality = 100 ;
24
- protected $ _keepAspectRatio = false ;
25
- protected $ _keepFrame = false ;
21
+ protected $ _keepAspectRatio = true ;
22
+ protected $ _keepFrame = true ;
26
23
protected $ _keepTransparency = true ;
27
24
protected $ _constrainOnly = true ;
28
25
protected $ _backgroundColor = [255 , 255 , 255 ];
29
26
protected $ _baseFile ;
30
- protected $ _isBaseFilePlaceholder ;
31
27
protected $ _newFile ;
32
- protected $ _processor ;
33
- protected $ _destinationSubdir ;
34
- protected $ _angle ;
35
- protected $ _watermarkFile ;
36
- protected $ _watermarkPosition ;
37
- protected $ _watermarkWidth ;
38
- protected $ _watermarkHeight ;
39
- protected $ _watermarkImageOpacity = 0 ;
40
28
41
29
public function __construct (
42
30
\Magento \Framework \App \Helper \Context $ context ,
@@ -57,64 +45,52 @@ public function init($baseFile)
57
45
return $ this ;
58
46
}
59
47
60
- public function getImageProcessor ( )
48
+ public function resize ( $ width , $ height = null )
61
49
{
62
- //if (!$this->_processor) {
63
- $ filename = $ this ->_baseFile ? $ this ->_mediaDirectory ->getAbsolutePath ($ this ->_baseFile ) : null ;
64
- $ this ->_processor = $ this ->_imageFactory ->create ($ filename );
65
- //}
66
- $ this ->_processor ->keepAspectRatio ($ this ->_keepAspectRatio );
67
- $ this ->_processor ->keepFrame ($ this ->_keepFrame );
68
- $ this ->_processor ->keepTransparency ($ this ->_keepTransparency );
69
- $ this ->_processor ->constrainOnly ($ this ->_constrainOnly );
70
- $ this ->_processor ->backgroundColor ($ this ->_backgroundColor );
71
- $ this ->_processor ->quality ($ this ->_quality );
72
- $ this ->_processor ->resize ($ this ->_width , $ this ->_height );
73
- return $ this ->_processor ;
74
- }
75
-
76
- public function saveFile ()
77
- {
78
- $ filename = $ this ->_mediaDirectory ->getAbsolutePath ($ this ->_newFile );
79
- $ this ->getImageProcessor ()->save ($ filename );
80
- //$this->_coreFileStorageDatabase->saveFile($filename);
50
+ if ($ this ->_baseFile ){
51
+ $ path = 'blog/cache/ ' . $ width . 'x ' . $ height ;
52
+ $ this ->_newFile = $ path . '/ ' . $ this ->_baseFile ;
53
+ if (true || !$ this ->fileExists ($ this ->_newFile )) {
54
+ $ this ->resizeBaseFile ($ width , $ height );
55
+ }
56
+ }
81
57
return $ this ;
82
58
}
83
59
84
- protected function _fileExists ( $ filename )
60
+ protected function resizeBaseFile ( $ width , $ height )
85
61
{
86
- if ($ this ->_mediaDirectory ->isFile ($ filename )) {
87
- return true ;
88
- } else {
89
- return false ;
62
+ if (!$ this ->fileExists ($ this ->_baseFile )) {
63
+ $ this ->_baseFile = null ;
64
+ return $ this ;
90
65
}
91
- }
92
66
93
- public function isCached ()
94
- {
95
- if (is_string ($ this ->_newFile )) {
96
- return $ this ->_fileExists ($ this ->_newFile );
97
- }
67
+ $ processor = $ this ->_imageFactory ->create (
68
+ $ this ->_mediaDirectory ->getAbsolutePath ($ this ->_baseFile )
69
+ );
70
+ $ processor ->keepAspectRatio ($ this ->_keepAspectRatio );
71
+ $ processor ->keepFrame ($ this ->_keepFrame );
72
+ $ processor ->keepTransparency ($ this ->_keepTransparency );
73
+ $ processor ->constrainOnly ($ this ->_constrainOnly );
74
+ $ processor ->backgroundColor ($ this ->_backgroundColor );
75
+ $ processor ->quality ($ this ->_quality );
76
+ $ processor ->resize ($ width , $ height );
77
+
78
+ $ newFile = $ this ->_mediaDirectory ->getAbsolutePath ($ this ->_newFile );
79
+ $ processor ->save ($ newFile );
80
+ unset($ processor );
81
+
82
+ return $ this ;
98
83
}
99
84
100
- public function resize ( $ width , $ height = null )
85
+ protected function fileExists ( $ filename )
101
86
{
102
- if ($ this ->_baseFile ){
103
- $ this ->_width = $ width ;
104
- $ this ->_height = $ height ;
105
- $ path = 'blog/cache/ ' .$ width .'x ' .$ height ;
106
- $ this ->_newFile = $ path . '/ ' . $ this ->_baseFile ;
107
- if (!$ this ->isCached ()){
108
- $ this ->saveFile ();
109
- }
110
- }
111
- return $ this ;
87
+ return $ this ->_mediaDirectory ->isFile ($ filename );
112
88
}
113
89
114
90
public function __toString ()
115
91
{
116
92
$ url = "" ;
117
- if ($ this ->_baseFile ){
93
+ if ($ this ->_baseFile ){
118
94
$ url = $ this ->_storeManager ->getStore ()->getBaseUrl (
119
95
\Magento \Framework \UrlInterface::URL_TYPE_MEDIA
120
96
) . $ this ->_newFile ;
0 commit comments