Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    Nov 2015
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttpRequest to get a php script loading a thumbnail image

    Hello @all,

    first off: I'm a German php programmer with my first steps in javascript. So sorry if I make some mistakes in explaining.

    My issue: the src is not filled:
    In my html file I have the following code:
    Code:
    <script>
            ...
           function fillNavBar() {
           ...
                elImg.src = getThumbnail(strFileName,cx*dpr,cy*dpr);
           ...
          }
    
          function getThumbnail(filename,width,height){
              //return filename;
              
              var oImg = new XMLHttpRequest();
              
              oImg.responseType = "blob";
              oImg.onreadystatechange = function() {
                if (oImg.readyState == 4 && oImg.status == 200) {
                    var blob = new Blob([oImg.response],{type:'image/jpeg'});
                    var urlCreator = window.URL || window.webkitURL;
                    var imageUrl = urlCreator.createObjectURL( blob );
                    return imageUrl;
                }
              };
              oImg.open("GET","./panos/create_thumbnail.php?src=" + filename + "&width=" + width + "&height=" + height , true);  // `false` makes the request synchronous
              oImg.send();        
          }
    To debug I use netbeans / chrome netbeans connector:
    - The value of var blob is BLOB. Is this correct or the the first problem!???
    - The value of var imageURL is "blob:http%3A//localhost%3A8383/5066ed81-2668-4ab7-9b60-be912cc4e20a".

    The called php Skript sends an image to the browser. So do I have to strip the imageURL???

    Thx in advance

    maen

  2. #2
    New Coder
    Join Date
    Sep 2014
    Posts
    87
    Thanks
    0
    Thanked 11 Times in 11 Posts
    First, Is there any reason why you could not just do:


    PHP Code:
    elImg.src "./panos/create_thumbnail.php?src=" whatever;
    instead of
    elImg
    .src getThumbnail(strFileName,cx*dpr,cy*dpr); 
    You can always resize the image using "elImg.width = whatever, elImg.height = whatever" or the mimetype and dimension could be returned by create_thumbnail.php.

    in any case, if you are using synchronous (not recommended) request, then don't do oImg.onreadystatechange. Instead, just process the response after the send.

    PHP Code:
    oImg.open("GET","./panos/create_thumbnail.php?src=" filename "&width=" width "&height=" height true);
    oImg.send(); 

     if (
    oImg.status == 200) {
        var 
    blob = new Blob([oImg.response],{type:'image/jpeg'});
        var 
    urlCreator window.URL || window.webkitURL;
        var 
    imageUrl urlCreator.createObjectURLblob );
        return 
    imageUrl;
    }
    else {
        
    alert("response -> " oImg.status);
        return 
    null
    With synchronous request, I am not sure if the returned status is in oImg.status.

  3. #3
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,103
    Thanks
    30
    Thanked 241 Times in 239 Posts
    getThumbnail does not return anything


    do not 'return' from onreadystatechange
    instead supply a function to
    be called upon completion

    Code:
    getThumbnail(strFileName,cx*dpr,cy*dpr,fillNavBar);
    
    
           function fillNavBar(arg) {
           ...
                elImg.src = arg;
           ...
          }
    
    
          function getThumbnail(filename,width,height,cFunc){
              //return filename;
              
              var oImg = new XMLHttpRequest();
              
              oImg.responseType = "blob";
              oImg.onreadystatechange = function() {
                if (oImg.readyState == 4 && oImg.status == 200) {
                    var blob = new Blob([oImg.response],{type:'image/jpeg'});
                    var urlCreator = window.URL || window.webkitURL;
                    var imageUrl = urlCreator.createObjectURL( blob );
                    cFunc( imageUrl );
                }
              };
              oImg.open("GET","./panos/create_thumbnail.php?src=" + filename + "&width=" + width + "&height=" + height , true);  // `false` makes the request synchronous
              oImg.send();        
          }
    blob and URL are native in Microsoft Edge
    but are not available in Internet Explorer
    Last edited by DaveyErwin; 11-22-2015 at 04:30 PM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •