Skip to content

Instantly share code, notes, and snippets.

@screeny05
Last active February 15, 2017 07:23
Show Gist options
  • Save screeny05/32eefced4adb7d1a62de8f3196e81b21 to your computer and use it in GitHub Desktop.
Save screeny05/32eefced4adb7d1a62de8f3196e81b21 to your computer and use it in GitHub Desktop.
<?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());
}
<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