Created
January 11, 2016 08:04
-
-
Save Bloodb0ne/4699dac0473eb40bfccc to your computer and use it in GitHub Desktop.
This file contains 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 | |
function jpegStrip($path){ | |
$image = imagecreatefromjpeg($path); | |
list ($w, $h) = getimagesize($path); | |
$height = ( 40*$h / $w ); | |
$thumb = imagecreatetruecolor(40, $height); | |
imagecopyresized($thumb,$image,0,0,0,0,40,$height,$w,$h); | |
imagedestroy($image); | |
ob_start(); | |
imagejpeg($thumb); | |
imagedestroy($thumb); | |
$res = ob_get_contents(); | |
ob_end_clean(); | |
//Stip header | |
return base64_encode($res); | |
} | |
$data = jpegStrip('Mars_south_pole_and_beyond.jpg'); | |
?> | |
<img data-orig='Mars_south_pole_and_beyond.jpg' data-preload="data:image/jpeg;base64,<?php echo $data; ?>" class='preload' width="2584" height="6456"> | |
<script type="text/javascript"> | |
preloadImages('preload'); | |
function toArray(obj) { | |
var array = []; | |
// iterate backwards ensuring that length is an UInt32 | |
for (var i = obj.length >>> 0; i--;) { | |
array[i] = obj[i]; | |
} | |
return array; | |
} | |
function preloadImages(name){ | |
var images = toArray(document.getElementsByClassName(name)); | |
var drawSurface = document.createElement('canvas'); | |
images.forEach(function(img){ | |
var preload = img.getAttribute('data-preload'); | |
var idata_img = new Image(); | |
idata_img.onload = function(){ | |
w = img.width; | |
h = img.height; | |
drawSurface.style.width = w + 'px'; | |
drawSurface.style.height = h + 'px'; | |
drawSurface.width = w; | |
drawSurface.height = h; | |
var context = drawSurface.getContext('2d'); | |
context.clearRect(0, 0, w, h); | |
context.drawImage(idata_img, 0, 0, w, h); | |
// var imageData = canvas.getContext('2d').getImageData(0, 0, w, h); | |
// Preform Image Edit | |
// canvas.getContext('2d').putImageData(imageData, 0, 0); | |
img.src = drawSurface.toDataURL(); | |
var orig_src = img.getAttribute('data-orig'); | |
if(orig_src != null && orig_src != '') { | |
var orig_img = new Image(); | |
orig_img.onload = function() { | |
img.src = orig_src; | |
}; | |
orig_img.src = orig_src; | |
} | |
} | |
idata_img.src = preload; | |
}) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment