Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SaveAs tool and example #34

Merged
merged 1 commit into from
May 21, 2015
Merged

SaveAs tool and example #34

merged 1 commit into from
May 21, 2015

Conversation

swederik
Copy link
Member

@swederik swederik commented May 8, 2015

Small function for saving PNG files from the canvases. Addresses #31.

Seems it can't be automatically merged because of changes to dist/cornerstoneTools... How should we approach this? I assume I stop using grunt before commits? But then the rawgit examples won't work...

Update: Rebased and fixed

saveas

@cancan101
Copy link

Is it possible to use any of this code for send the binary image over HTTP, for example using window.fetch?

@swederik
Copy link
Member Author

Yeah, sure, but what for? You want to save the canvas as an image on your server?

I think you can just use canvas.toDataURL(); and then POST that data to the server. Then you just decode the base64 string to produce an image on the server.

@cancan101
Copy link

Yes, exactly. I want to save the save the canvas (which has all of the user's modifications, transforms and annotations) to the server. This along with #30 so that I have both the steps to re-create the modifications and the image of the canvas.

@swederik
Copy link
Member Author

Ok so you want to use the image to show the saved-state then. Seems easy enough to do, but I don't know if it's worth having extra code for it in cornerstone itself.

// Get the canavas
var canvas = $(element).find("canvas").get(0);
var imageData = canvas.toDataURL();
$.ajax({
  method: "POST",
  url: "/your/server/",
  data: {"image": imageData}
})

Then on the server (at least in Python):

import base64

# Assuming you have a request like in Django
data = request.POST["image"]

filename = "MyNewImage.png"
out_file = open(filename, "w")
out_file.write(base64.decodestring(data))
out_file.close()

Hope that helps

@cancan101
Copy link

Sure that seems simple enough. I do think though that a save as tool for in browser saves still stands on its own as a useful feature.


function saveAs(element, filename)
{
var canvas = $(element).find("canvas").get(0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to extract $(element).find("canvas").get(0); as a helper method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not, it's boilerplate jQuery.

swederik added a commit that referenced this pull request May 21, 2015
@swederik swederik merged commit 461b7f8 into cornerstonejs:master May 21, 2015
@swederik swederik deleted the saveAs branch May 21, 2015 11:39
@swederik swederik mentioned this pull request May 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants