Skip to content

Instantly share code, notes, and snippets.

@arlg
Last active December 11, 2015 02:19
Show Gist options
  • Save arlg/4529490 to your computer and use it in GitHub Desktop.
Save arlg/4529490 to your computer and use it in GitHub Desktop.
/*
* Specifies folders and wich matchmedia we use
* here I want a desktop 'normal' image, a desktop/ipad 3 retina image,
a mobile or a mobile retina image
fallback to noscript loads the desktop one.
*/
function getResponsiveImg($imageName, $altAttribute)
{
$responsiveImage =
"
<div data-picture data-alt='".$altAttribute."'>
<div data-src='images/normal/".$imageName."' ></div>
<div data-src='images/normal/2x/".$imageName."' data-media='(min-device-width: 768px) and (max-device-width: 1024px) and (-o-min-device-pixel-ratio: 2/1), (min-device-width: 768px) and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 2), (min-device-width: 768px) and (max-device-width: 1024px) and (min-device-pixel-ratio: 2)''></div>
<div data-src='images/small/".$imageName."' data-media='(max-width: 480px)' ></div>
<div data-src='images/small/2x/".$imageName."' data-media='(max-width: 480px) and (-o-min-device-pixel-ratio: 2/1), (max-width: 480px) and (-webkit-min-device-pixel-ratio: 2), (max-width: 480px) and (min-device-pixel-ratio: 2)' ></div>
<noscript><img src='images/desktop/".$imageName."' alt='".$altAttribute."' ></noscript>
</div>
";
return $responsiveImage;
}
//How to use it :
//In your php file, call :
<?
echo getResponsiveImg("name_of_your_image", "image_alt_attribute");
?>
//DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment