Inheritance: extends Model
Example #1
0
 /**
  * Create an image instance from the given image path and size
  *
  * @param string|File   $image 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 image instance
  *
  * @deprecated Deprecated since Contao 4.3, to be removed in Contao 5.0.
  *             Use the contao.image.image_factory service instead.
  */
 public static function create($image, $size = null)
 {
     @trigger_error('Using Image::create() has been deprecated and will no longer work in Contao 5.0. Use the contao.image.image_factory service instead.', E_USER_DEPRECATED);
     if (is_string($image)) {
         $image = new \File(rawurldecode($image));
     }
     /** @var Image $imageObj */
     $imageObj = new static($image);
     // tl_image_size ID as resize mode
     if (is_array($size) && !empty($size[2]) && is_numeric($size[2])) {
         $size = (int) $size[2];
     }
     if (is_array($size)) {
         $size = $size + array(0, 0, 'crop');
         $imageObj->setTargetWidth($size[0])->setTargetHeight($size[1])->setResizeMode($size[2]);
     } elseif (($imageSize = \ImageSizeModel::findByPk($size)) !== null) {
         $imageObj->setTargetWidth($imageSize->width)->setTargetHeight($imageSize->height)->setResizeMode($imageSize->resizeMode)->setZoomLevel($imageSize->zoom);
     }
     $fileRecord = \FilesModel::findByPath($image->path);
     // Set the important part
     if ($fileRecord !== null && $fileRecord->importantPartWidth && $fileRecord->importantPartHeight) {
         $imageObj->setImportantPart(array('x' => (int) $fileRecord->importantPartX, 'y' => (int) $fileRecord->importantPartY, 'width' => (int) $fileRecord->importantPartWidth, 'height' => (int) $fileRecord->importantPartHeight));
     }
     return $imageObj;
 }
Example #2
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;
 }