Esempio n. 1
0
 /**
  * @param array $options ['etag'=> bool, 'file_get_contents' => bool]
  */
 public function __construct($filename, $options = array('etag' => false))
 {
     $this->filename = $filename;
     $this->etag = array_value($options, 'etag');
     if (!file_exists($filename)) {
         if (basename($filename) == 'index.html') {
             $this->error = new HttpError(403);
         } else {
             $this->error = new HttpError(404);
         }
         return;
     }
     $last_modified = filemtime($filename);
     if ($last_modified === false) {
         $this->error = new HttpError(500);
         return;
     }
     if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
         $if_modified_since = strtotime(preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']));
         if ($if_modified_since >= $last_modified) {
             // Is the Cached version the most recent?
             $this->notModified = true;
             return;
         }
     }
     if ($this->etag) {
         $etag = md5_file($filename);
         if (array_value($_SERVER, 'HTTP_IF_NONE_MATCH') === $etag) {
             $this->notModified = true;
             return;
         }
         $this->headers[] = 'ETag: ' . md5_file($filename);
     }
     $this->notModified = false;
     if (is_dir($filename)) {
         $this->error = new HttpError(403);
         return;
     }
     $this->headers['Content-Type'] = \Sledgehammer\mimetype($filename);
     $this->headers['Last-Modified'] = gmdate('r', $last_modified);
     $filesize = filesize($filename);
     if ($filesize === false) {
         $this->error = new HttpError(500);
         return;
     }
     $this->headers['Content-Length'] = $filesize;
     // @todo Detecteer bestanden groter dan 2GiB, deze geven fouten.
     if (array_value($options, 'file_get_contents')) {
         $this->fileContents = file_get_contents($filename);
     }
 }
Esempio n. 2
0
 protected function rasterize()
 {
     if ($this->gd === null) {
         $mimetype = \Sledgehammer\mimetype($this->filename, true, 'UNKNOWN');
         if ($mimetype == 'UNKNOWN' || $this->createFromMimetype($mimetype) == false) {
             $imageInfo = getimagesize($this->filename);
             if (isset($imageInfo['mime'])) {
                 $detectedMimetype = $imageInfo['mime'];
             } else {
                 notice('Imagetype detection failed');
                 $detectedMimetype = false;
             }
             if ($detectedMimetype === false || $this->createFromMimetype($detectedMimetype) == false) {
                 throw new Exception('Unable to load "' . $this->filename . '"');
             }
             if ($mimetype != 'UNKNOWN') {
                 notice('Invalid extension, detected mimetype: "' . $detectedMimetype . '" for "' . $this->filename . '"');
             }
         }
     }
     return $this->gd;
 }
Esempio n. 3
0
 protected function isImage($filename)
 {
     return substr(\Sledgehammer\mimetype($filename, true), 0, 6) == 'image/';
 }
Esempio n. 4
0
 /**
  * Save the graphic as an image.
  *
  * @param string $filename
  * @param array  $options
  */
 public function saveAs($filename, $options = array())
 {
     $defaults = array('mimetype' => null, 'quality' => 85);
     $options = $options + $defaults;
     $error = 'Failed to save the image to "' . $filename . '"';
     $mimetype = $options['mimetype'];
     if ($filename === null) {
         $error = 'Failed to render the image';
     } elseif ($mimetype === null) {
         $mimetype = \Sledgehammer\mimetype($filename, true);
     }
     if ($mimetype === 'image/jpeg') {
         if (!imagejpeg($this->rasterize(), $filename, $options['quality'])) {
             throw new Exception($error);
         }
         return;
     }
     $mimetype_to_function = array('image/png' => 'imagepng', 'image/gif' => 'imagegif');
     if (isset($mimetype_to_function[$mimetype])) {
         $function = $mimetype_to_function[$mimetype];
         if (!$function($this->rasterize(), $filename)) {
             throw new Exception($error);
         }
     } else {
         warning('Unsupported mimetype: "' . $mimetype . '"');
     }
 }