image.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function image_file_init() {
  2. str = '<table border="1" width="100%">';
  3. str += '<tr><td>file:</td><td><input type="file" id="file" style="width:95%"/></td></tr>';
  4. str += '<tr><td>size:</td><td><input type="text" id="size" style="width:95%"/></td></tr>';
  5. str += '<tr><td colspan="2"><button id="convert">convert</button></td></tr>';
  6. str += '<tr><td colspan="2" id="image">&nbsp;</td></tr>';
  7. str += '</table>';
  8. $('#content').html(str);
  9. $('#result').html('');
  10. $('#convert').click(function() {
  11. image_file();
  12. });
  13. }
  14. function image_url_init() {
  15. str = '<table border="1" width="100%">';
  16. str += '<tr><td>url:</td><td><input type="text" id="url" style="width:95%"/></td></tr>';
  17. str += '<tr><td>size:</td><td><input type="text" id="size" style="width:95%"/></td></tr>';
  18. str += '<tr><td colspan="2"><button id="convert">convert</button></td></tr>';
  19. str += '<tr><td colspan="2" id="image">&nbsp;</td></tr>';
  20. str += '</table>';
  21. $('#content').html(str);
  22. $('#result').html('');
  23. $('#convert').click(function() {
  24. image_url();
  25. });
  26. }
  27. function image_file() {
  28. var formData = new FormData();
  29. formData.append('file', $('#file')[0].files[0]);
  30. var url = '../servlet/image' + ($('#size').val() == '' ? '' : ('/size/' + $('#size').val()));
  31. jQuery.ajax({
  32. type : 'post',
  33. url : url,
  34. data : formData,
  35. dataType : 'json',
  36. cache : false,
  37. contentType : false,
  38. processData : false,
  39. xhrFields : {
  40. 'withCredentials' : true
  41. },
  42. crossDomain : true,
  43. success : function(json) {
  44. $('#result').html(JSON.stringify(json, null, 4));
  45. $('#image').html('<img src="data:image/png;base64,' + json.data + '" />');
  46. },
  47. error : function(data) {
  48. failure(data);
  49. }
  50. });
  51. }
  52. function image_url() {
  53. var data = {
  54. url : $('#url').val()
  55. };
  56. if ($('#size', '#content').val()) {
  57. data.size = $('#size', '#content').val();
  58. }
  59. $.ajax({
  60. type : 'post',
  61. dataType : 'json',
  62. url : '../jaxrs/image',
  63. contentType : 'application/json; charset=utf-8',
  64. data : JSON.stringify(data),
  65. xhrFields : {
  66. 'withCredentials' : true
  67. },
  68. crossDomain : true
  69. }).done(function(json) {
  70. if (json.type == 'success') {
  71. $('#result').html(JSON.stringify(json, null, 4));
  72. $('#image', '#content').html('<img src="data:image/png;base64,' + json.data + '" />');
  73. } else {
  74. failure(data);
  75. }
  76. }).fail(function(data) {
  77. failure(data);
  78. });
  79. }