Exemple #1
0
 /**
  * Сохраняет картинку по формату
  *
  * @param \cs\services\File     $file
  * @param \cs\services\SitePath $destination
  * @param array $field
  * @param array | false $format => [
  *           3000,
  *           3000,
  *           FileUpload::MODE_THUMBNAIL_OUTBOUND
  *          'isExpandSmall' => true,
  *      ] ,
  *
  * @return \cs\services\SitePath
  */
 private static function saveImage($file, $destination, $format, $field)
 {
     if ($format === false || is_null($format)) {
         $file->save($destination->getPathFull());
         return $destination;
     }
     $widthFormat = 1;
     $heightFormat = 1;
     if (is_numeric($format)) {
         // Обрезать квадрат
         $widthFormat = $format;
         $heightFormat = $format;
     } else {
         if (is_array($format)) {
             $widthFormat = $format[0];
             $heightFormat = $format[1];
         }
     }
     // generate a thumbnail image
     $mode = ArrayHelper::getValue($format, 2, self::MODE_THUMBNAIL_CUT);
     if ($file->isContent()) {
         $image = Image::getImagine()->load($file->content);
     } else {
         $image = Image::getImagine()->open($file->path);
     }
     if (ArrayHelper::getValue($format, 'isExpandSmall', true)) {
         $image = self::expandImage($image, $widthFormat, $heightFormat, $mode);
     }
     $quality = ArrayHelper::getValue($field, 'widget.1.options.quality', 80);
     $options = ['quality' => $quality];
     $image->thumbnail(new Box($widthFormat, $heightFormat), $mode)->save($destination->getPathFull(), $options);
     return $destination;
 }