Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
MediaThumbnailSizeCollection example
}
public
function
unserialize
(
EntityLoadedEvent
$event
)
: void
{
/** @var MediaFolderConfigurationEntity $media */
foreach
(
$event
->
getEntities
(
)
as
$media
)
{
if
(
$media
->
getMediaThumbnailSizes
(
)
=== null
)
{
if
(
$media
->
getMediaThumbnailSizesRo
(
)
)
{
$media
->
setMediaThumbnailSizes
(
unserialize
(
$media
->
getMediaThumbnailSizesRo
(
)
)
)
;
}
else
{
$media
->
setMediaThumbnailSizes
(
new
MediaThumbnailSizeCollection
(
)
)
;
}
}
}
}
}
$media
=
$this
->
getJpgWithFolder
(
)
;
$criteria
=
new
Criteria
(
[
$media
->
getId
(
)
]
)
;
$criteria
->
addAssociation
(
'mediaFolder.configuration.mediaThumbnailSizes'
)
;
/** @var MediaEntity $media */
$media
=
$this
->mediaRepository->
search
(
$criteria
,
$this
->context
)
->
get
(
$media
->
getId
(
)
)
;
static
::
assertInstanceOf
(
MediaFolderEntity::
class
,
$media
->
getMediaFolder
(
)
)
;
static
::
assertInstanceOf
(
MediaFolderConfigurationEntity::
class
,
$media
->
getMediaFolder
(
)
->
getConfiguration
(
)
)
;
$media
->
getMediaFolder
(
)
->
getConfiguration
(
)
->
setMediaThumbnailSizes
(
new
MediaThumbnailSizeCollection
(
[
(
new
MediaThumbnailSizeEntity
(
)
)
->
assign
(
[
'id' => Uuid::
randomHex
(
)
,
'width' => 1530,
'height' => 1530,
]
)
,
]
)
)
;
$media
->
getMediaFolder
(
)
->
getConfiguration
(
)
->
setThumbnailQuality
(
100
)
;
$filePath
=
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
$resource
=
fopen
(
__DIR__ . '/../fixtures/shopware_optimized.jpg', 'rb'
)
;
$strict
= \
func_get_args
(
)
[
2
]
?? false;
if
(
$config
->
getMediaThumbnailSizes
(
)
=== null
)
{
return
0;
}
if
(
$media
->
getThumbnails
(
)
=== null
)
{
return
0;
}
$toBeCreatedSizes
=
new
MediaThumbnailSizeCollection
(
$config
->
getMediaThumbnailSizes
(
)
->
getElements
(
)
)
;
$toBeDeletedThumbnails
=
new
MediaThumbnailCollection
(
$media
->
getThumbnails
(
)
->
getElements
(
)
)
;
foreach
(
$toBeCreatedSizes
as
$thumbnailSize
)
{
foreach
(
$toBeDeletedThumbnails
as
$thumbnail
)
{
if
(
!
$this
->
isSameDimension
(
$thumbnail
,
$thumbnailSize
)
)
{
continue
;
}
if
(
$strict
=== true
&& !
$this
->
getFileSystem
(
$media
)
->
fileExists
(
$this
->urlGenerator->
getRelativeThumbnailUrl
(
$media
,
$thumbnail
)
)
)
{
continue
;
}