Change content-type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr.Clean

    #1

    Change content-type

    I need to write a function to return an image that
    has been base64 encoded into a string.


    This is how it is done in PHP and I need to know
    how I can do the same in javascript:

    function img_65()
    {
    header("Content-type: image/png");
    header("Content-length: 174");
    echo base64_decode(
    "iVBORw0KGgoAAA ANSUhEUgAAABAAA AAWCAMAAAD+dOxO AAAAB3".
    "RJTUUH0gkQEysK Ru4rNAAAAAlwSFl zAAALEgAACxIB0t 1+/AAA".
    "AARnQU1BAACxjw v8YQUAAAAJUExUR QAAAP//AAAzM+Z4gaYAAA" .
    "AoSURBVHjaY2DA AExMDIyMUARkYxH AABAJCAKysQgMnB ZkhEVg".
    "QBwGAJlmANH7wb aHAAAAAElFTkSuQ mCC".
    "");
    }

    Here is how I call it in my PHP code:

    <?
    $img = $_GET["img"];
    switch($img)
    {
    case "65":
    img_65();
    break;
    }
    ?>


    In my HTML, I do this:

    <img src="digits.php ?img=65" />


    I have javascript base64 decode functions so I just
    need to know how to change the content-type like
    in img_65 function above above in javascript.

    Also, how would I call this javascript function
    in the img tag?



  • Martin Honnen

    #2
    Re: Change content-type



    Mr.Clean wrote:
    [color=blue]
    > I need to write a function to return an image that
    > has been base64 encoded into a string.[/color]
    [color=blue]
    > Here is how I call it in my PHP code:
    >
    > <?
    > $img = $_GET["img"];
    > switch($img)
    > {
    > case "65":
    > img_65();
    > break;
    > }
    > ?>
    >
    >
    > In my HTML, I do this:
    >
    > <img src="digits.php ?img=65" />
    >
    >
    > I have javascript base64 decode functions so I just
    > need to know how to change the content-type like
    > in img_65 function above above in javascript.[/color]

    Where do you want to use Javascript, on the server instead of PHP? Then
    it depends on the environment, in ASP you would need to set
    Response.Conten tType = 'image/png';
    With client-side JavaScript you can't change a HTTP response header
    obviously but you are dealing with specialized host objects like an
    HTMLImageElemen t which expects image data.
    [color=blue]
    > Also, how would I call this javascript function
    > in the img tag?[/color]

    Some browsers support
    <img src="javascript :functionToRetu rnImageData()" alt="">
    but don't rely on that, see
    <http://www.elf.org/pnglets/>
    <http://www.elf.org/essay/inline-image.html>
    Browser support for such stuff has not improved.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    Working...