private function saveAsWebp($imageInstance, $path, $sourceExtension, $saveOptions)
    {
        /** @var ConfigModel $settings */
        $config = ImagerService::getConfig();

        if ($config->getSetting('useCwebp')) {

            // Save temp file
            $tempFile = $this->saveTemporaryFile($imageInstance, $sourceExtension);

            // Convert to webp with cwebp
            $command = escapeshellcmd($config->getSetting('cwebpPath').' '.$config->getSetting('cwebpOptions').' -q '.$saveOptions['webp_quality'].' "'.$tempFile.'" -o "'.$path.'"');
            $r = shell_exec($command);

            if (!file_exists($path)) {
                $msg = Craft::t('imager', 'Creation of webp with cwebp failed with error "'.$r. '". The executed command was "' . $command . '"');
                Craft::error($msg, __METHOD__);
                throw new ImagerException($msg);
            }

            // Delete temp file
            unlink($tempFile);
        } else {
            if (ImagerService::$imageDriver === 'gd') {
                /** @var GdImage $imageInstance */
                $instance = $imageInstance->getGdResource();

                if (false === /** @scrutinizer ignore-call */ \imagewebp($instance, $path, $saveOptions['webp_quality'])) {
                    $msg = Craft::t('imager', 'GD webp save operation failed');
                    Craft::error($msg, __METHOD__);
                    throw new ImagerException($msg);
                }

                // Fix for corrupt file bug (http://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files)
                if (filesize($path) % 2 === 1) {
                    file_put_contents($path, "\0", FILE_APPEND);
                }
            }

            if (ImagerService::$imageDriver === 'imagick') {
                 /** @var ImagickImage $imageInstance */
                $instance = $imageInstance->getImagick();

                $instance->setImageFormat('webp');

                $hasTransparency = $instance->getImageAlphaChannel();

                if ($hasTransparency) {
                    $instance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
                    $instance->setBackgroundColor(new \ImagickPixel('transparent'));
                }

                $instance->setImageCompressionQuality($saveOptions['webp_quality']);
                $imagickOptions = $saveOptions['webp_imagick_options'];

                if ($imagickOptions && \count($imagickOptions) > 0) {
                    foreach ($imagickOptions as $key => $val) {
                        $instance->setOption('webp:'.$key, $val);
                    }
                }

                $instance->writeImage($path);
            }
        }
    }
private function saveAsWebp($imageInstance, $path, $sourceExtension, $saveOptions)
    {
        /** @var ConfigModel $settings */
        $config = ImagerService::getConfig();

        if ($config->getSetting('useCwebp')) {

            // Save temp file
            $tempFile = $this->saveTemporaryFile($imageInstance, $sourceExtension);

            // Convert to webp with cwebp
            $command = escapeshellcmd($config->getSetting('cwebpPath').' '.$config->getSetting('cwebpOptions').' -q '.$saveOptions['webp_quality'].' "'.$tempFile.'" -o "'.$path.'"');
            $r = shell_exec($command);

            if (!file_exists($path)) {
                $msg = Craft::t('imager', 'Creation of webp with cwebp failed with error "'.$r. '". The executed command was "' . $command . '"');
                Craft::error($msg, __METHOD__);
                throw new ImagerException($msg);
            }

            // Delete temp file
            unlink($tempFile);
        } else {
            if (ImagerService::$imageDriver === 'gd') {
                /** @var GdImage $imageInstance */
                $instance = $imageInstance->getGdResource();

                if (false === /** @scrutinizer ignore-call */ \imagewebp($instance, $path, $saveOptions['webp_quality'])) {
                    $msg = Craft::t('imager', 'GD webp save operation failed');
                    Craft::error($msg, __METHOD__);
                    throw new ImagerException($msg);
                }

                // Fix for corrupt file bug (http://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files)
                if (filesize($path) % 2 === 1) {
                    file_put_contents($path, "\0", FILE_APPEND);
                }
            }

            if (ImagerService::$imageDriver === 'imagick') {
                 /** @var ImagickImage $imageInstance */
                $instance = $imageInstance->getImagick();

                $instance->setImageFormat('webp');

                $hasTransparency = $instance->getImageAlphaChannel();

                if ($hasTransparency) {
                    $instance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
                    $instance->setBackgroundColor(new \ImagickPixel('transparent'));
                }

                $instance->setImageCompressionQuality($saveOptions['webp_quality']);
                $imagickOptions = $saveOptions['webp_imagick_options'];

                if ($imagickOptions && \count($imagickOptions) > 0) {
                    foreach ($imagickOptions as $key => $val) {
                        $instance->setOption('webp:'.$key, $val);
                    }
                }

                $instance->writeImage($path);
            }
        }
    }
