Skip to content

Instantly share code, notes, and snippets.

@ademalp
Created March 27, 2013 08:15
Show Gist options
  • Save ademalp/5252584 to your computer and use it in GitHub Desktop.
Save ademalp/5252584 to your computer and use it in GitHub Desktop.
Resimleri ajax ile html'ye aktarmak
<?php
function resize($resim, $max_en, $max_boy)
{
$boyut = getimagesize($resim);
$en = $boyut[0];
$boy = $boyut[1];
$x_oran = $max_en / $en;
$y_oran = $max_boy / $boy;
if (($en <= $max_en) and ($boy <= $max_boy)) {
$son_en = $en;
$son_boy = $boy;
} else if (($x_oran * $boy) < $max_boy) {
$son_en = $max_en;
$son_boy = ceil($x_oran * $boy);
} else {
$son_en = ceil($y_oran * $en);
$son_boy = $max_boy;
}
$eski = imagecreatefromjpeg($resim);
$yeni = imagecreate($son_en, $son_boy);
imagecopyresized($yeni, $eski, 0, 0, 0, 0, $son_en, $son_boy, $en, $boy);
ob_start();
imagejpeg($yeni, NULL, 100);
$return = ob_get_contents();
ob_end_clean();
return $return;
}
//header("Content-Type: image/jpg");
echo base64_encode(resize('dosyalar/urun/24-3-2.jpg', 200, 200));
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="Site Açıklama">
<title>Site Başlık</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#click").click(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'image.php',
processData: false,
success: function(msg){
var data = 'data:image/jpg;base64,'+msg;
$('#img').attr('src',data);
}
});
});
});
</script>
</head>
<body>
<img id="img">
<a href="#" id="click">click</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment