Image Datatypes
Image
An image field is stored in an INT column in the database. It holds the ID of the referenced \Pimcore\Model\Asset\Image
.
Unlike other object relation types, an image relation is not stored in the relations table (this has historic reasons),
but it creates a dependency in the dependencies table.
To set an object's image field programmatically, an \Pimcore\Model\Asset\Image
must be passed to the according setter.
$image = Asset\Image::getByPath("/examples/example1.jpg");
$object->setImage($image);
$object->save();
The image field is represented in the UI by an image drop area. The image drop area's width and height can be configured in the class settings as follows:
Working with images in frontend
The get a thumbnail of an image field, just call getThumbnail()
on the returned asset object.
{% if object.myImage is instanceif('Pimcore\Model\Asset\Image') %}
{{ object.myImage.thumbnail('myThumbnailDefinitionName').html }}
{% endif %}
Since $object->getImage()
just returns an asset object, you can of course use all other thumbnail features of Pimcore\Model\Asset\Image
.
External Image
This one allows you to enter an external image URL which is then shown as a preview.
{% if object.getExternalImage() is instanceof('\\Pimcore\\Model\\DataObject\\Data\\ExternalImage') %}
<img src="{{ object.getExternalImage().getUrl() }}" />
{% endif %}
Image Gallery
Is a collection of Advanced Images
(see below). The sort order can be changed via drag & drop.
Populate an ImageGallery
<?php
$galleryData = [
AssetImage,
AssetImage,
//....
];
$items = [];
foreach($galleryData as $img){
$advancedImage = new \Pimcore\Model\DataObject\Data\Hotspotimage();
$advancedImage->setImage($img);
$items[] = $advancedImage;
}
$news->setGallery(new \Pimcore\Model\DataObject\Data\ImageGallery($items));
?>
Image Advanced (supporting Hotspots/Markers/Cropping)
This data type is an advanced extension to the image data type which allows defining hotspots, markers and cropping on the assigned image.
The hotspots are defined by a name and are stored as an array with the attributes name, top, left, width and height whereas the values top, left, width, height are stored as percentages of the image dimensions.