Пример #1
0
 /**
  * Create a picture instance from the given image path and size
  *
  * @param string|File   $file The image path or File instance
  * @param array|integer $size  The image size as array (width, height, resize mode) or an tl_image_size ID
  *
  * @return static The created picture instance
  */
 public static function create($file, $size = null)
 {
     if (is_string($file)) {
         $file = new \File(rawurldecode($file));
     }
     $imageSize = null;
     $picture = new static($file);
     // tl_image_size ID as resize mode
     if (is_array($size) && !empty($size[2]) && is_numeric($size[2])) {
         $size = (int) $size[2];
     }
     $imageSize = null;
     if (!is_array($size)) {
         $imageSize = \ImageSizeModel::findByPk($size);
         if ($imageSize === null) {
             $size = array();
         }
     }
     if (is_array($size)) {
         $size = $size + array(0, 0, 'crop');
         $imageSize = new \stdClass();
         $imageSize->width = $size[0];
         $imageSize->height = $size[1];
         $imageSize->resizeMode = $size[2];
         $imageSize->zoom = 0;
     }
     $picture->setImageSize($imageSize);
     if ($imageSize !== null && !empty($imageSize->id)) {
         $picture->setImageSizeItems(\ImageSizeItemModel::findVisibleByPid($imageSize->id, array('order' => 'sorting ASC')));
     }
     $fileRecord = \FilesModel::findByPath($file->path);
     if ($fileRecord !== null && $fileRecord->importantPartWidth && $fileRecord->importantPartHeight) {
         $picture->setImportantPart(array('x' => (int) $fileRecord->importantPartX, 'y' => (int) $fileRecord->importantPartY, 'width' => (int) $fileRecord->importantPartWidth, 'height' => (int) $fileRecord->importantPartHeight));
     }
     return $picture;
 }