Skip to content

Instantly share code, notes, and snippets.

@putzflorian
Created April 9, 2013 08:14
Show Gist options
  • Save putzflorian/5343903 to your computer and use it in GitHub Desktop.
Save putzflorian/5343903 to your computer and use it in GitHub Desktop.
Ein kleiner Helper für das einfügen von Retina Images. Bei den Thumbnails ein weiteres Thumbnail anlegen wo am Ende "_2x" angehängt wird. z.b. Das normale Thumbnail hat den Namen "teaserImage" lautet der Name vom Retina Image "teaserImage_2x".
var retina = window.devicePixelRatio > 1;
$(document).ready(function() {
// some js
if(retina){
var allimages = $('body').find('img');
$(allimages).each(function(index, image){
if($(image).attr('data-large-src') != ""){
$(image).attr('src', $(image).attr('data-large-src'));
}
});
}
});
public static function getRetinaImage($image, $thumbnail){
if($image instanceof Document_Tag_Image){
$myfile = $image->image;
} else if($image instanceof Asset_Image){
$myfile = $image;
} else {
return false;
exit;
}
$end = explode('.', $myfile->getFilename());
$end = end($end);
$newthumb = $thumbnail . '_2x';
$dir = Asset_Image_Thumbnail_Config::getWorkingDir();
$thumbnails = array();
$files = scandir($dir);
foreach ($files as $file) {
if(strpos($file, ".xml")) {
$name = str_replace(".xml", "", $file);
$thumbnails[] = Asset_Image_Thumbnail_Config::getByName($name);
}
}
$allthumbs = array();
foreach ($thumbnails as $thumb) {
$allthumbs[] = $thumb->getName();
}
$retinaimage = 'data-large-src="/website/var/tmp/thumb_' . $myfile->getId() . '__' . $thumbnail . '_2x.' . $end . '"';
if(in_array($newthumb, $allthumbs)){
return $retinaimage;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment