55 * PHPThumb creates thumbnails and altered images on the fly and caches them
66 *
77 * @category snippet
8- * @version 1.3.2
8+ * @version 1.3.3
99 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License (GPL)
1010 * @internal @properties
1111 * @internal @modx_category Content
1616 * @link noimage.png here [+site_url+]assets/snippets/phpthumb/noimage.png
1717 * @author Bumkaka
1818 * @author Many contributors since then
19- * @lastupdate 30/10 /2018
19+ * @lastupdate 26/11 /2018
2020 */
21- if (!defined ('MODX_BASE_PATH ' )) {
21+ if (! defined ('MODX_BASE_PATH ' )) {
2222 die ('What are you doing? Get out of here! ' );
2323}
2424
2525$ newFolderAccessMode = $ modx ->getConfig ('new_folder_permissions ' );
2626$ newFolderAccessMode = empty ($ new ) ? 0777 : octdec ($ newFolderAccessMode );
2727
28- $ cacheFolder = isset ($ cacheFolder ) ? $ cacheFolder : $ modx ->getCacheFolder () . "images " ;
28+ $ cacheFolder = isset ($ cacheFolder ) ? $ cacheFolder : $ modx ->getCacheFolder () . 'images ' ;
29+ $ phpThumbPath = isset ($ phpThumbPath ) ? $ phpThumbPath : 'assets/snippets/phpthumb/ ' ;
30+
2931/**
3032 * @see: https://github.com/kalessil/phpinspectionsea/blob/master/docs/probable-bugs.md#mkdir-race-condition
3133 */
3234$ path = MODX_BASE_PATH . $ cacheFolder ;
33- if (!file_exists ($ path ) && mkdir ($ path ) && is_dir ($ path )) {
34- chmod (MODX_BASE_PATH . $ cacheFolder , $ newFolderAccessMode );
35+ if (! file_exists ($ path ) && mkdir ($ path ) && is_dir ($ path )) {
36+ chmod ($ path , $ newFolderAccessMode );
3537}
3638
37- $ tmpFolder = $ modx ->getCacheFolder () . 'tmp ' ;
3839if (!empty ($ input )) {
3940 $ input = rawurldecode ($ input );
4041}
4142
42- if (empty ($ input ) || !file_exists (MODX_BASE_PATH . $ input )) {
43- $ input = isset ($ noImage ) ? $ noImage : ' assets/snippets/phpthumb/ noimage.png ' ;
43+ if (empty ($ input ) || ! file_exists (MODX_BASE_PATH . $ input )) {
44+ $ input = isset ($ noImage ) ? $ noImage : $ phpThumbPath . ' noimage.png ' ;
4445}
4546
4647/**
4748 * allow read in phpthumb cache folder
4849 */
49- if (!file_exists (MODX_BASE_PATH . $ cacheFolder . '/.htaccess ' ) &&
50+ if (! file_exists (MODX_BASE_PATH . $ cacheFolder . '/.htaccess ' ) &&
5051 $ cacheFolder !== $ modx ->getCacheFolder () &&
5152 strpos ($ cacheFolder , $ modx ->getCacheFolder ()) === 0
5253) {
5354 file_put_contents (MODX_BASE_PATH . $ cacheFolder . '/.htaccess ' , "order deny,allow \nallow from all \n" );
5455}
5556
56- $ path = MODX_BASE_PATH . $ tmpFolder ;
57- if (!file_exists ($ path ) && mkdir ($ path ) && is_dir ($ path )) {
58- chmod ($ path , $ newFolderAccessMode );
59- }
60-
6157$ path_parts = pathinfo ($ input );
62- $ tmpImagesFolder = str_replace ('assets/images ' , '' ,$ path_parts ['dirname ' ]);
58+ $ tmpImagesFolder = str_replace ('assets/images ' , '' , $ path_parts ['dirname ' ]);
6359$ tmpImagesFolder = explode ('/ ' , $ tmpImagesFolder );
6460$ ext = strtolower ($ path_parts ['extension ' ]);
6561$ options = 'f= ' . (in_array ($ ext , array ('png ' , 'gif ' , 'jpeg ' )) ? $ ext : 'jpg&q=85 ' ) . '& ' .
6662 strtr ($ options , array (', ' => '& ' , '_ ' => '= ' , '{ ' => '[ ' , '} ' => '] ' ));
6763
6864parse_str ($ options , $ params );
6965foreach ($ tmpImagesFolder as $ folder ) {
70- if (!empty ($ folder )) {
66+ if (! empty ($ folder )) {
7167 $ cacheFolder .= '/ ' . $ folder ;
7268 $ path = MODX_BASE_PATH . $ cacheFolder ;
73- if (!file_exists ($ path ) && mkdir ($ path ) && is_dir ($ path )) {
69+ if (! file_exists ($ path ) && mkdir ($ path ) && is_dir ($ path )) {
7470 chmod ($ path , $ newFolderAccessMode );
7571 }
7672 }
7975$ fNamePref = rtrim ($ cacheFolder , '/ ' ) . '/ ' ;
8076$ fName = $ path_parts ['filename ' ];
8177$ fNameSuf = '- ' .
82- $ params ['w ' ] .'x ' . $ params ['h ' ] . '- ' .
78+ ( isset ( $ params ['w ' ]) ? $ params [ ' w ' ] : '' ) .'x ' . ( isset ( $ params ['h ' ]) ? $ params [ ' h ' ] : '' ) . '- ' .
8379 substr (md5 (serialize ($ params ) . filemtime (MODX_BASE_PATH . $ input )), 0 , 3 ) .
8480 '. ' . $ params ['f ' ];
8581
8682$ outputFilename = MODX_BASE_PATH . $ fNamePref . $ fName . $ fNameSuf ;
87- if (!file_exists ($ outputFilename )) {
88- require_once MODX_BASE_PATH . 'assets/snippets/phpthumb/phpthumb.class.php ' ;
83+ if (! file_exists ($ outputFilename )) {
84+ if (! class_exists ('phpthumb ' )) {
85+ require_once MODX_BASE_PATH . $ phpThumbPath . '/phpthumb.class.php ' ;
86+ }
8987 $ phpThumb = new phpthumb ();
90- $ phpThumb ->config_cache_directory = MODX_BASE_PATH . $ tmpFolder ;
91- $ phpThumb ->config_temp_directory = $ tmpFolder ;
88+ $ phpThumb ->config_cache_directory = MODX_BASE_PATH . $ modx -> getCacheFolder () ;
89+ $ phpThumb ->config_temp_directory = $ modx -> getCacheFolder () ;
9290 $ phpThumb ->config_document_root = MODX_BASE_PATH ;
9391 $ phpThumb ->setSourceFilename (MODX_BASE_PATH . $ input );
9492 foreach ($ params as $ key => $ value ) {
10199 }
102100}
103101
104- return $ fNamePref . rawurlencode ($ fName ) . $ fNameSuf ;
102+ return $ fNamePref . rawurlencode ($ fName ) . $ fNameSuf ;
0 commit comments