Back to PhocacartFileThumbnail class

Method createFileThumbnail

public static
createFileThumbnail
(mixed $fileOriginal, mixed $fileThumbnail, mixed $size, mixed $frontUpload = 0, mixed $manager = '', mixed &$errorMsg = '')

Method createFileThumbnail - Source code

public static function createFileThumbnail($fileOriginal, $fileThumbnail, $size, $frontUpload = 0, $manager = '', &$errorMsg = '')
{
    $app = Factory::getApplication();
    $paramsC = PhocacartUtils::getComponentParameters();
    $enable_thumb_creation = $paramsC->get('enable_thumb_creation', 1);
    $watermarkParams['create'] = $paramsC->get('create_watermark', 0);
    // Watermark
    $watermarkParams['x'] = $paramsC->get('watermark_position_x', 'center');
    $watermarkParams['y'] = $paramsC->get('watermark_position_y', 'middle');
    $crop_thumbnail = $paramsC->get('crop_thumbnail', 5);
    // Crop or not
    $create_webp_copy = $paramsC->get('create_webp_copy', 0);
    $crop = null;
    switch ($size) {
        case 'small':
            if ($crop_thumbnail == 3 || $crop_thumbnail == 5 || $crop_thumbnail == 6 || $crop_thumbnail == 7) {
                $crop = 1;
            }
            break;
        case 'medium':
            if ($crop_thumbnail == 2 || $crop_thumbnail == 4 || $crop_thumbnail == 5 || $crop_thumbnail == 7) {
                $crop = 1;
            }
            break;
        case 'large':
        default:
            if ($crop_thumbnail == 1 || $crop_thumbnail == 4 || $crop_thumbnail == 6 || $crop_thumbnail == 7) {
                $crop = 1;
            }
            break;
    }
    // disable or enable the thumbnail creation
    if ($enable_thumb_creation == 1) {
        $fileResize = PhocacartFileThumbnail::getThumbnailResize($size);
        if (File::exists($fileOriginal)) {
            //file doesn't exist, create thumbnail
            $doThumbnail = false;
            if (!File::exists($fileThumbnail)) {
                $doThumbnail = true;
            }
            // WEBP COPY
            if ($create_webp_copy && !File::exists(PhocacartFile::changeFileExtension($fileThumbnail, 'webp'))) {
                $doThumbnail = true;
                self::deleteFileThumbnail($fileOriginal, 1, 1, 1, $manager);
            }
            if ($doThumbnail) {
                $errorMsg = 'Error4';
                //Don't do thumbnail if the file is smaller (width, height) than the possible thumbnail
                list($width, $height) = GetImageSize($fileOriginal);
                //larger
                /*phocacart import('phocacart.image.imagemagic');*/
                if ($width > $fileResize['width'] || $height > $fileResize['height']) {
                    $imageMagic = PhocacartImageMagic::imageMagic($fileOriginal, $fileThumbnail, $fileResize['width'], $fileResize['height'], $crop, null, $watermarkParams, $frontUpload, $manager, $errorMsg);
                } else {
                    $imageMagic = PhocacartImageMagic::imageMagic($fileOriginal, $fileThumbnail, $width, $height, $crop, null, $watermarkParams, $frontUpload, $manager, $errorMsg);
                }
                if ($imageMagic) {
                    return true;
                } else {
                    return false;
                    // error Msg will be taken from imageMagic
                }
            } else {
                $errorMsg = 'ThumbnailExists';
                //thumbnail exists
                return false;
            }
        } else {
            $errorMsg = 'ErrorFileOriginalNotExists';
            return false;
        }
        $errorMsg = 'Error3';
        return false;
    } else {
        $errorMsg = 'DisabledThumbCreation';
        // User have disabled the thumbanil creation e.g. because of error
        return false;
    }
}