예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function register(Application $app)
 {
     if (!isset($app['thumbnails'])) {
         $app->register(new Thumbs\ServiceProvider());
     }
     $app['thumbnails.filesystems'] = ['files', 'themebase'];
     $app['thumbnails.filesystem_cache'] = $app->share(function ($app) {
         if (!$app['filesystem']->hasFilesystem('web')) {
             return null;
         }
         return $app['filesystem']->getFilesystem('web');
     });
     $app['thumbnails.cache'] = $app->share(function ($app) {
         return $app['cache'];
     });
     $app['thumbnails.default_image'] = $app->share(function ($app) {
         $finder = new Thumbs\Finder($app['filesystem'], ['app', 'themebase', 'files'], new Image());
         return $finder->find($app['config']->get('general/thumbnails/notfound_image'));
     });
     $app['thumbnails.error_image'] = $app->share(function ($app) {
         $finder = new Thumbs\Finder($app['filesystem'], ['app', 'themebase', 'files'], new Image());
         return $finder->find($app['config']->get('general/thumbnails/error_image'));
     });
     $app['thumbnails.cache_time'] = $app['config']->get('general/thumbnails/browser_cache_time');
     $app['thumbnails.limit_upscaling'] = !$app['config']->get('general/thumbnails/allow_upscale', false);
     $app['thumbnails.save_files'] = $app['config']->get('general/thumbnails/save_files');
     ImageResource::setNormalizeJpegOrientation($app['config']->get('general/thumbnails/exif_orientation', true));
     ImageResource::setQuality($app['config']->get('general/thumbnails/quality', 80));
 }
예제 #2
0
 public function testExifOrientation()
 {
     $images = array('1-top-left', '2-top-right', '3-bottom-right', '4-bottom-left', '5-left-top', '6-right-top', '7-right-bottom', '8-left-bottom');
     $expected = new Dimensions(400, 200);
     foreach ($images as $name) {
         $image = $this->fs->getImage('exif-orientation/' . $name . '.jpg');
         $resource = ImageResource::createFromString($image->read());
         $this->assertDimensions($expected, $resource->getDimensions());
         $color = $resource->getColorAt(new Point());
         $this->assertTrue($color->getRed() > 250 && $color->getGreen() < 10 && $color->getBlue() < 5, 'Wrong orientation');
     }
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function register(Application $app)
 {
     if (!isset($app['thumbnails'])) {
         $app->register(new Thumbs\ServiceProvider());
     }
     $app['thumbnails.filesystems'] = ['files', 'themes'];
     $app['thumbnails.save_files'] = $app['config']->get('general/thumbnails/save_files');
     $app['thumbnails.filesystem_cache'] = $app->share(function ($app) {
         if ($app['thumbnails.save_files'] === false) {
             return null;
         }
         if (!$app['filesystem']->hasFilesystem('web')) {
             return null;
         }
         return $app['filesystem']->getFilesystem('web');
     });
     $app['thumbnails.caching'] = $app['config']->get('general/caching/thumbnails');
     $app['thumbnails.cache'] = $app->share(function ($app) {
         if ($app['thumbnails.caching'] === false) {
             return null;
         }
         return $app['cache'];
     });
     $app['thumbnails.default_image'] = $app->share(function ($app) {
         $matcher = new Matcher($app['filesystem'], ['view', 'app', 'themes', 'files']);
         try {
             return $matcher->getImage($app['config']->get('general/thumbnails/notfound_image'));
         } catch (FileNotFoundException $e) {
             return new Image();
         }
     });
     $app['thumbnails.error_image'] = $app->share(function ($app) {
         $matcher = new Matcher($app['filesystem'], ['view', 'app', 'themes', 'files']);
         try {
             return $matcher->getImage($app['config']->get('general/thumbnails/error_image'));
         } catch (FileNotFoundException $e) {
             return new Image();
         }
     });
     $app['thumbnails.default_imagesize'] = $app['config']->get('general/thumbnails/default_image');
     $app['thumbnails.cache_time'] = $app['config']->get('general/thumbnails/browser_cache_time');
     $app['thumbnails.limit_upscaling'] = !$app['config']->get('general/thumbnails/allow_upscale', false);
     $app['thumbnails.only_aliases'] = $app['config']->get('general/thumbnails/only_aliases', false);
     $app['thumbnails.aliases'] = $app['config']->get('theme/thumbnails/aliases', []);
     ImageResource::setNormalizeJpegOrientation($app['config']->get('general/thumbnails/exif_orientation', true));
     ImageResource::setQuality($app['config']->get('general/thumbnails/quality', 80));
 }
예제 #4
0
 /**
  * Do the actual resize/crop/fit/border logic and return the image data.
  *
  * @param Transaction $transaction
  *
  * @return string
  */
 protected function resize(Transaction $transaction)
 {
     $crop = $transaction->getAction() === Action::CROP;
     $fit = $transaction->getAction() === Action::FIT;
     $border = $transaction->getAction() === Action::BORDER;
     try {
         $img = ImageResource::createFromString($transaction->getSrcImage()->read());
     } catch (Exception $e) {
         return false;
     }
     $target = clone $transaction->getTarget();
     $original = $img->getDimensions();
     $point = new Point();
     if ($crop) {
         $xratio = $original->getWidth() / $target->getWidth();
         $yratio = $original->getHeight() / $target->getHeight();
         // calculate x or y coordinate and width or height of source
         if ($xratio > $yratio) {
             $point->setX(round(($original->getWidth() - $original->getWidth() / $xratio * $yratio) / 2));
             $original->setWidth(round($original->getWidth() / $xratio * $yratio));
         } elseif ($yratio > $xratio) {
             $point->setY(round(($original->getHeight() - $original->getHeight() / $yratio * $xratio) / 2));
             $original->setHeight(round($original->getHeight() / $yratio * $xratio));
         }
     } elseif (!$border && !$fit) {
         $ratio = min($target->getWidth() / $original->getWidth(), $target->getHeight() / $original->getHeight());
         $target->setWidth($original->getWidth() * $ratio);
         $target->setHeight($original->getHeight() * $ratio);
     }
     $new = ImageResource::createNew($target, $img->getType());
     if ($border) {
         $new->fill($this->background);
         $tmpheight = $original->getHeight() * ($target->getWidth() / $original->getWidth());
         if ($tmpheight > $target->getHeight()) {
             $target->setWidth($original->getWidth() * ($target->getHeight() / $original->getHeight()));
             $point->setX(round(($transaction->getTarget()->getWidth() - $target->getWidth()) / 2));
         } else {
             $target->setHeight($tmpheight);
             $point->setY(round(($transaction->getTarget()->getHeight() - $target->getHeight()) / 2));
         }
     }
     if (!$crop && !$border) {
         $img->resample(new Point(), new Point(), $target, $original, $new);
     } elseif ($border) {
         $img->resample($point, new Point(), $target, $original, $new);
     } else {
         $img->resample(new Point(), $point, $target, $original, $new);
     }
     return $img->toString();
 }