javascript - Export Resized Image In Canvas To New JSZip Package -
i can load image canvas element , resize it, i'm having trouble grabbing resized image.....
var logo = $(".logo"), loader = $(".load"), canvas = $(".holder"), ctx = canvas[0].getcontext("2d"); function displaypreview(file) { var reader = new filereader(); reader.onload = function(e) { var img = new image(); img.src = e.target.result; img.onload = function() { // x, y, width, height ctx.drawimage(img, 0, 0, 128, 128); var dataurl = canvas[0].todataurl("image/png"); var logo = $(".logo"); var imgurl = dataurl; var imgz = $("<img>"); imgz.attr("src", imgurl); logo.html(""); logo.append(imgz); }; }; reader.readasdataurl(file); }
into download package function jszip.
// download zip $(".download").on("click", function(imgurl) { var zip = new jszip(); zip.file("logo.png", imgurl); var content = zip.generate({type:"blob"}); // see filesaver.js saveas(content, "test.zip"); });
any appreciated
var logo = $(".logo"), loader = $(".load"), canvas = $(".holder"), ctx = canvas[0].getcontext("2d"); function displaypreview(file) { var reader = new filereader(); reader.onload = function(e) { var img = new image(); img.src = e.target.result; img.onload = function() { // x, y, width, height ctx.drawimage(img, 0, 0, 128, 128); var dataurl = canvas[0].todataurl("image/png"); var logo = $(".logo"); var imgurl = dataurl; var imgz = $("<img>"); imgz.attr("src", imgurl); logo.html(""); logo.append(imgz); }; }; reader.readasdataurl(file); } $(document).ready(function() { loader.on("change", function(evt) { var file = evt.target.files[0]; displaypreview(file); var reader = new filereader(); reader.onload = function(e) { // download zip $(".download").on("click", function(imgurl) { var zip = new jszip(); zip.file("logo.png", imgurl); var content = zip.generate({type:"blob"}); // see filesaver.js saveas(content, "test.zip"); }); return false; }; reader.readasarraybuffer(file); }); // trigger load image $(".trigload").click(function() { $("input").trigger("click"); }); });
@import url("http://necolas.github.io/normalize.css/3.0.1/normalize.css"); .hide { display: none; } .logo { text-align: center; } .fill { width: 100%; } .fr { float: right; }
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="http://stuk.github.io/jszip/dist/jszip.min.js"></script> <script type="text/javascript" src="http://stuk.github.io/jszip-utils/dist/jszip-utils.js"></script> <script type="text/javascript" src="http://stuk.github.io/jszip/vendor/filesaver.js"></script> <input type="file" class="hide load"> <a class="trigload" href="javascript:void(0)">load image</a> <a class="download fr" href="javascript:void(0)">download</a> <div class="logo"></div> <div class="fill" align="center"> <canvas class="holder" width="128" height="128"></canvas> </div>
in order jszip correctly save dataurl valid png file, seems need add object containing {base64: true}
as third argument of zip.file()
method, , remove data:image/png;base64,
dataurl.
also, assigning click event imgurl
variable. may want store in global variable, or check $('.logo>img')[0].src
or call once again canvas[0].todataurl()
.
var logo = $(".logo"), loader = $(".load"), canvas = $(".holder"), ctx = canvas[0].getcontext("2d"); function displaypreview(file) { var reader = new filereader(); reader.onload = function(e) { var img = new image(); img.src = e.target.result; img.onload = function() { // x, y, width, height ctx.drawimage(img, 0, 0, 128, 128); var dataurl = canvas[0].todataurl("image/png"); var logo = $(".logo"); var imgurl = dataurl; var imgz = $("<img>"); imgz.attr("src", imgurl); logo.html(""); logo.append(imgz); }; }; reader.readasdataurl(file); } $(document).ready(function() { loader.on("change", function(evt) { var file = evt.target.files[0]; displaypreview(file); var reader = new filereader(); reader.onload = function(e) { // download zip $(".download").on("click", function() { var imgurl = canvas[0].todataurl(); var zip = new jszip(); zip.file("logo.png", imgurl.split('base64,')[1],{base64: true}); var content = zip.generate({type:"blob"}); // see filesaver.js saveas(content, "test.zip"); }); return false; }; reader.readasarraybuffer(file); }); // trigger load image $(".trigload").click(function() { $("input").trigger("click"); }); });
@import url("http://necolas.github.io/normalize.css/3.0.1/normalize.css"); .hide { display: none; } .logo { text-align: center; } .fill { width: 100%; } .fr { float: right; }
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="http://stuk.github.io/jszip/dist/jszip.min.js"></script> <script type="text/javascript" src="http://stuk.github.io/jszip-utils/dist/jszip-utils.js"></script> <script type="text/javascript" src="http://stuk.github.io/jszip/vendor/filesaver.js"></script> <input type="file" class="hide load"> <a class="trigload" href="javascript:void(0)">load image</a> <a class="download fr" href="javascript:void(0)">download</a> <div class="logo"></div> <div class="fill" align="center"> <canvas class="holder" width="128" height="128"></canvas> </div>
Comments
Post a Comment