Contao\Image::create PHP Method

create() public static method

Create an image instance from the given image path and size
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public static create ( string | contao\File $image, array | integer $size = null ) : static
$image string | contao\File The image path or File instance
$size array | integer The image size as array (width, height, resize mode) or an tl_image_size ID
return static The created image instance
    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;
    }

Usage Example

Example #1
0
 /**
  * Gibt den Pfad und die Abmessungen des angepassten Bilds zurück.
  * @param $strImageId
  * @param $strSize
  * @return mixed
  */
 public static function getImageData($strImageId, $strSize)
 {
     $objImg = \Contao\FilesModel::findByPk($strImageId);
     $arrSize = deserialize($strSize);
     $arrMeta = deserialize($objImg->meta);
     if ($arrMeta != null && isset($arrMeta[$GLOBALS['TL_LANGUAGE']])) {
         $arrData['meta'] = $arrMeta[$GLOBALS['TL_LANGUAGE']];
     } else {
         $arrData['meta']['title'] = $objImg->name;
     }
     if (!is_array($arrSize)) {
         $arrSize = getimagesize($objImg->path);
         $imageObj = \Contao\Image::create($objImg->path, array($arrSize[0], $arrSize[1], $arrSize[2]));
         $imageObj->executeResize();
         $arrData['path'] = $imageObj->getResizedPath();
     }
     if (!isset($arrData['path'])) {
         $arrData['path'] = $objImg->path;
     }
     $arrData['name'] = $objImg->name;
     $arrData['width'] = $arrSize[0];
     $arrData['height'] = $arrSize[1];
     return $arrData;
 }
All Usage Examples Of Contao\Image::create