6

Here is a youtube video showing the problem: https://www.youtube.com/watch?v=znzLQSYlsKM.

I give links to all code involved in the video description.

I'm using a js library to apply affects to an image. Then I'm triggering an event that gives the user a download link, and dynamically creates a form element with the base64 for the image. I then pass that to a php file and save it to a folder. The image you can download has the effect applied to it, but the one that is saved is saved without the effect. The problem is they are both the same exact file.

JS Code:

function showDownload(canvas){
    //this is how i send it to my main page and use ajax script to upload to the php file.

    var url = canvas.toDataURL("image/png;base64;");
    $('<input/>').attr({
         type: 'hidden', id: 'fileroast', name: 'fileroast', value: url
    }).appendTo('#output');

    // this is how i link the download file
    downloadImage.off('click').click(function(){
        var url = canvas.toDataURL("image/png;base64;");
        downloadImage.attr('href', url);
    }).fadeIn();

}

the filter code:

filters.click(function(e){
    e.preventDefault();
    var f = $(this);
    if(f.is('.active')){
        // Apply filters only once
        return false;
    }
    filters.removeClass('active');
    f.addClass('active');
    // Clone the canvas
    var clone = originalCanvas.clone();
    // Clone the image stored in the canvas as well
    clone[0].getContext('2d').drawImage(originalCanvas[0],0,0);
    // Add the clone to the page and trigger
    // the Caman library on it
    photo.find('canvas').remove().end().append(clone);
    var effect = $.trim(f[0].id);
    Caman(clone[0], function () {
        // If such an effect exists, use it:
        if( effect in this){
            this[effect]();
            this.render();
            showDownload(clone[0]);
        }
        else{
            hideDownload();
        }
    });
});
// Use the mousewheel plugin to scroll
// scroll the div more intuitively
filterContainer.find('ul').on('mousewheel',function(e, delta){
    this.scrollLeft -= (delta * 50);
    e.preventDefault();
});
Grant Zukel
  • 1,153
  • 2
  • 24
  • 48
  • Is `base64` portion of `var url = canvas.toDataURL("image/png;base64;");` ; valid image type parameter ? See https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL . Tried without "base64" ? Does `png` possess ability to animate similar to `gif` ? – guest271314 May 06 '15 at 02:45
  • @guest271314 it's not valid per-se (only the first mime-type), but the end result is the same in this case as an image mime that isn't recognized will default to png. –  May 06 '15 at 02:53
  • @KenFyrstenberg Yes. Not certain if interpret Question correctly . Was attempting to filter out possible items that could alter expected result. Not sure notice this type of effect with `png` previously . Perhaps aware there if `png` type images could "animate" similar to `gif` type images ? – guest271314 May 06 '15 at 02:56
  • 1
    @guest271314 there is [apng](https://en.wikipedia.org/wiki/APNG) but canvas cannot save out apng nor animated gifs directly. –  May 06 '15 at 02:58
  • @KenFyrstenberg Let me explain further. When you click a filter it runs the filter code which applies the clicked filter to the image. It then runs the showDownload which gives you the link to download the image with the filter applied. The clone[0] data that is sent to showDownload is the filtered images data. Thus, by logic if you are downloading that image with the click of a button based on the data in clone which is passed as canvas then sending the image as base64 to the php should have the filter applied as well because its the same image in both cases. – Grant Zukel May 06 '15 at 03:08
  • http://www.thewaywardjourney.com to see a life demo just register an account and click the camera and drag and drop an image into the dropzone drag and drop. You will see that you can download the image with the filter but when you click upload it will upload a picture without the filter. – Grant Zukel May 06 '15 at 03:08
  • @GrantZukel What is "filter" ? What is the effect of applying "filter" to image ? – guest271314 May 06 '15 at 03:09
  • @guest271314 its applying image effects through a js library called camman, it resizes the image, and then applies image effects to the image like instagram a filter is a image filter that changes the look of the photo like Instagram does. – Grant Zukel May 06 '15 at 03:37
  • @GrantZukel Not tried "Instagram" . Should be able to resize image , apply different effects to a `canvas`; see http://stackoverflow.com/questions/28882154/resize-a-html-5-canvas-and-its-content-in-pure-javascript/ , . Can create stacksnippets http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/ , jsfiddle http://jsfiddle.net to demonstrate ? – guest271314 May 06 '15 at 03:40
  • @GrantZukel See also http://stackoverflow.com/questions/26530970/get-base64-of-image-from-input-in-ie7/ , http://stackoverflow.com/questions/28923269/display-portion-of-scaled-image-in-div . Applied "effects" should also be displayed as set prior to download , after download . Not certain why "effects" set not applied at Question , if interpret question correctly ? – guest271314 May 06 '15 at 03:49
  • http://jsfiddle.net/96mef6oh/ there is a jsfiddle use test as the user gismogt as the password click the camera icon and drag an image to see what i mean. – Grant Zukel May 06 '15 at 04:03
  • it doesn't work entirely but it should get you close enough to see what im doing – Grant Zukel May 06 '15 at 04:04
  • @guest271314 I'm making a video to show you I will post link shortly – Grant Zukel May 06 '15 at 04:12
  • @GrantZukel jsfiddle http://jsfiddle.net/96mef6oh/ did not appear to permit "login" . Tried at site http://www.thewaywardjourney.com/ . Note, click on "Upload" does not appear to open files dialog. Dragged smaller image at http://stackoverflow.com/a/26615971/2801559 , applied first "Grungy" effect , then downloaded image file . Tried again, same image, selected "Old Boot" "effect" ; downloaded image file. Both downloaded images appeared to retain "effect" applied prior to "download" . First image set "Grungy" , second "Old Boot" "effect" http://jsfiddle.net/96mef6oh/2/ – guest271314 May 06 '15 at 04:43
  • @guest271314 https://www.youtube.com/watch?v=znzLQSYlsKM that should help – Grant Zukel May 06 '15 at 04:51
  • @GrantZukel Text on screen of screen appear blurry , difficult to view at video ; can include all full `js` at original post ? – guest271314 May 06 '15 at 05:02
  • Appear "effect" called after `clone[0].getContext('2d').drawImage(originalCanvas[0],0,0);` ? at `// If such an effect exists, use it: if( effect in this){` ? Also , what is expected result of `photo.find('canvas').remove().end().append(clone);` ? – guest271314 May 06 '15 at 05:10

1 Answers1

0

Here is the answer.

    function submitForm() {

      var fd = '';



      if(get_user != logged_username){



        }else{

        var d = new Date();

        var time = d.getTime();

        var fd = new FormData(document.getElementById("fileinfo"));



        $.ajax({

          url: "upload_photo.php",

          type: "POST",

          data: fd,

          enctype: 'multipart/form-data',

          processData: false,  // tell jQuery not to process the data

          contentType: false   // tell jQuery not to set contentType



        }).done(function( data ) {



            if (data.indexOf("Invalid") >= 0) { 

                alert('invalid file type, must be jpeg, jpg, or png.');

            }else{

              console.log(data);

              var post = {"pic_location":data, "time":time, "username": logged_username};

              var json_data = post;

              Cynergi.insert('http://thewaywardjourney.com:3000/profile_pictures', json_data);

              $( "#fileroast" ).remove();

            //this is where we save the photos location to the db for retrieveal.

            }

        });

        return false;

    }

   }

script.js

var apply = $('#apply');



function showDownload(canvas){

    apply.off('click').click(function(){

        var url = canvas.toDataURL("image/png;base64;");

        $('<input/>').attr({type: 'hidden', id: 'fileroast', name: 'fileroast', value: url}).appendTo('#output');

        console.log(url);   

        console.log('apply');

    }).fadeIn();

}



function hideDownload(){

    downloadImage.fadeOut();

}

modal hhtml

<div id="uploadPic" class="modal fade" >

  <form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-header" style="background:#f3f3f3;">

                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>

                <h4 class="modal-title" style="color:black;">Choose picture to upload as profile pic.</h4>



            </div>

            <div class="modal-body">



              <div id="photo">





              </div>



              <div id="filterContainer" style='width:400px;'>

                <ul id="filters" style='width:400px;'>

                  <li> <a href="#" id="normal">Normal</a> </li>

                  <li> <a href="#" id="vintage">Vintage</a> </li>

                  <li> <a href="#" id="lomo">Lomo</a> </li>

                  <li> <a href="#" id="clarity">Clarity</a> </li>

                  <li> <a href="#" id="sinCity">Sin City</a> </li>

                  <li> <a href="#" id="sunrise">Sunrise</a> </li> 

                  <li> <a href="#" id="crossProcess">Cross Process</a> </li>

                  <li> <a href="#" id="orangePeel">Orange Peel</a> </li>

                  <li> <a href="#" id="love">Love</a> </li>

                  <li> <a href="#" id="grungy">Grungy</a> </li>

                  <li> <a href="#" id="jarques">Jarques</a> </li>

                  <li> <a href="#" id="pinhole">Pinhole</a> </li>

                  <li> <a href="#" id="oldBoot">Old Boot</a> </li>

                  <li> <a href="#" id="glowingSun">Glowing Sun</a> </li>

                  <li> <a href="#" id="hazyDays">Hazy Days</a> </li>

                  <li> <a href="#" id="herMajesty">Her Majesty</a> </li>

                  <li> <a href="#" id="nostalgia">Nostalgia</a> </li>

                  <li> <a href="#" id="hemingway">Hemingway</a> </li>

                  <li> <a href="#" id="concentrate">Concentrate</a> </li>

                </ul>

              </div>

              <div id='output_file'></div>

              <div id="output"></div>



            </div>

            <div class="modal-footer">

              <button id='apply' class="btn btn-info">Upload</button>

                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>



            </div>

        </div>

    </div>

  </form>

</div>    

php

<?php

    // requires php5

    define('UPLOAD_DIR', 'images/profile_images/');

    $img = $_POST['fileroast'];

    $img = str_replace('data:image/png;base64,', '', $img);

    $img = str_replace(' ', '+', $img);

    $data = base64_decode($img);

    $files = UPLOAD_DIR . uniqid() . '.png';

    $success = file_put_contents($files, $data);

    print $files;

    //print $file ? 'default':'images/profile_images/default.png';

?>
Grant Zukel
  • 1,153
  • 2
  • 24
  • 48
  • Does answer resolve Question ? Which specific portions were adjusted from original `js` , `php` , to achieve expected results ? – guest271314 May 06 '15 at 16:41
  • 1
    yes it is the answer this is the code that works fully. the true answer was instead of using a download like call the .off with a button press and then serve the image to the php file. I'm not entirely sure why though, I can't find it in any of the scripts but that solved the problem – Grant Zukel May 06 '15 at 19:03