/** * Save the image based on its file type. * @param string $savePath Where to save the image * @return boolean */ public function save($savePath) { $image = $this->image; $imageQuality = $this->getOption('quality'); // Determine the image type from the destination file $extension = FileHelper::extension($savePath) ?: $this->extension; // Create and save an image based on it's extension switch (strtolower($extension)) { case 'jpg': case 'jpeg': // Check JPG support is enabled if (imagetypes() & IMG_JPG) { imagejpeg($image, $savePath, $imageQuality); } break; case 'gif': // Check GIF support is enabled if (imagetypes() & IMG_GIF) { imagegif($image, $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; // Check PNG support is enabled if (imagetypes() & IMG_PNG) { imagepng($image, $savePath, $invertScaleQuality); } break; default: throw new Exception(sprintf('Invalid image type: %s. Accepted types: jpg, gif, png.', $extension)); break; } // Remove the resource for the resized image imagedestroy($image); }
/** * Returns the file extension. */ public function getExtension() { return FileHelper::extension($this->file_name); }