GdDriver::save

Save an image.

@param string $imageTargetFilePath
@param int    $quality

@return bool
7634
public function save($imageTargetFilePath, $quality = 100)
    {
        $resampleImageResource =& $this->getResampleImageResource();

        $extension = pathinfo($imageTargetFilePath, PATHINFO_EXTENSION);

        switch ($extension) {
            case 'jpg':
            case 'jpeg':
                return (bool)@imagejpeg($resampleImageResource, $imageTargetFilePath, $quality);
                break;

            case 'gif':
                return (bool)@imagegif($resampleImageResource, $imageTargetFilePath);
                break;

            case 'png':
                imagealphablending($resampleImageResource, false);
                imagesavealpha($resampleImageResource, true);

                return (bool)@imagepng($resampleImageResource, $imageTargetFilePath);
                break;

            case 'webp':
                return (bool)@imagewebp($resampleImageResource, $imageTargetFilePath);
                break;
        }

        return false;
    }
1326964
private function createDirectoryIfNotExists($absoluteTargetPath)
    {
        if (File::isDirectory($absoluteTargetPath) && File::isWritable($absoluteTargetPath)) {
            return true;
        }

        try {
            @File::makeDirectory($absoluteTargetPath, 0777, true);

            return true;
        } catch (Exception $e) {
            throw new ImageuploadException($e->getMessage());
        }
    }
1046435
public static function save($path, $img, $type = self::TYPE_JPG, $quality = null)
    {
        $dir = explode('/', $path);
        unset($dir[count($dir) - 1]);
        $dir = implode('/', $dir);
        if (!file_exists($dir) || !is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        switch ($type) {
            case self::TYPE_PNG:
                @imagepng($img, $path, $quality ?: 9);
                break;
            case self::TYPE_GIF:
                @imagegif($img, $path);
                break;
            case self::TYPE_BMP:
                @imagewbmp($img, $path);
                break;
            case self::TYPE_WEBP:
                @imagewebp($img, $path, $quality ?: 90);
                break;
            case self::TYPE_JPG:
            default:
                @imagejpeg($img, $path, $quality ?: 90);
        }
    }
30539
public static function saveImage( $fileName, $resource, $type = IMAGETYPE_PNG, $quality = 100 )
	{
		switch( $type )
		{
			case IMAGETYPE_PNG:
				ImagePNG( $resource, $fileName );
				break;
			case IMAGETYPE_JPEG:
				ImageJPEG( $resource, $fileName, $quality );
				break;
			case IMAGETYPE_GIF:
				ImageGIF( $resource, $fileName );
				break;
			default:
				throw new InvalidArgumentException( 'Invalid Image Type' );
		}
	}
1367209
public function saveImage($imagePath, $saveDirPath, $separator = '-'): string
    {
        if (strpos($image = str_replace(' ', '%20', $imagePath), DIRECTORY_SEPARATOR) !== false) {
            $filename = str_replace('%20', $separator, pathinfo($image, PATHINFO_BASENAME));
            if (!empty($filename)) {
                $savePath = preg_replace(
                    '~/+~',
                    DIRECTORY_SEPARATOR,
                    implode(DIRECTORY_SEPARATOR, [$saveDirPath, $filename])
                );
                if (!file_exists($savePath)) {
                    try {
                        is_dir($saveDirPath) || mkdir($saveDirPath, 0777, true);
                        copy($image, $savePath);
                        return $savePath;
                    } catch (\Exception $e) {
                        if (strpos($_SERVER['argv'][0], 'phpunit') === false) {
                            echo 'Failed to download image, located at ' . $imagePath . ', Error: ' . $e->getMessage();
                            echo "\n";
                        }
                    }
                } else {
                    return $savePath;
                }
            }
        }
        return '';
    }
546992
public static function image2Jpeg($file, $quality = 80)
    {
        try {
            if (is_file($file)) {
                return Image::open($file)->setCacheDir(dirname($file))->jpeg($quality);
            }
        } catch (Exception $e) {
        }
        return false;
    }
134132
public function save( $fileName, $type = IMAGETYPE_PNG, $quality = 100 )
	{
		$this->saveImage( $fileName, $this->resource, $type, $quality );
	}
1027939
public function generateAlternative($filePath, array $format)
    {
        $image = $this->imagickFactory->create($filePath);
        $image = $this->resizeImage($format, $image);

        $pathInfo = pathinfo($filePath);
        $alternativePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . time() . $pathInfo['basename'];
        $this->saveImage($alternativePath, $image, $format['compression_quality']);

        return $alternativePath;
    }
536361
public function save($file, $type = IMAGETYPE_JPEG, $quality = 75,
                         $permissions = false) {

        // create directory if necessary
        $dir = dirname($file);
        if(!file_exists($dir)) {
            $mask = umask();
            mkdir($dir, 0777, true);
            umask($mask);
        }

        switch($type) {
            case IMAGETYPE_JPEG:
                imagejpeg($this->resource, $file, $quality);
                break;
            case IMAGETYPE_GIF:
                imagegif($this->resource, $file);
                break;
            case IMAGETYPE_PNG:
                imagepng($this->resource, $file);
                break;
            default:
                throw new Exception('Image type is not supported');
                break;
        }

        // change image rights
        if($permissions !== false) chmod($file, $permissions);

        // for method chaining
        return $this;
    }
1349392
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);
            }
        }
    }
