stuppid_bot

Untitled

Apr 23rd, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     var frameWidth = 690;
  3.     var frameHeight = 460;
  4.  
  5.     getFrame = function(id) {
  6.         id = id || 0;
  7.         var frames = document.getElementsByClassName('frame'),
  8.             i = frames.length;
  9.         gid('heading').innerHTML = frames[id].getAttribute('data-heading');
  10.         gid('prev').style.display = id ? 'inline' : 'none';  
  11.  
  12.         while (i--) {
  13.             frames[i].style.display = i == id ? 'block' : 'none';
  14.         }  
  15.  
  16.         VK.callMethod('resizeWindow', frameWidth, document.body.clientHeight > frameHeight ? document.body.clientHeight : frameHeight);
  17.     }
  18. })();
  19.  
  20. (function() {
  21.     var i = 0;
  22.    
  23.     nextFrame = function() {
  24.         getFrame(++i);
  25.     };
  26.    
  27.     prevFrame = function() {
  28.         getFrame(--i);
  29.     }
  30. })();
  31.  
  32. function setUploadError(message) {
  33.     gid('uploadError').innerHTML = message ? message : '';
  34. }
  35.  
  36. function imagePreview(data) {
  37.     var size = 680,
  38.         img = new Image,
  39.         cnv = gid('preview'),
  40.         ctx = cnv.getContext('2d');
  41.     img.src = data;
  42.    
  43.     img.onload = function() {
  44.         var proportion = this.width / this.height;
  45.         (this.width > this.height) ? this.height = (this.width = size) / proportion : this.width = (this.height = size) * proportion;
  46.         cnv.width = this.width;
  47.         cnv.height = this.height;
  48.         ctx.drawImage(this, 0, 0, this.width, this.height);
  49.         nextFrame();
  50.     };
  51.    
  52.     gid('uploadForm').reset();
  53.     setUploadError();
  54. }
  55.  
  56. function clean(text) {
  57.     return text.trim().replace(/\s+/g, ' ');
  58. }
  59.  
  60. function editImage(type, img) {
  61.     var cnv = gid('result'),
  62.         ctx = cnv.getContext('2d'),
  63.         renderImage;  
  64.    
  65.     switch (type) {
  66.         case 'dem':        
  67.             var fontSize1 = img.width * 0.1,
  68.                 fontSize2 = img.width * 0.06,
  69.                 paddingFont = 'px Calibri, Arial, sans-serif',
  70.                 // отступ от края
  71.                 x1 = 32,
  72.                 // толщина белой линии
  73.                 x2 = 2,
  74.                 // промежуток между линией и изображением
  75.                 x3 = 2,
  76.                 x4 = x1 + x2 + x3,
  77.                 x5 = x4 * 2,
  78.                 x6 = x2 * 2 +  x3 * 2,
  79.                 x7 = x1 + x2,
  80.                 x8 = x3 * 2;
  81.             cnv.width = img.width;          
  82.             cnv.height = img.height + fontSize1 + fontSize2;
  83.             img.width -= x5;
  84.             img.height -= x5;          
  85.            
  86.             renderImage = function() {
  87.                 var text1 = clean( gid('text1').value );
  88.                 var text2 = clean( gid('text2').value );              
  89.                 ctx.fillStyle = '#000';
  90.                 ctx.fillRect(0, 0, cnv.width, cnv.height);
  91.                 ctx.fillStyle = '#fff';
  92.                 ctx.fillRect(x1, x1, img.width + x6, img.height + x6);
  93.                 ctx.fillStyle = '#000';
  94.                 ctx.fillRect(x7, x7, img.width + x8, img.height + x8);
  95.                 ctx.drawImage(img, x4, x4, img.width, img.height);
  96.                 // верхний текст
  97.                 ctx.textAlign = 'center';
  98.                 ctx.fillStyle = '#fff';            
  99.                 // текст уменьшается на 1 пиксель каждые два символа                            
  100.                 ctx.font = fontSize1 - text1.length / 2 + paddingFont;                
  101.                 ctx.fillText( text1, cnv.width / 2 + 0.4, img.height + x4 + fontSize1 + 0.4 );
  102.                 // нижний текст
  103.                 ctx.font = fontSize2 - text2.length / 4 + paddingFont;
  104.                 ctx.fillText( text2, cnv.width / 2, cnv.height - x1 );
  105.             };
  106.                        
  107.             break;
  108.     }
  109.    
  110.     gid('text1').onkeypress = gid('text1').onkeyup = gid('text2').onkeypress = gid('text2').onkeyup = renderImage;
  111.     renderImage();      
  112.     nextFrame();
  113. }
  114.  
  115. this.addEventListener('load', function() {  
  116.     gid('uploadForm').onsubmit = function() {
  117.         var u = ( gid('uploadUrl').value = gid('uploadUrl').value.trim() );
  118.        
  119.         if (u) {
  120.             var o = url.parse(u);
  121.            
  122.             if (!o) {
  123.                 setUploadError('Это не ссылка');
  124.             }        
  125.             else if (o.scheme && o.scheme != 'http') {
  126.                 setUploadError('Протокол не поддерживается');
  127.             }
  128.             else if (!o.host) {
  129.                 setUploadError('Не указан хост');
  130.             }
  131.             else if (!o.path || o.path == '/') {
  132.                 setUploadError('Не указан путь до файла');
  133.             }
  134.             else {            
  135.                 ajax.post('get_image.php', {hostname: o.host, uri: o.path + (o.query ? '?' + o.query : '')}, {
  136.                     done: function(response) {
  137.                         if (response.substr(0, 4) != 'data') {
  138.                             return setUploadError(response);
  139.                         }
  140.                        
  141.                         imagePreview(response);
  142.                     },
  143.                    
  144.                     fail: function() {
  145.                         this.reset()
  146.                         setUploadError('Сервис временно недоступен');
  147.                     }
  148.                 });
  149.             }
  150.         }
  151.        
  152.         return false;
  153.     };
  154.    
  155.     gid('uploadImage').onchange = function(event) {
  156.         var file = event.target.files[0];
  157.        
  158.         if ( !new RegExp('^image/(' + conf.allowedImageTypes.join('|') + ')$').test(file.type) ) {
  159.             return setUploadError('Неправильный тип файла');
  160.         }
  161.  
  162.         var reader = new FileReader;
  163.  
  164.         reader.onload = function(event) {
  165.             imagePreview(event.target.result);
  166.         };
  167.  
  168.         reader.readAsDataURL(file);
  169.     };
  170.    
  171.     gid('prev').onclick = function() {
  172.         prevFrame();
  173.         return false;
  174.     };
  175.    
  176.     gid('previewForm').onsubmit = function() {
  177.         var img = new Image;
  178.         img.src = gid('preview').toDataURL();
  179.        
  180.         img.onload = function() {
  181.             editImage( gid('imageType').options[ gid('imageType').selectedIndex ].value, this );
  182.         };
  183.        
  184.         return false;
  185.     };
  186.  
  187.     gid('text1').onchange = gid('text2').onchange = function() {
  188.         this.value = clean(this.value);
  189.     };
  190.    
  191.     gid('editForm').onsubmit = function() {
  192.         var data = gid('result').toDataURL('image/jpeg');
  193.         var binary = atob( data.substr( data.indexOf(',') + 1 ) );
  194.         var i = binary.length;
  195.         var view = new Uint8Array(i);
  196.        
  197.         while (i--) {
  198.             view[i] = binary.charCodeAt(i);
  199.         }
  200.        
  201.         VK.api('photos.getWallUploadServer', function(serverInfo) {
  202.             ajax.put('upload.php?url=' + encodeURIComponent(serverInfo.response.upload_url), new Blob([view]), {            
  203.                 done: function(uploadInfo) {
  204.                     eval('uploadInfo=' + uploadInfo);
  205.                    
  206.                     VK.api('photos.saveWallPhoto', uploadInfo, function(saveInfo) {
  207.                         VK.api('wall.post', {
  208.                             message: 'Я создал свой мем в приложении vk.com/app' + query.api_id,
  209.                             attachments: saveInfo.response[0].id + ',http://' + location.host
  210.                         }, function() {
  211.                             nextFrame();
  212.                         });
  213.                     });
  214.  
  215.                 }
  216.             });                  
  217.         });
  218.        
  219.         return false;
  220.     }
  221.    
  222.     gid('info').innerHTML = '&copy; Все права защищены <a href="/" target="_blank">' + location.host + '</a>, 2013.';  
  223.     VK.init(getFrame);
  224. });
Advertisement
Add Comment
Please, Sign In to add comment