Jump to content

[SOLVED] Ajax call to show PNG image comes out garbage


sandbird

Recommended Posts

Hi, I am trying to call a php page through ajax to show a png file created in that php file.

My code is this:

 

Index.html

<script text="text/Javascript">

//////////////////////////////////////////////////////////////////////////////////////////////
// AJAX FUNCTIONS ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
function xmlhttpPost(f,  strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(f, self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(f);
}


function updatepage(f,str) {
    document.getElementById(f).innerHTML = str;
}


</script>

<input id="makebutton" onclick="this.disabled=true; document.getElementById('result').innerHTML='<p><b>Bringing pic...</b> Please wait</p>.'; xmlhttpPost('result','button.php');" value="Bring Pic" type="button">

<div style="text-align: center;"><span id="result"></span></div>

 

button.php

<?
// print the header telling the web browser that its a png image
header ("Content-type: image/png",false);
// Create an image width 200 and height 30 pixels
$im=imagecreate(200,30) or die("Can not initialize GD Stream");
// BackGround Color of the image
$bgcolor=ImageColorAllocate($im,255,0,0);
// text color of the image
$textcolor=ImageColorAllocate($im,255,255,255);
//write string to image
// 5 : font , 1 : x , 5 : y
imagestring($im,5,1,5,"Hello World",$textcolor);
imagepng($im); // output to browser
imagedestroy($im); // Frees Memory
?>

 

Output

outputge4.jpg

 

What the hell am i doing wrong ?  ??? ???

Link to comment
Share on other sites

You cannot embed images in HTML.  You must use an image tag (img) so that the browser knows to embed it as an image on the page.

 

It would be better to just change the innerHTML of result to <img src="button.php" /> or something like that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.