Base64 image from cropit is getting clipped when decoded with PHP - javascript

I'm using the cropit jquery plugin to manage image cropping on my website. The way I have it setup is that cropit will give me a base 64 string that I'll pass to PHP which will decode it and place it in the folder. The issue is that when I decode the string it will only make about 1/10 of the image, the rest will just be white / transparent. My code is as follows:
jQuery:
var username = "<?php echo $userData['username']; ?>";
$('#image-cropper').cropit({
imageState:{
src:'users/'+username+'/profile_picture.jpg'
},
});
$('#image-cropper').cropit('previewSize', {width:500, height:500});
$('.export').click(function() {
var imageData = $('#image-cropper').cropit('export');
//$("#code").val(imageData);
window.open(imageData);
});
PHP:
function decode ($base64) {
list($type, $base64) = explode(';', $base64);
list(, $base64) = explode(',', $base64);
$code = base64_decode($base64);
echo $userData['username'];
file_put_contents('users/' . $userData['username'] . '/profile_picture.png', $base64);
}
The code I have here was working when I had the width/height of $('#image-cropper').cropit('previewSize', {width:500, height:500}); set to 250. I had to change it because without a larger width/height it would save a very low resolution image which is still an issue but not as major. Any help would be great. Thanks!
base64 viewed in browser:
base64 when decoded with PHP:

The data URI scheme that the export function is using as size limitations (depending on the browser).
As the cropit export function allows to tweak the image format and compression factor, you could try to save in jpeg and adjust the quality for best results inside the data URI limits:
// Returns the cropped image in Data URI format.
// The second argument `options` takes the following keys:
// - [type='image/png'] exported image type.
// - [quality=.75] exported image quality, only works when type is
// 'image/jpeg' or 'image/webp'.
// - [originalSize=false] when set to true, export cropped part in
// original size, overriding exportZoom.
// - [fillBg='#fff'] if `type` is 'image/jpeg', this color will be
// filled as the background of the exported image.
$imageCropper.cropit('export', {
type: 'image/jpeg',
quality: .9,
originalSize: true
});

