partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

ImageResponse.php (1334B)


      1 <?php
      2 
      3 namespace PartKeepr\ImageBundle\Response;
      4 
      5 use Imagine\Gd\Imagine;
      6 use Imagine\Image\Box;
      7 use Imagine\Image\Point;
      8 use Symfony\Component\HttpFoundation\Response;
      9 
     10 class ImageResponse extends Response
     11 {
     12     /**
     13      * Constructs a new ImageNotFoundResponse.
     14      *
     15      * @param int    $maxWidth
     16      * @param int    $maxHeight
     17      * @param int    $code
     18      * @param string $message
     19      */
     20     public function __construct($maxWidth, $maxHeight, $code, $message)
     21     {
     22         if ($maxWidth == 0) {
     23             $maxWidth = 300;
     24         }
     25 
     26         if ($maxHeight == 0) {
     27             $maxHeight = 300;
     28         }
     29 
     30         $imagine = new Imagine();
     31 
     32         $size = new Box(300, 300);
     33         $image = $imagine->create($size);
     34 
     35         $black = $image->palette()->color('000');
     36 
     37         $path = realpath(
     38             __DIR__.
     39             '/../Resources/public/fonts/OpenSans-Regular.ttf'
     40         );
     41 
     42         $font = $imagine->font($path, 24, $black);
     43 
     44         $image->draw()->text($message, $font, new Point(0, 0));
     45 
     46         $box = $image->getSize();
     47         $box = $box->widen($maxWidth);
     48 
     49         if ($box->getHeight() > $maxHeight) {
     50             $box = $box->heighten($maxHeight);
     51         }
     52 
     53         $image->resize($box);
     54 
     55         return parent::__construct($image->get('png'), $code, ['Content-Type' => 'image/png']);
     56     }
     57 }