Last active
February 15, 2017 07:23
-
-
Save screeny05/32eefced4adb7d1a62de8f3196e81b21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Turn a media-id into a valid path | |
* @param $params | |
* @param $template | |
* @return string | |
*/ | |
function smarty_function_mediaById($params, $template) | |
{ | |
$id = $params['id']; | |
$size = $params['size'] ?: false; | |
$mediaRepository = Shopware()->Models()->getRepository('Shopware\Models\Media\Media'); | |
$mediaService = Shopware()->Container()->get('shopware_media.media_service'); | |
$mediaObject = $mediaRepository->find((int)$id); | |
if (!$mediaObject) { | |
return $id; | |
} | |
if ($size) { | |
$thumbnails = $mediaObject->getThumbnails(); | |
$thumbnail = $thumbnails[$size] ?: array_values($thumbnails)[$size]; | |
if ($thumbnail) { | |
return $thumbnail; | |
} | |
} | |
// turn object into path | |
return $mediaService->getUrl($mediaObject->getPath()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<img src="{mediaById id=152322}"> | |
<img src="{mediaById id=152323 size=1}"> | |
<img src="{mediaById id=152324 size=80x80}"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment