src/Troika/MainBundle/Helper/ImageHelper.php line 18

Open in your IDE?
  1. <?php
  2. namespace Troika\MainBundle\Helper;
  3. use Imagick;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. class ImageHelper
  6. {
  7.     public const ImagickCOLOR_OPACITY 18;
  8.     public static function preview($img$widthO 285$heightO 180$name)
  9.     {
  10.         try {
  11.             $imgFile $_SERVER['DOCUMENT_ROOT'] . $img;
  12.             $imageSize = @getimagesize($imgFile);
  13.             if (!is_array($imageSize)) {
  14.                 throw new NotFoundHttpException('Файл изображения не найден');
  15.             }
  16.             $image = new Imagick($imgFile);
  17.             $height $image->getImageHeight();
  18.             $width $image->getImageWidth();
  19.             if ($width $height) {
  20.                 $ratio $widthO $heightO $width $height;
  21.                 if ($ratio 0) {
  22.                     $heightRatio $height $width $heightO $widthO;
  23.                     $image->cropImage($width$height $heightRatio0, ($heightRatio 2));
  24.                 } else {
  25.                     $widthRatio $width $height $widthO $heightO;
  26.                     $image->cropImage($width $widthRatio$height, ($widthRatio 2), 0);
  27.                 }
  28.                 $image->resizeImage($widthO$heightOself::ImagickCOLOR_OPACITYtrue);
  29.                 $thumbnail $image->getImageBlob();
  30.             } else {
  31.                 $nWidth $width * ($heightO $height);
  32.                 $image->resizeImage(
  33.                   (int)($width * ($heightO $height)),
  34.                   (int)$heightO,
  35.                   self::ImagickCOLOR_OPACITY,
  36.                   true
  37.                 ); //Imagick::COLOR_OPACITY
  38.                 $imagePod = new Imagick();
  39.                 $imagePod->newImage($widthO$heightO, new \ImagickPixel('transparent'));
  40.                 $imagePod->setImageFormat('png');
  41.                 $imagePod->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
  42.                 $imagePod->compositeImage($image, \Imagick::COMPOSITE_PLUS, ($widthO $nWidth) / 20);
  43.                 $thumbnail $imagePod->getImageBlob();
  44.                 header("Content-Type: image/png");
  45.                 echo $thumbnail;
  46.                 return;
  47.             }
  48.             if (!$name) {
  49.                 $image->writeImage($_SERVER['DOCUMENT_ROOT'] . '/cache' $img);
  50.                 touch($_SERVER['DOCUMENT_ROOT'] . '/cache' $img);
  51.             } else {
  52.                 $image->writeImage($_SERVER['DOCUMENT_ROOT'] . '/cache' $name);
  53.                 touch($_SERVER['DOCUMENT_ROOT'] . '/cache' $name);
  54.             }
  55.             header("Content-Type: image/jpeg");
  56.             echo $thumbnail;
  57.         } catch (\Exception $e) {
  58.             throw new NotFoundHttpException('Изображение не найдено',$e);
  59.         }
  60.     }
  61.     public static function watermark($img)
  62.     {
  63.         try {
  64.             if (class_exists('Imagick')) {
  65.                 $image = new Imagick($_SERVER['DOCUMENT_ROOT'] . $img);
  66.                 $wm = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/troika.png');
  67.                 $widthRatio $image->getImageWidth() / 1096;
  68.                 $height $wm->getImageHeight() * $widthRatio;
  69.                 //$wm->adaptiveResizeImage($image->getImageWidth(), $height, false);
  70.                 $wm->scaleImage($image->getImageWidth(), $heightfalse);
  71.                 
  72.                 $image->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
  73.                 $image->compositeImage(
  74.                   $wm,
  75.                   \Imagick::COMPOSITE_PLUS,
  76.                   ($image->getImageWidth() - $wm->getImageWidth()) / 2,
  77.                   ($image->getImageHeight() - $wm->getImageHeight()) / 2
  78.                 );
  79.                 $image->writeImage($_SERVER['DOCUMENT_ROOT'] . $img);
  80.             }
  81.         } catch (\Exception $e) {
  82.             return;
  83.         }
  84.     }
  85. }