Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
MediaThumbnailCollection example
/** @var MediaEntity $media */
foreach
(
$event
->
getEntities
(
)
as
$media
)
{
if
(
$media
->
getMediaTypeRaw
(
)
)
{
$media
->
setMediaType
(
unserialize
(
$media
->
getMediaTypeRaw
(
)
)
)
;
}
if
(
$media
->
getThumbnails
(
)
=== null
)
{
if
(
$media
->
getThumbnailsRo
(
)
)
{
$media
->
setThumbnails
(
unserialize
(
$media
->
getThumbnailsRo
(
)
)
)
;
}
else
{
$media
->
setThumbnails
(
new
MediaThumbnailCollection
(
)
)
;
}
}
}
}
private
function
addThumbnailUrl
(
MediaThumbnailEntity
$thumbnail
, MediaEntity
$media
)
: void
{
$thumbnail
->
setUrl
(
$this
->urlGenerator->
getAbsoluteThumbnailUrl
(
$media
,
$thumbnail
)
$subscriber
=
$this
->
getContainer
(
)
->
get
(
MediaLoadedSubscriber::
class
)
;
$context
= Context::
createDefaultContext
(
)
;
$mediaId
= '34556f108ab14969a0dcf9d9522fd7df';
$mimeType
= 'image/png';
$mediaEntity
=
new
MediaEntity
(
)
;
$mediaEntity
->
setId
(
$mediaId
)
;
$mediaEntity
->
setMimeType
(
$mimeType
)
;
$mediaEntity
->
setFileExtension
(
'png'
)
;
$mediaEntity
->
setFileName
(
$mediaId
. '-134578345'
)
;
$mediaEntity
->
setThumbnails
(
new
MediaThumbnailCollection
(
)
)
;
$mediaLoadedEvent
=
new
EntityLoadedEvent
(
$this
->
getContainer
(
)
->
get
(
MediaDefinition::
class
)
,
[
$mediaEntity
]
,
$context
)
;
$subscriber
->
addUrls
(
$mediaLoadedEvent
)
;
static
::
assertStringEndsWith
(
$mediaEntity
->
getFileName
(
)
. '.' .
$mediaEntity
->
getFileExtension
(
)
,
$mediaEntity
->
getUrl
(
)
)
;
static
::
assertEquals
(
[
]
,
$mediaEntity
->
getThumbnails
(
)
->
getElements
(
)
)
;
}
$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
;
}