JavaScript Saving a canvas to a PNG or JPEG file canvas save jpeg,save canvas image jpeg

I'm currently programming a webstie where the users can Upload .mp4 videos. I would like to make a thumbnail when the user uploads his video and write this image to a .jpg or .png file on my server.

The problem that I encounter is that the file is stored on my server but it isn't viewed/ seen as an image file.

This is my code for the moment.

The HTML part

<canvas id="c"></canvas>
<video id="v" controls>
  <source src="/215003/1.1/videos/7/f9ed57e2b0ee1e87a4cbc17c7b743eff.mp4" type="video/mp4">
</video>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

The javascript part where I process the canvas etc.

document.addEventListener('DOMContentLoaded', function(){
var v = document.getElementById('v');
var canvas = document.getElementById('c');
var context = canvas.getContext('2d');

var v = document.getElementById("v");

v.addEventListener('canplay', function() {
 this.currentTime = this.duration / 2;
  }, false);

v.addEventListener( "loadedmetadata", function (e) {
 var cw = this.videoWidth,
     ch = this.videoHeight;
     console.log(cw);
     console.log(ch);
     canvas.width = cw;
     canvas.height = ch;
     draw(this,context,cw,ch);
     var dataURL = canvas.toDataURL('image/png');

    $.ajax({
      type: 'POST',
      url: 'saveThumb.php',
      data: 'img=' + dataURL
    }).done(function(o) {
        console.log(dataURL);
      console.log('saved'); 
    });
 }, false );
},false);
function draw(v,c,w,h) {
  c.drawImage(v,0,0,w,h);
  setTimeout(draw,20,v,c,w,h);
}

The PHP file to process the data and store the image :

<?php
// requires php5
define('UPLOAD_DIR', 'img/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>

Somebody that can help me out ? :)

Answer:1

I seldom ever use PHP but I do know that Base64 encoding does not have any spaces in it. Not that it would change anything because there are no spaces, but why the second replace space with plus?

As to your problem. You need to change the event you are using to trigger the post. onloadedmetadata is fired before there is any data to render a frame. You should be using the oncanplay then you will have data to send, though as it is you should at least be getting a blank image. To see what you have on the client add 'image = new Image(); image.src=dataURL;' then add that image to the dom and you can see if you have got what you wanted rather than guessing.

Apart from that I can't find anything.

Answer:2

I've hit a snag after a few hours -- wondered if a fresh minded developer can just review the below, the code is simplied to show the problem. I am filtering a property value on an array of objects, ...

I've hit a snag after a few hours -- wondered if a fresh minded developer can just review the below, the code is simplied to show the problem. I am filtering a property value on an array of objects, ...

  1. filter array objects javascript
  2. filter array objects angular 6
  3. filter array objects
  4. filter array objects js
  5. filter array objects swift
  6. filter array objects jquery
  7. filter array objects angularjs
  8. filter array objects lodash
  9. filter array objects angular2
  10. array filter objects php
  11. filter array of objects by property
  12. filter array of objects typescript
  13. filter array of objects javascript es6
  14. filter array of objects by property javascript
  15. filter array of objects javascript by key
  16. filter array of objects by key
  17. filter array of objects by key value
  18. filter array of objects javascript by value
  19. filter array of objects based on property
  20. filter array of objects javascript based on property

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem. I have a sidebar which has ...

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem. I have a sidebar which has ...

  1. react redux component not updating
  2. react redux component not updating on state change
  3. react redux component lifecycle
  4. react redux component state
  5. react redux component example
  6. react redux component local state
  7. react redux component did mount
  8. react redux component communication
  9. react redux component dispatch
  10. react redux component not re rendering
  11. react redux component testing
  12. react redux component rendering twice
  13. react redux component
  14. react redux component will receive props
  15. react redux component container
  16. react redux component does not update
  17. react redux component did update
  18. react redux component vs container
  19. react redux component props
  20. react redux component library

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

By what logic js works (!+[]+[]+![]).length returns 9? How is that possible? As I know js has dynamic types cast but very hard to understand whats going on here console.log((!+[]+[]+![]).length);

By what logic js works (!+[]+[]+![]).length returns 9? How is that possible? As I know js has dynamic types cast but very hard to understand whats going on here console.log((!+[]+[]+![]).length);

  1. console log length of array
  2. console.log length
  3. console.log length limit
  4. console log length of string
  5. console.log max length
  6. intellij console log length
  7. javascript console.log length
  8. console.log('hi '.length)
  9. console.log maximum length
  10. eclipse console log length
  11. console.log array length 0
  12. console.log(data.length)
  13. console.log(array.length)
  14. javascript console.log max length
  15. javascript console log array length
  16. ie console log length
  17. console.log(foot.length)
  18. console.log($('ol li.even').length)