/**
  * Apply appropriate headers to the request
  *
  * @return void
  */
 public function setHeaders()
 {
     $expire_in = getenv('CACHE_EXPIRATION_HOURS') ?: $this->cache_expiration_hours;
     // Set time for cache expires. Default to 120 days (2880 hours)
     $expires = $this->getRfcCompliantDate(Carbon::now(), $expire_in);
     $last_modified = $this->getRfcCompliantDate(Carbon::now(), 0);
     $max_age_seconds = $expire_in * 60 * 60;
     header("Last-Modified: {$last_modified}");
     header("Cache-Control: max-age={$max_age_seconds}");
     header("Expires: {$expires}");
     // File specific headers
     header('Content-Type: ' . $this->file->getMimeType());
 }
 /**
  * Set up file object
  *
  * @param string|array $allowed_hosts
  * @return \Fuzz\ImageResizer\File
  */
 public function setupFile($allowed_hosts)
 {
     if (is_string($allowed_hosts)) {
         $this->checkDomain(explode(',', $allowed_hosts));
     } else {
         $this->checkDomain($allowed_hosts);
     }
     // Setup file
     return $this->file = File::createFromBlob(file_get_contents($this->source));
 }
Example #3
0
 /**
  * Ensure that the request contains valid options
  *
  * @param array $options
  * @return array
  */
 private function validateOptions(array $options)
 {
     $required_options = ['width' => 'is_numeric', 'height' => 'is_numeric'];
     foreach ($required_options as $required => $validator) {
         if (!array_key_exists($required, $options) || is_null($options[$required]) || !$validator($options[$required])) {
             http_response_code(400);
             throw new InvalidArgumentException("The {$required} parameter is missing or invalid.");
         }
     }
     $options['format'] = $this->getOptionsKey($options, 'format', $this->file->getExtension());
     $options['crop'] = $this->getOptionsKey($options, 'crop', false);
     $options['min_quality'] = $this->getOptionsKey($options, 'min_quality', self::FULL_COMPRESSION);
     $options['max_quality'] = $this->getOptionsKey($options, 'max_quality', self::NO_COMPRESSION);
     $options['max_file_size_bytes'] = $this->getOptionsKey($options, 'max_file_size_bytes', false);
     return $options;
 }
 public function testItCanReturnRawImageData()
 {
     $blob = file_get_contents($this->getPlaceholditImage(300, 300));
     $file = File::createFromBlob($blob);
     $this->assertTrue(is_string($file->getRaw()));
 }
 public function testItSetsCompressionQuality()
 {
     $file = File::createFromBlob(file_get_contents(__DIR__ . '/images/stock-photo.jpg'));
     // ~814kb
     $image = new Image($file, false);
     $options = ['height' => '200', 'width' => '400', 'min_quality' => 2, 'max_quality' => 100, 'max_file_size_bytes' => 800000];
     $image->alterImage($options);
     $this->assertEquals(96, $image->getImageHandler()->getImageCompressionQuality());
 }
Example #6
0
 /**
  * Validate that the file passed to the constructor is an image.
  *
  * @param \Fuzz\ImageResizer\File $file
  */
 private function validateFile(File $file)
 {
     if (!$file->isImage()) {
         http_response_code(400);
         throw new InvalidArgumentException('The file is not an image. Abort!');
     }
 }
 public function getImageFile($height, $width)
 {
     return File::createFromBlob(file_get_contents($this->getPlaceholditImage($height, $width)));
 }