Skip to content

Instantly share code, notes, and snippets.

@ChoiZ
Created December 3, 2012 18:02
Show Gist options
  • Save ChoiZ/4196750 to your computer and use it in GitHub Desktop.
Save ChoiZ/4196750 to your computer and use it in GitHub Desktop.
Update SimpleImage.class.php
<?php
/* [...] */
//
// Save an image
//
// $filename - the filename to save to (defaults to original file)
// $quality - 0-100 percent for PNG or JPG
//
// Notes:
//
// The resulting format will be determined by the file extension.
//
public function save($filename = null, $quality = null) {
if( !$filename ) $filename = $this->filename;
// Determine format via file extension (fall back to original format)
$format = $this->file_ext($filename);
if( !$format ) $format = $this->original_info['format'];
// Determine output format
switch( $format ) {
case 'gif':
$result = imagegif($this->image, $filename);
break;
case 'jpg':
case 'jpeg':
if($quality === null || $quality > 100) {
$quality = 85;
}
$quality = $this->keep_within($quality, 0, 100);
$result = imagejpeg($this->image, $filename, $quality);
break;
case 'png':
if($quality === null || $quality > 90) {
$quality = 90;
}
$quality = intval($quality / 10);
$quality = $this->keep_within($quality, 0, 9);
$result = imagepng($this->image, $filename, $quality);
break;
/* [...] */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment