1212 */
1313class Image extends AbstractHelper
1414{
15- protected $ _width ;
16- protected $ _height ;
17-
1815 /**
1916 * Default quality value (for JPEG images only).
2017 *
2118 * @var int
2219 */
2320 protected $ _quality = 100 ;
24- protected $ _keepAspectRatio = false ;
25- protected $ _keepFrame = false ;
21+ protected $ _keepAspectRatio = true ;
22+ protected $ _keepFrame = true ;
2623 protected $ _keepTransparency = true ;
2724 protected $ _constrainOnly = true ;
2825 protected $ _backgroundColor = [255 , 255 , 255 ];
2926 protected $ _baseFile ;
30- protected $ _isBaseFilePlaceholder ;
3127 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 ;
4028
4129 public function __construct (
4230 \Magento \Framework \App \Helper \Context $ context ,
@@ -57,64 +45,52 @@ public function init($baseFile)
5745 return $ this ;
5846 }
5947
60- public function getImageProcessor ( )
48+ public function resize ( $ width , $ height = null )
6149 {
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+ }
8157 return $ this ;
8258 }
8359
84- protected function _fileExists ( $ filename )
60+ protected function resizeBaseFile ( $ width , $ height )
8561 {
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 ;
9065 }
91- }
9266
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 ;
9883 }
9984
100- public function resize ( $ width , $ height = null )
85+ protected function fileExists ( $ filename )
10186 {
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 );
11288 }
11389
11490 public function __toString ()
11591 {
11692 $ url = "" ;
117- if ($ this ->_baseFile ){
93+ if ($ this ->_baseFile ){
11894 $ url = $ this ->_storeManager ->getStore ()->getBaseUrl (
11995 \Magento \Framework \UrlInterface::URL_TYPE_MEDIA
12096 ) . $ this ->_newFile ;
0 commit comments