SimpleImage::output PHP Method

output() public method

Outputs image without saving
public output ( null | string $format = null, integer | null $quality = null )
$format null | string If omitted or null - format of original file will be used, may be gif|jpg|png
$quality integer | null Output image quality in percents 0-100
    function output($format = null, $quality = null)
    {
        // Determine quality
        $quality = $quality ?: $this->quality;
        // Determine mimetype
        switch (strtolower($format)) {
            case 'gif':
                $mimetype = 'image/gif';
                break;
            case 'jpeg':
            case 'jpg':
                imageinterlace($this->image, true);
                $mimetype = 'image/jpeg';
                break;
            case 'png':
                $mimetype = 'image/png';
                break;
            default:
                $info = empty($this->imagestring) ? getimagesize($this->filename) : getimagesizefromstring($this->imagestring);
                $mimetype = $info['mime'];
                unset($info);
                break;
        }
        // Output the image
        header('Content-Type: ' . $mimetype);
        switch ($mimetype) {
            case 'image/gif':
                imagegif($this->image);
                break;
            case 'image/jpeg':
                imagejpeg($this->image, null, round($quality));
                break;
            case 'image/png':
                imagepng($this->image, null, round(9 * $quality / 100));
                break;
            default:
                throw new Exception('Unsupported image format: ' . $this->filename);
                break;
        }
        // Since no more output can be sent, call the destructor to free up memory
        $this->__destruct();
    }

Usage Example

Beispiel #1
0
 static function Thumbnail($image_file, $thumb_file, $width = 48, $height = 48)
 {
     $ctype = FSS_Helper::datei_mime("png");
     // thumb file exists
     if (file_exists($thumb_file) && filesize($thumb_file) > 0) {
         header("Content-Type: " . $ctype);
         header('Cache-control: max-age=' . 60 * 60 * 24 * 365);
         header('Expires: ' . gmdate(DATE_RFC1123, time() + 60 * 60 * 24 * 365));
         @readfile($thumb_file);
         exit;
     }
     require_once JPATH_SITE . DS . 'components' . DS . 'com_fss' . DS . 'helper' . DS . 'third' . DS . 'simpleimage.php';
     $im = new SimpleImage();
     $im->load($image_file);
     if (!$im->image) {
         // return a blank thumbnail of some sort!
         $im->load(JPATH_SITE . DS . 'components' . DS . 'com_fss' . DS . 'assets' . DS . 'images' . DS . 'blank_16.png');
     }
     $im->resize($width, $height);
     $im->output();
     $im_data = ob_get_clean();
     if (strlen($im_data) > 0) {
         // if so use JFile to write the thumbnail image
         JFile::write($thumb_file, $im_data);
     } else {
         // it failed for some reason, try doing a direct write of the thumbnail
         $im->save($thumb_file);
     }
     header('Cache-control: max-age=' . 60 * 60 * 24 * 365);
     header('Expires: ' . gmdate(DATE_RFC1123, time() + 60 * 60 * 24 * 365));
     header("Content-Type: " . $ctype);
     if (file_exists($thumb_file && filesize($thumb_file) > 0)) {
         @readfile($thumb_file);
     } else {
         $im->output();
     }
     exit;
 }
All Usage Examples Of SimpleImage::output