ImagemagickDriver::save

Save an image.

@param string $imageTargetFilePath
@param int    $quality

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

        $extension = pathinfo($imageTargetFilePath, PATHINFO_EXTENSION);

        switch ($extension) {
            case 'jpg':
            case 'jpeg':
                $resampleImageResource->setImageFormat('jpg');
                $resampleImageResource->setCompression(\Imagick::COMPRESSION_LOSSLESSJPEG);
                break;

            case 'gif':
                $resampleImageResource->setImageFormat('gif');
                $resampleImageResource->setCompression(\Imagick::COMPRESSION_UNDEFINED);
                break;

            case 'png':
                $resampleImageResource->setImageFormat('png');
                $resampleImageResource->setCompression(\Imagick::COMPRESSION_UNDEFINED);
                $resampleImageResource->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
                $resampleImageResource->setBackgroundColor(new \ImagickPixel('transparent'));
                break;

            case 'webp':
                $resampleImageResource->setImageFormat('webp');
                $resampleImageResource->setCompression(\Imagick::COMPRESSION_UNDEFINED);
                $resampleImageResource->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
                $resampleImageResource->setBackgroundColor(new \ImagickPixel('transparent'));
                break;
        }

        $resampleImageResource->setCompressionQuality($quality);
        $resampleImageResource->stripImage();

        return (bool)$resampleImageResource->writeImage($imageTargetFilePath);
    }
195457
public function savePdf($targetPath)
    {
        $this->callBrowser($this->createPdfCommand($targetPath));
        if (! file_exists($targetPath)) {
            throw CouldNotTakeBrowsershot::chromeOutputEmpty($targetPath);
        }
    }
1295690
public function saveFile($filePath, $fileContent)
    {
        if (false === is_dir(dirname($filePath))) {
            mkdir(dirname($filePath), 0755);
        }

        return file_put_contents($filePath, $fileContent);
    }
63011
public function saveImage($savePath, $imageQuality = "100")
		# Author:     Jarrod Oberto
		# Date:       27-02-08
		# Purpose:    Saves the image
		# Param in:   $savePath: Where to save the image including filename:
		#             $imageQuality: image quality you want the image saved at 0-100
		# Param out:  n/a
		# Reference:
		# Notes:    * gif doesn't have a quality parameter
		#       * jpg has a quality setting 0-100 (100 being the best)
		#       * png has a quality setting 0-9 (0 being the best)
		#
		#             * bmp files have no native support for bmp files. We use a
		#       third party class to save as bmp.
	{

		// *** Perform a check or two.
		if ( ! is_resource($this->imageResized))
		{
			if ($this->debug)
			{
				throw new Exception('saveImage: This is not a resource.');
			}
			else
			{
				throw new Exception();
			}
		}
		$fileInfoArray = pathInfo($savePath);
		clearstatcache();
		if ( ! is_writable($fileInfoArray['dirname']))
		{
			if ($this->debug)
			{
				throw new Exception('The path is not writable. Please check your permissions.');
			}
			else
			{
				throw new Exception();
			}
		}

		// *** Get extension
		$extension = strrchr($savePath, '.');
		$extension = fix_strtolower($extension);

		$error = '';

		switch ($extension)
		{
			case '.jpg':
			case '.jpeg':
				$this->checkInterlaceImage($this->isInterlace);
				if (imagetypes() & IMG_JPG)
				{
					imagejpeg($this->imageResized, $savePath, $imageQuality);
				}
				else
				{
					$error = 'jpg';
				}
				break;

			case '.gif':
				$this->checkInterlaceImage($this->isInterlace);
				if (imagetypes() & IMG_GIF)
				{
					imagegif($this->imageResized, $savePath);
				}
				else
				{
					$error = 'gif';
				}
				break;

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

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

				$this->checkInterlaceImage($this->isInterlace);
				if (imagetypes() & IMG_PNG)
				{
					imagepng($this->imageResized, $savePath, $invertScaleQuality);
				}
				else
				{
					$error = 'png';
				}
				break;

			case '.bmp':
				file_put_contents($savePath, $this->GD2BMPstring($this->imageResized));
				break;


			// ... etc

			default:
				// *** No extension - No save.
				$this->errorArray[] = 'This file type (' . $extension . ') is not supported. File not saved.';
				break;
		}

		//imagedestroy($this->imageResized);

		// *** Display error if a file type is not supported.
		if ($error != '')
		{
			$this->errorArray[] = $error . ' support is NOT enabled. File not saved.';
		}
	}
