SimpleImage::save PHP Method

save() public method

The resulting format will be determined by the file extension.
public save ( null | string $filename = null, null | integer $quality = null ) : SimpleImage
$filename null | string If omitted - original file will be overwritten
$quality null | integer Output image quality in percents 0-100
return SimpleImage
    function save($filename = null, $quality = null)
    {
        // Determine quality, filename, and format
        $quality = $quality ?: $this->quality;
        $filename = $filename ?: $this->filename;
        $format = $this->file_ext($filename) ?: $this->original_info['format'];
        // Create the image
        switch (strtolower($format)) {
            case 'gif':
                $result = imagegif($this->image, $filename);
                break;
            case 'jpg':
            case 'jpeg':
                imageinterlace($this->image, true);
                $result = imagejpeg($this->image, $filename, round($quality));
                break;
            case 'png':
                $result = imagepng($this->image, $filename, round(9 * $quality / 100));
                break;
            default:
                throw new Exception('Unsupported format');
        }
        if (!$result) {
            throw new Exception('Unable to save image: ' . $filename);
        }
        return $this;
    }

Usage Example

Example #1
1
 function imageDualResize($filename, $cleanFilename, $wtarget, $htarget)
 {
     if (!file_exists($cleanFilename)) {
         $dims = getimagesize($filename);
         $width = $dims[0];
         $height = $dims[1];
         while ($width > $wtarget || $height > $htarget) {
             if ($width > $wtarget) {
                 $percentage = $wtarget / $width;
             }
             if ($height > $htarget) {
                 $percentage = $htarget / $height;
             }
             /*if($width > $height)
             		{
             			$percentage = ($target / $width);
             		}
             		else
             		{
             			$percentage = ($target / $height);
             		}*/
             //gets the new value and applies the percentage, then rounds the value
             $width = round($width * $percentage);
             $height = round($height * $percentage);
         }
         $image = new SimpleImage();
         $image->load($filename);
         $image->resize($width, $height);
         $image->save($cleanFilename);
         $image = null;
     }
     //returns the new sizes in html image tag format...this is so you can plug this function inside an image tag and just get the
     return "src=\"{$baseurl}/{$cleanFilename}\"";
 }
All Usage Examples Of SimpleImage::save