function decode ($base64) {
$explodeBase64 = explode(";base64,", $base64);
$code = base64_decode($explodeBase64[0]);
file_put_contents('users/' . $userData['username'] . '/profile_picture.'.basename(#$explodeBase64[0]), $code);
}
Use the above function to create an image using base64 encoded values, here you need to pass a parameter to function decode('YOUR_IMAGE_ENCODED_STRING')
My output:

Related

Why is the conversion of base64 to a PNG image resulting in a black image?

I have created a paint app using JavaScript and Flask. When I store the images drawn by the user using the canvas.toDataURL() and then try to display them together in another /finish domain using the PIL, I get a black image as follows:
This is my code for storing the canvas data on the press of the Save button:
function save(){
var filename = document.getElementById("fname").value;
var data = JSON.stringify(canvas_data);
var image = canvas.toDataURL();
var targetX = document.getElementById("targetX").value;
var targetY = document.getElementById("targetY").value;
var sizeX = document.getElementById("sizeX").value;
var sizeY = document.getElementById("sizeY").value;
$.post("/", { save_fname: filename, save_cdata: data, save_image: image, save_targetX: targetX, save_targetY: targetY, save_sizeX: sizeX, save_sizeY: sizeY });
alert(filename + " saved");
document.getElementById("fname").value = ''
reset();
}
And this is the function I use for decoding the encoding:
def stringToRGB(base64_string):
imgdata = base64.b64decode(str(base64_string))
image = Image.open(io.BytesIO(imgdata))
return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
This is how I call the function:
img_arrays.append(stringToRGB(base64_string.split(',')[1]))
This is a common error that mainly has the reasons that the user is trying to save them as JPEG images or there's some error with the base54 encoding/decoding but I am not able to find out the exact issue for my code. I used a similar answer (can't find the link) to write the snippets for conversion of the base64 image to a PIL image.
Here's a sample base64 string sample:
svg_arr='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu4AAAHCCAYAAACwm0waAAAAAXNSR0IArs4c6QAAIABJREFUeF7t3U+odXtZB/DHe6/eTLnXqNRrk5SiJhoakUKCowIdFToooSZZoJAYBA2EdCBNwkhQSJsUUgOFmijUsAItSEsnRaFN0mtZqJji1Wux6mzd7nvO2f/WWvv3fPfnhQvKu/48z+f5nbW/Z71r7/208ocAAQIECBAgQIAAgeEFnjZ8hQokQIAAAQIECBAgQKAEd4uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBDcGwxJiQQIECBAgAABAgQEd2uAAAECBAgQIECAQAMBwb3BkJRIgAABAgQIECBAQHC3BggQIECAAAECBAg0EBD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REgUFWCu2VAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgADTmLrGAAAJUklEQVQBAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnECgnvcSDVEgAABAgQIECCQKCC4J05VTwQIECBAgAABAnEC/wttGW/hY4MvrAAAAABJRU5ErkJggg=='

blob image always the same size and displaying not the full image

I'm posting a blob to php via jquery with the following code.
var blob = dataURLToBlob(dataURL);
var reader = new FileReader();
// this function is triggered once a call to readAsDataURL returns
reader.onload = function(event){
var fd = new FormData();
fd.append('data', event.target.result);
$.post("upload.php",{data: fd},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
};
// trigger the read from the reader...
reader.readAsDataURL(blob);
upload.php
// pull the raw binary data from the POST array
$data = substr($_POST['data'], strpos($_POST['data'], ",") + 1);
// decode it
$decodedData = base64_decode($data);
// print out the raw data,
echo ($decodedData);
$timestamp = date('YmdHis');
$fileName = ''.$timestamp.'.png';
file_put_contents($fileName, $decodedData);
I'm not getting the data from a database it comes directly from my "signature pad" and then saving the image to folder. When I display my image via window.URL.createObjectURL(blob); the image is correct but the uploaded image only display a quarter to half of the image.
I have noticed that the images are always the same size (2KB) and my php settings for upload files and post size are both on 40MB.
Any thoughts on why my images are always the same size, thus uploading only maybe half the image?
Change your max_input_vars in your php.ini file to a larger number.

Store canvas image to mysql using path location

I'm trying to save my canvas image (generated by the user in the template) in my database. I've been through a lot of sources here, majority I've combined everything on it and got pretty much confused.
Steps that I'm trying to make:
Html form where provides different choices asked from the user (done)
Each time the user selects his/her choice, beside of it is the canvas that displays the image. (done)
A button that displays the summary of his choices and the canvas image generated through modal (done)
A button to Submit his order with the image in database using php and mysql No error! But it doesn't store my image
I don't need to download it, i just want it to directly store in my database once submitted
So where I'm stuck is in the STEP 4
I'll give a summary choice on my codes: (it works fine in my file, just giving you the outcome idea)
/*This retrieves my data my JS w/c is same file with my HTML: */
$('#review').click(function () {
$('#shape').html($('input[name="shape_design"]:checked').val());
$('#color').html($('input[name="color_design"]:checked').val());
//in this part, i am getting the image displayed in the canvas based on the user's choice
var canvasSource = document.getElementById('myCanvas');
var contextSource = canvasSource.getContext('2d');
var canvasShow = document.getElementById('show_canvas');
var contextShow = canvasShow.getContext('2d');
var image = contextSource.getImageData(0, 0, canvasSource.width, canvasSource.height);
contextShow.putImageData(image, 0, 0); });
});
$(function () { //this function converts the canvas to image, so it is now called as "Show Canvas" , it display well in my modal... not sure if its the right code
$("#submitBtn").bind("click", function () {
var base64 = $('#myCanvas')[0].toDataURL();
$("#show_canvas").attr("src", base64);
$("#show_canvas").show();
});
});
//After reviewing it, this part will perform to store it in the database.. which is quite I'm confused
$('#submitOrder').click(function(confirm){
swal({ //sweetalert style
title: "ORDER SENT", },
function(submit){
setTimeout(function(){
$('#showchoices').submit(); }, 500); //"showchoices" is the name of the Html form, it will proceed to ordersent.php
});
});
For my php which I'm not sure what I'm doing.
Ordersent.php
<? php
$shape_design = $_POST['shape_design'];
$color_design = $_POST['color_design'];
$upload_dir = "upload/"; //what does this do?
$myCanvas = $_POST['myCanvas']; //myCanvas is the name and id in my form
$myCanvas = str_replace('data:image/png;base64,', '', $myCanvas);
$myCanvas = str_replace(' ', '+', $myCanvas);
$data = base64_decode($myCanvas);
$file = $upload_dir."image_name.png";
$success = file_put_contents($file, $data);
$dbc = #mysql_connect('localhost' , 'root', '');
#mysql_select_db('order_tbl', $dbc);
$query1 = "INSERT INTO choices_tbl VALUES (NULL,'$shape_design','$color_design', '$myCanvas')";
if(#mysql_query($query1,$dbc))
{ Header("Location: /home.php"); }
else { print 'error!! '.mysql_error().''; }
?>
I tried following the instructions most of the tutorial, but it doesn't work on me.
I wanted to save it in my database using PATH , my my data type in my tables are
order_tbl
designID INT(6) NOT NULL ,
shape_design VARCHAR(20) ,
color_design VARCHAR(20) ,
myCanvas VARCHAR(150) ,
am I missing an attribute?
thanks for the help in advance! Thanks for bearing with me.

GET data:image/png;base64,{{image}} net::ERR_INVALID_URL

I want to convert image data that I get from server using Angular.js (for use in ionic-framework), I have use this code :
$http.post(link, {
token: token,
reservationCode: reservationCode
}).success(function (res){
$scope.image = btoa(unescape(encodeURIComponent(res)));
}).error(function (data) {
return false;
});
And use this code in my html :
<img src="data:image/png;base64,{{image}}">
But this error always show :
GET data:image/png;base64,{{image}} net::ERR_INVALID_URL
anybody can help ?
Though a late answer, but will be helpful for future readers:
What you should do is:
<img ng-src="data:image/png;base64,{{image}}">
That would mean that the browser will try to access it only when the data is loaded and will be taken care by AngularJS and hence you will not get that error anymore.
A working approach to base64 encoding of images is to use Canvas and toDataURL() method, but you need to load your image data from server to an Image istance (via src property). Here is an example:
function getBase64() {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function () {
ctx.drawImage(img, 0, 0);
// pay attention: image here contains complete base 64 data url, no need for "data:image/png;base64," header...
$scope.image = canvas.toDataURL();
if (!$scope.$$phase) $scope.$apply();
};
img.src = "http://.../myImagepng";
}
Preferably you could encode to base64 your image on the server side and return the response to the client already encoded.
For example in PHP:
$pathToImg = 'myfolder/myimage.png';
$type = pathinfo($pathToImg, PATHINFO_EXTENSION);
$data = file_get_contents($pathToImg);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

HTML5 URL to saved image displays previously created image when clicked

I have an HTML5 app that saves the canvas image to the server and then provides a link to that image that opens in a new window.
This works fine the first time I save it, but if I create and save a new image and then click on the link it displays the old image that was previously created.
Clicking on refresh will force it to display the new one, but I was wondering if there is a way to make sure it displays the correct image so that I don't have to refresh the page?
Below is what I am using to save the image.
<script>
function saveImageAs (imgOrURL) {
if (typeof imgOrURL == 'object')
imgOrURL = imgOrURL.src;
window.win = open (imgOrURL);
setTimeout('win.document.execCommand("SaveAs")', 500);
}
</script>
<script type="text/javascript">
//****************************************************************
// Save canvas content into image file. //
//****************************************************************
function saveViaAJAX()
{
document.getElementById('saveimage').style.visibility="hidden";
document.getElementById("debugFilenameConsole").innerHTML="Please wait while your image is been generated";
var testCanvas = document.getElementById('canvas');
var canvasData = testCanvas.toDataURL("image/jpg");
var postData = "canvasData="+canvasData;
var debugConsole= document.getElementById("debugConsole");
debugConsole.value=canvasData;
//alert("canvasData ="+canvasData );
var ajax = new XMLHttpRequest();
ajax.open("POST",'savecanvas.php',true);
ajax.setRequestHeader('Content-Type', 'canvas/upload');
//ajax.setRequestHeader('Content-TypeLength', postData.length);
ajax.onreadystatechange=function()
{
if (ajax.readyState == 4)
{
//alert(ajax.responseText);
// Write out the filename.
document.getElementById("debugFilenameConsole").innerHTML="Saved as <a target='_blank' href='myimage.php'> MyImage.jpg"+ajax.responseText+"</a><br>Reload this page to start a new image or click on the link above to open the file.";
}
}
ajax.send(postData);
}
</script>
And the PHP
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// Get the data
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);
// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);
//echo "unencodedData".$unencodedData;
// Save file. This example uses a hard coded filename for testing,
// but a real application can specify filename in POST variable
$fp = fopen( 'MyImage.jpg', 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
}
header("Content-Type: image/jpg");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment; filename=MyImage.jpg");
imagejpeg($img, null, 100);
?>
Because you are giving the image the same name and path, the browser is helpfully caching it for you.
Use a unique path to avoid this!
You can try
var random=new Date();
var random2=getTime();
var rand=random+random2;
imgOrURL = imgOrURL.src+'?rnd='+rand
or for PHP
$rand=rand();
MyImage.php?rand=<?=$rand?>
or
$image='MyImage.php?rand='.$rand.'';
For future use, to save headache. Add dates and times first because using md5 , sha1() or a rand() or JavaScript unique key can be a nightmare, example:
/// This is much cleaner
/11.1.2012/11.05/eachier93.jpg
/11.1.2012/11.05/4358390485/93.jpg
/11.1.2012/11.10/3249203489834/234234.jpg
/// then this
/files/342748234234234/234982348394/333535.jpg
/files/4535345345/234234234234/3332.jpg
/files/23423434324/023840348234/2343.jpg

Categories