private function saveAsWebp ( $ imageInstance , $ path , $ sourceExtension , $ saveOptions ) { /** @var ConfigModel $settings */ $ config = ImagerService :: getConfig ( ) ; if ( $ config -> getSetting ( 'useCwebp' ) ) { // Save temp file $ tempFile = $ this -> saveTemporaryFile ( $ imageInstance , $ sourceExtension ) ; // Convert to webp with cwebp $ command = escapeshellcmd ( $ config -> getSetting ( 'cwebpPath' ) . ' ' . $ config -> getSetting ( 'cwebpOptions' ) . ' -q ' . $ saveOptions [ 'webp_quality' ] . ' "' . $ tempFile . '" -o "' . $ path . '"' ) ; $ r = shell_exec ( $ command ) ; if ( ! file_exists ( $ path ) ) { $ msg = Craft :: t ( 'imager' , 'Creation of webp with cwebp failed with error "' . $ r . '". The executed command was "' . $ command . '"' ) ; Craft :: error ( $ msg , __METHOD__ ) ; throw new ImagerException ( $ msg ) ; } // Delete temp file unlink ( $ tempFile ) ; } else { if ( ImagerService :: $ imageDriver === 'gd' ) { /** @var GdImage $imageInstance */ $ instance = $ imageInstance -> getGdResource ( ) ; if ( false === /** @scrutinizer ignore-call */ \ imagewebp ( $ instance , $ path , $ saveOptions [ 'webp_quality' ] ) ) { $ msg = Craft :: t ( 'imager' , 'GD webp save operation failed' ) ; Craft :: error ( $ msg , __METHOD__ ) ; throw new ImagerException ( $ msg ) ; } // Fix for corrupt file bug (http://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files) if ( filesize ( $ path ) % 2 === 1 ) { file_put_contents ( $ path , "\0" , FILE_APPEND ) ; } } if ( ImagerService :: $ imageDriver === 'imagick' ) { /** @var ImagickImage $imageInstance */ $ instance = $ imageInstance -> getImagick ( ) ; $ instance -> setImageFormat ( 'webp' ) ; $ hasTransparency = $ instance -> getImageAlphaChannel ( ) ; if ( $ hasTransparency ) { $ instance -> setImageAlphaChannel ( \ Imagick :: ALPHACHANNEL_ACTIVATE ) ; $ instance -> setBackgroundColor ( new \ ImagickPixel ( 'transparent' ) ) ; } $ instance -> setImageCompressionQuality ( $ saveOptions [ 'webp_quality' ] ) ; $ imagickOptions = $ saveOptions [ 'webp_imagick_options' ] ; if ( $ imagickOptions && \ count ( $ imagickOptions ) > 0 ) { foreach ( $ imagickOptions as $ key => $ val ) { $ instance -> setOption ( 'webp:' . $ key , $ val ) ; } } $ instance -> writeImage ( $ path ) ; } } }
Saves image as webp

@param GdImage|ImagickImage $imageInstance
@param string               $path
@param string               $sourceExtension
@param array                $saveOptions

@throws ImagerException
Saves image as webp