1286853
public function downloadProcessedImage(ProcessedImage $processedImage, $savePath) {
    $url = $processedImage->fileUrls[0];

    if ($this->accessToken != null) {
      $url .= "?access_token=" . $this->accessToken->token();
    }

    self::downloadImage($url, $savePath);
  }
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' );
		}
	}
169033
public function save(string $path, string $name, ?string $extension = null, int $quality = 100): bool
    {
        if (false === is_dir($path)) {
            mkdir($path, 0777, true);
        }
        if (null === $extension) {
            /** @var string $extension */
            $extension = $this->extension;
        }
        if (true === $this->encrypted) {
            file_put_contents($path . $name . '.' . self::FORMAT_WD_IMAGE, $this->workingData);
            return true;
        } elseif (0 !== imagetypes()) {
            $filename = $path . $name . '.' . $extension;
            switch (strtolower($extension)) {
                case self::FORMAT_GIF:
                    imagegif($this->workingData, $filename);
                    break;
                case self::FORMAT_JPEG:
                case self::FORMAT_JPG:
                    imagejpeg($this->workingData, $filename, $quality);
                    break;
                case self::FORMAT_PNG:
                    imagepng($this->workingData, $filename, (int)round(9 - ((9 * $quality) / 100)));
                    break;
                default:
                    throw new ImageException('Unsupported file type');
            }
            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());
        }
    }
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;
    }
639673
public function output($image_type = null, $quality = null)
    {
        $image_type = $image_type ?: $this->source_type;

        header('Content-Type: ' . image_type_to_mime_type($image_type));

        $this->save(null, $image_type, $quality);
    }
1215676
protected function saveFileContent ($fullPath = '', & $fileContent = '') {
		$toolClass = \MvcCore\Application::GetInstance()->GetToolClass();
		$toolClass::SingleProcessWrite($fullPath, $fileContent);
		@chmod($fullPath, 0766);
	}
597849
public function output($type = 'jpg', $quality = 75)
        {
            if($type == 'jpg' && (imagetypes() & IMG_JPG)) {
                header("Content-Type: image/jpeg");
                imagejpeg($this->im, null, $quality);
                return true;
            } elseif($type == 'png' && (imagetypes() & IMG_PNG)) {
                header("Content-Type: image/png");
                imagepng($this->im);
                return true;
            } elseif($type == 'gif' && (imagetypes() & IMG_GIF)) {
                header("Content-Type: image/gif");
                imagegif($this->im);
                return true;
            } else
                return false;
        }
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;
    }
1250934
public function saveImage(FileCreateOrUpdateEvent $event, FileModelInterface $imageModel)
    {
        return $this->saveFile($event->getParentId(), $imageModel);
    }
570973
public function getImageQuality(string $path, int $quality = null): int
    {
        $desiredQuality = (!$quality) ? ImageResizer::$plugin->getSettings()->imageQuality : $quality;
        $desiredQuality = (!$desiredQuality) ? Craft::$app->config->general->defaultImageQuality : $desiredQuality;

        if (@pathinfo($path, PATHINFO_EXTENSION) == 'png') {
            // Valid PNG quality settings are 0-9, so normalize and flip, because we're talking about compression
            // levels, not quality, like jpg and gif.
            $quality = (int)round(($desiredQuality * 9) / 100);
            $quality = 9 - $quality;

            if ($quality < 0) {
                $quality = 0;
            }

            if ($quality > 9) {
                $quality = 9;
            }
        } else {
            $quality = $desiredQuality;
        }

        return $quality;
    }
326193
protected function saveImage($filePath, Imagick $image, $compression_quality)
    {
        $image->setImageCompression(Imagick::COMPRESSION_JPEG);
        $image->setImageCompressionQuality($compression_quality);
        $image->stripImage();
        $image->writeImage($filePath);

        return $image;
    }
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;
		}
	}