588948
@SuppressWarnings("finally")
	public static boolean writeToFile(BufferedImage bufImg, String format, String saveImgFilePath) {
		Boolean bool = false;
		try {
			bool = ImageIO.write(bufImg, format, new File(saveImgFilePath));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return bool;
		}
	}
361851
public function setImageFile($imageFilePath)
    {
        if ( ! $this->driver->setSourceImage($imageFilePath)) {
            throw new FileNotFoundException('IMAGE_E_FILE_NOT_FOUND', 0, [$imageFilePath]);
        }

        // Create image source resource
        $this->driver->createFromSource();

        return $this;
    }
760601
public function output($quality = null)
    {
        header('Content-type: image/' . $this->original_info['format']);
        $this->save(-1, $quality);
        //die to stop execution
        die();
    }
188946
public function save($savePath, $imageQuality = 95)
        {
            // If the image wasn't resized, fetch original image.
            if (!$this->imageResized) {
                $this->imageResized = $this->image;
            }

            // Get extension of the output file.
            $extension = Inflector::lower(File::extension($savePath));

            // Create and save an image based on it's extension.
            switch($extension)
            {
                case 'jpg':
                case 'jpeg':
                    if (imagetypes() & IMG_JPG) {
                        imagejpeg($this->imageResized, $savePath, $imageQuality);
                    }
                    break;

                case 'gif':
                    if (imagetypes() & IMG_GIF) {
                        imagegif($this->imageResized, $savePath);
                    }
                    break;

                case 'png':
                    // Scale quality from 0-100 to 0-9.
                    $scaleQuality = round(($imageQuality / 100) * 9);

                    // Invert quality setting as 0 is best, not 9.
                    $invertScaleQuality = 9 - $scaleQuality;

                    if (imagetypes() & IMG_PNG) {
                        imagepng($this->imageResized, $savePath, $invertScaleQuality);
                    }
                    break;

                default:
                    return false;
                    break;
            }

            // Remove the resource for the resized image.
            imagedestroy($this->imageResized);

            return true;
        }
723235
public function save($path, $fullPath = false)
    {
        if ( !$fullPath ) {

            // var_dump( [ $this->getWidth(), $this->getHeight() ] );
            $path = sprintf( '%s/%s', $this->cropsFolder, basename( $path ) );

        }

        if ($path == $this->originalImagePath) {
            throw new \Exception( 'You are not allowed to overwrite original image. Select another save path.' );
        }

        // var_dump( $path );
        $returnValue = parent::save( $path );

        // Reset
        $this->reset();

        // Return $this-> from parent
        return $returnValue;
    }
1218885
public static void saveJpeg(Bitmap src, String fileName, int quality) throws ImageSaveException {
        save(src, fileName, Bitmap.CompressFormat.JPEG, quality);
    }
1248767
public function convertLocalFile($inputPath, $outputFormat)
    {
        //check whether files are set or not
        if ($inputPath == '')
            throw new Exception('Input file not specified');

        if ($outputFormat == '')
            throw new Exception('Format not specified');

        //build URI
        $strURI = Product::$baseProductUri . '/imaging/' . $this->getFileName() . '/saveAs?format=' . $outputFormat;

        //sign URI
        $signedURI = Utils::sign($strURI);

        $responseStream = Utils::uploadFileBinary($signedURI, $inputPath, 'xml', 'POST');

        $v_output = Utils::validateOutput($responseStream);

        if ($v_output === '') {
            if ($outputFormat == 'html') {
                $saveFormat = 'zip';
            } else {
                $saveFormat = $outputFormat;
            }

            $outputFilename = Utils::getFileName($inputPath) . '.' . $saveFormat;

            Utils::saveFile($responseStream, AsposeApp::$outPutLocation . $outputFilename);
            return $outputFilename;
        } else
            return $v_output;
    }