Back to PhocacartFileThumbnail class

Method getOrCreateThumbnail

public static
getOrCreateThumbnail
(mixed $fileNo, mixed $refreshUrl, mixed $small = 0, mixed $medium = 0, mixed $large = 0, mixed $frontUpload = 0, mixed $manager = '')

Method getOrCreateThumbnail - Source code

/*
 * Main Thumbnail creating function
 *
 * file 		= abc.jpg
 * fileNo	= folder/abc.jpg
 * if small, medium, large = 1, create small, medium, large thumbnail
 */
public static function getOrCreateThumbnail($fileNo, $refreshUrl, $small = 0, $medium = 0, $large = 0, $frontUpload = 0, $manager = '')
{
    /*	if ($frontUpload) {
    			$returnFrontMessage = '';
    		}*/
    $returnFrontMessage = '';
    $onlyThumbnailInfo = 0;
    if ($small == 0 && $medium == 0 && $large == 0) {
        $onlyThumbnailInfo = 1;
    }
    $app = Factory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    //$additional_thumbnails 					= $paramsC->get( 'additional_thumbnails',0 );
    $path = PhocacartPath::getPath($manager);
    $origPathServer = str_replace('\\', '/', $path['orig_abs_ds']);
    $file['name'] = PhocacartFile::getTitleFromFilenameWithExt($fileNo);
    $file['name_no'] = ltrim($fileNo, '/');
    $file['name_original_abs'] = PhocacartFile::getFileOriginal($fileNo, 0, $manager);
    $file['name_original_rel'] = PhocacartFile::getFileOriginal($fileNo, 1, $manager);
    $file['path_without_file_name_original'] = str_replace($file['name'], '', $file['name_original_abs']);
    $file['path_without_file_name_thumb'] = str_replace($file['name'], '', $file['name_original_abs'] . 'thumbs' . '/');
    //$file['path_without_name']				= str_replace('\\', '/', JPath::clean($origPathServer));
    //$file['path_with_name_relative_no']		= str_replace($origPathServer, '', $file['name_original']);
    /*
    		$file['path_with_name_relative']		= $path['orig_rel_ds'] . str_replace($origPathServer, '', $file['name_original']);
    		$file['path_with_name_relative_no']		= str_replace($origPathServer, '', $file['name_original']);
    
    		$file['path_without_name']				= str_replace('\\', '/', Path::clean($origPath.'/'));
    		$file['path_without_name_relative']		= $path['orig_rel_ds'] . str_replace($origPathServer, '', $file['path_without_name']);
    		$file['path_without_name_relative_no']	= str_replace($origPathServer, '', $file['path_without_name']);
    		$file['path_without_name_thumbs'] 		= $file['path_without_name'] .'thumbs';
    		$file['path_without_file_name_original'] 			= str_replace($file['name'], '', $file['name_original']);
    		$file['path_without_name_thumbs_no'] 	= str_replace($file['name'], '', $file['name_original'] .'thumbs');*/
    $ext = strtolower(File::getExt($file['name']));
    switch ($ext) {
        case 'jpg':
        case 'png':
        case 'gif':
        case 'jpeg':
        case 'webp':
            //Get File thumbnails name
            $thumbNameS = PhocacartFileThumbnail::getThumbnailName($fileNo, 'small', $manager);
            $file['thumb_name_s_no_abs'] = $thumbNameS->abs;
            $file['thumb_name_s_no_rel'] = $thumbNameS->rel;
            $thumbNameM = PhocacartFileThumbnail::getThumbnailName($fileNo, 'medium', $manager);
            $file['thumb_name_m_no_abs'] = $thumbNameM->abs;
            $file['thumb_name_m_no_rel'] = $thumbNameM->rel;
            $thumbNameL = PhocacartFileThumbnail::getThumbnailName($fileNo, 'large', $manager);
            $file['thumb_name_l_no_abs'] = $thumbNameL->abs;
            $file['thumb_name_l_no_rel'] = $thumbNameL->rel;
            // Don't create thumbnails from watermarks...
            $dontCreateThumb = PhocacartFileThumbnail::dontCreateThumb($file['name']);
            if ($dontCreateThumb == 1) {
                $onlyThumbnailInfo = 1;
                // WE USE $onlyThumbnailInfo FOR NOT CREATE A THUMBNAIL CLAUSE
            }
            // We want only information from the pictures OR
            if ($onlyThumbnailInfo == 0) {
                $thumbInfo = $fileNo;
                //Create thumbnail folder if not exists
                $errorMsg = 'ErrorCreatingFolder';
                $creatingFolder = PhocacartFileThumbnail::createThumbnailFolder($file['path_without_file_name_original'], $file['path_without_file_name_thumb'], $errorMsg);
                switch ($errorMsg) {
                    case 'Success':
                    case 'DisabledThumbCreation':
                        break;
                    //case 'ErrorCreatingFolder':
                    //	return JText::_('COM_PHOCACART_ERROR_CREATING_THUMBS_FOLDER'); We can test to create the folder directly with file
                    default:
                        $returnFrontMessage = $errorMsg;
                        break;
                }
                // Folder must exist
                if (Folder::exists($file['path_without_file_name_thumb'])) {
                    $errorMsgS = $errorMsgM = $errorMsgL = '';
                    //Small thumbnail
                    if ($small == 1) {
                        PhocacartFileThumbnail::createFileThumbnail($file['name_original_abs'], $thumbNameS->abs, 'small', $frontUpload, $manager, $errorMsgS);
                    } else {
                        $errorMsgS = 'ThumbnailExists';
                        // in case we only need medium or large, because of if clause bellow
                    }
                    //Medium thumbnail
                    if ($medium == 1) {
                        PhocacartFileThumbnail::createFileThumbnail($file['name_original_abs'], $thumbNameM->abs, 'medium', $frontUpload, $manager, $errorMsgM);
                    } else {
                        $errorMsgM = 'ThumbnailExists';
                        // in case we only need small or large, because of if clause bellow
                    }
                    //Large thumbnail
                    if ($large == 1) {
                        PhocacartFileThumbnail::createFileThumbnail($file['name_original_abs'], $thumbNameL->abs, 'large', $frontUpload, $manager, $errorMsgL);
                    } else {
                        $errorMsgL = 'ThumbnailExists';
                        // in case we only need small or medium, because of if clause bellow
                    }
                    // Error messages for all 3 thumbnails (if the message contains error string, we got error
                    // Other strings can be:
                    // - ThumbnailExists  - do not display error message nor success page
                    // - OnlyInformation - do not display error message nor success page
                    // - DisabledThumbCreation - do not display error message nor success page
                    $creatingSError = $creatingMError = $creatingLError = false;
                    $creatingSError = preg_match("/Error/i", $errorMsgS);
                    $creatingMError = preg_match("/Error/i", $errorMsgM);
                    $creatingLError = preg_match("/Error/i", $errorMsgL);
                    // There is an error while creating thumbnail in m or in s or in l
                    if ($creatingSError || $creatingMError || $creatingLError) {
                        // if all or two errors appear, we only display the last error message
                        // because the errors in this case is the same
                        if ($errorMsgS != '') {
                            $creatingError = $errorMsgS;
                        }
                        if ($errorMsgM != '') {
                            $creatingError = $errorMsgM;
                        }
                        if ($errorMsgL != '') {
                            $creatingError = $errorMsgL;
                        }
                        // because the errors in this case is the same
                        $returnFrontMessage = Text::_('COM_PHOCACART_ERROR_CREATING_THUMBNAIL') . ' (' . $creatingError . ')';
                    } else {
                        if ($errorMsgS == '' && $errorMsgM == '' && $errorMsgL == '') {
                            $returnFrontMessage = 'Success';
                        } else {
                            if ($errorMsgS == '' && $errorMsgM == '' && $errorMsgL == 'ThumbnailExists') {
                                $returnFrontMessage = 'Success';
                            } else {
                                if ($errorMsgS == '' && $errorMsgM == 'ThumbnailExists' && $errorMsgL == 'ThumbnailExists') {
                                    $returnFrontMessage = 'Success';
                                } else {
                                    if ($errorMsgS == '' && $errorMsgM == 'ThumbnailExists' && $errorMsgL == '') {
                                        $returnFrontMessage = 'Success';
                                    } else {
                                        if ($errorMsgS == 'ThumbnailExists' && $errorMsgM == 'ThumbnailExists' && $errorMsgL == '') {
                                            $returnFrontMessage = 'Success';
                                        } else {
                                            if ($errorMsgS == 'ThumbnailExists' && $errorMsgM == '' && $errorMsgL == '') {
                                                $returnFrontMessage = 'Success';
                                            } else {
                                                if ($errorMsgS == 'ThumbnailExists' && $errorMsgM == '' && $errorMsgL == 'ThumbnailExists') {
                                                    $returnFrontMessage = 'Success';
                                                } else {
                                                    if ($errorMsgS == 'ThumbnailExists' && $errorMsgM == 'ThumbnailExists' && $errorMsgL == 'ThumbnailExists') {
                                                        $returnFrontMessage = 'SuccessThumbnailExists';
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if ($returnFrontMessage == 'Success') {
                        return '<span class="ph-result-txt ph-success-txt">' . Text::_('COM_PHOCACART_THUMBNAIL_CREATED') . '</span>';
                    } else {
                        if ($returnFrontMessage == 'SuccessThumbnailExists') {
                            return '<span class="ph-result-txt ph-success-txt">' . Text::_('COM_PHOCACART_THUMBNAIL_EXISTS') . '</span>';
                        } else {
                            return '<span class="ph-result-txt ph-error-txt">' . $returnFrontMessage . '</span>';
                        }
                    }
                }
            }
            break;
    }
    return $file;
}