print imagedata from string variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henri Schomäcker

    print imagedata from string variable

    Hi folks,

    I got a windows com executable which returns a jpg image in a BSTR.

    Let's say, the var that holds the data is $imgData.

    With perl, in a cgi script, I may simpy write:
    8<--------8<--------8<--------8<--------8<--------
    $| = 1;
    print "Content-type: image/jpeg\n\n";
    binmode(STDOUT) ;
    print $imgData;
    8<--------8<--------8<--------8<--------8<--------
    and the Image is propperly printed to the Client-Browser.

    Now I want to achieve the same with php, but I just can't find a way to
    treat the data as binary instead of a formated string.
    As far as I know, echo $imgData shouldn't expect a formated string, as print
    does.

    (I know, returning a Safearray from the com executable would be a better way
    and probably I will also do it that way, but at the moment, I'm trying to
    solve this problem.)

    It'll make me very happy if someone could give me a hint...

    Many thanks in advance,
    yours Henri
  • Virgil Green

    #2
    Re: print imagedata from string variable

    "Henri Schomäcker" <hs@byteconcept s.de> wrote in message
    news:cd92lh$l0v $07$1@news.t-online.com...[color=blue]
    > Hi folks,
    >
    > I got a windows com executable which returns a jpg image in a BSTR.
    >
    > Let's say, the var that holds the data is $imgData.
    >
    > With perl, in a cgi script, I may simpy write:
    > 8<--------8<--------8<--------8<--------8<--------
    > $| = 1;
    > print "Content-type: image/jpeg\n\n";
    > binmode(STDOUT) ;
    > print $imgData;
    > 8<--------8<--------8<--------8<--------8<--------
    > and the Image is propperly printed to the Client-Browser.
    >
    > Now I want to achieve the same with php, but I just can't find a way to
    > treat the data as binary instead of a formated string.
    > As far as I know, echo $imgData shouldn't expect a formated string, as[/color]
    print[color=blue]
    > does.
    >
    > (I know, returning a Safearray from the com executable would be a better[/color]
    way[color=blue]
    > and probably I will also do it that way, but at the moment, I'm trying to
    > solve this problem.)
    >[/color]




    though I haven't done it from a string before.

    - Virgil


    Comment

    • Andy Hassall

      #3
      Re: print imagedata from string variable

      On Fri, 16 Jul 2004 19:18:42 +0200, Henri Schomäcker <hs@byteconcept s.de>
      wrote:
      [color=blue]
      >I got a windows com executable which returns a jpg image in a BSTR.
      >
      >Let's say, the var that holds the data is $imgData.
      >
      >With perl, in a cgi script, I may simpy write:
      >8<--------8<--------8<--------8<--------8<--------
      >$| = 1;
      >print "Content-type: image/jpeg\n\n";
      >binmode(STDOUT );
      >print $imgData;
      >8<--------8<--------8<--------8<--------8<--------
      >and the Image is propperly printed to the Client-Browser.
      >
      >Now I want to achieve the same with php, but I just can't find a way to
      >treat the data as binary instead of a formated string.
      >As far as I know, echo $imgData shouldn't expect a formated string, as print
      >does.[/color]

      I don't know what you mean by a "formated string" with regards to print.
      You can print binary data without any special treatment.

      (If you've opened a filehandle and are outputting to that, you have to open it
      with 'b' in the open mode, but output to the browser is binary-safe by
      default).

      So it becomes just:

      <?php
      // magic to get $imgData = a jpeg
      header('Content-type: image/jpeg');
      print $imgData;
      ?>

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

      Comment

      • Henri Schomäcker

        #4
        Re: print imagedata from string variable

        > Andy Hassall wrote:[color=blue]
        > I don't know what you mean by a "formated string" with regards to print.
        > You can print binary data without any special treatment.
        >
        > (If you've opened a filehandle and are outputting to that, you have to
        > open it
        > with 'b' in the open mode, but output to the browser is binary-safe by
        > default).
        >
        > So it becomes just:
        >
        > <?php
        > // magic to get $imgData = a jpeg
        > header('Content-type: image/jpeg');
        > print $imgData;
        > ?>
        >
        > --
        > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]

        ;-) Yes, this is exactly what I mean, but it does NOT work.
        It should be typeless, but it seems it isn't because it just recognices the
        data until the first 0 like a 0 terminated string and that wouldn't be
        typeless.

        As I wrote, It works with perl, which really handles the data typeless, but
        when I recieve the Imagedata with php, php handles the received data like
        vbscript as a BSTR type, which really is send by the com object.

        Here's how I get the data with php, perhapst I should do this another way?
        8<--------8<--------8<--------8<--------8<--------8<--------8<--------
        $comObj = new COM("mytools.My Method") or Die ("Failed to connect to Com");
        $imgData['img'] = $comObj->getImage() or Die ("Couldn't get data from
        Object");
        8<--------8<--------8<--------8<--------8<--------8<--------8<--------

        Many thanks for the replys,
        yours Henri

        Comment

        • Henri Schomäcker

          #5
          Re: print imagedata from string variable

          Oops, sorry I have to make a correction,

          By mistake, I copied another try but the correct code I used is:
          8<--------8<--------8<--------8<--------8<--------8<--------8<--------
          $imgData = $comObj->getImage() or Die ("Couldn't get data from Object");
          8<--------8<--------8<--------8<--------8<--------8<--------8<--------

          Many thanks for the replys,
          yours Henri

          Comment

          • Andy Hassall

            #6
            Re: print imagedata from string variable

            On Sat, 17 Jul 2004 13:32:15 +0200, Henri Schomäcker <hs@byteconcept s.de>
            wrote:
            [color=blue][color=green]
            >> Andy Hassall wrote:
            >> I don't know what you mean by a "formated string" with regards to print.
            >> You can print binary data without any special treatment.
            >>
            >> (If you've opened a filehandle and are outputting to that, you have to
            >> open it
            >> with 'b' in the open mode, but output to the browser is binary-safe by
            >> default).
            >>
            >> So it becomes just:
            >>
            >> <?php
            >> // magic to get $imgData = a jpeg
            >> header('Content-type: image/jpeg');
            >> print $imgData;
            >> ?>[/color]
            >
            >;-) Yes, this is exactly what I mean, but it does NOT work.
            >It should be typeless, but it seems it isn't because it just recognices the
            >data until the first 0 like a 0 terminated string and that wouldn't be
            >typeless.
            >
            >As I wrote, It works with perl, which really handles the data typeless, but
            >when I recieve the Imagedata with php, php handles the received data like
            >vbscript as a BSTR type, which really is send by the com object.
            >
            >Here's how I get the data with php, perhapst I should do this another way?
            >8<--------8<--------8<--------8<--------8<--------8<--------8<--------
            >$comObj = new COM("mytools.My Method") or Die ("Failed to connect to Com");
            >$imgData['img'] = $comObj->getImage() or Die ("Couldn't get data from
            >Object");
            >8<--------8<--------8<--------8<--------8<--------8<--------8<--------[/color]

            It's sounding like it's not a core PHP problem (since PHP strings and print
            can definitely handle binary data containing NUL bytes), but rather a problem
            somewhere in the COM extension that's stopping the full data reaching the PHP
            variable. I've not used the COM extension myself so can't suggest much more on
            that. Perhaps try http://bugs.php.net ?

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

            Comment

            • Henri Schomäcker

              #7
              Re: print imagedata from string variable

              > Virgil Green wrote:[color=blue]
              >
              > www.php.net/imagecreatefromstring
              > www.php.net/imagejpeg
              >
              > though I haven't done it from a string before.
              >
              > - Virgil[/color]

              Thanks, but these functions are non-standart extensions that are part of the
              gd extension in php, which I don't want to use because the imagedata is
              already returned correctly by the com object.

              But anyway, thanks for the reply,
              yours Henri

              Comment

              • Henri Schomäcker

                #8
                Re: print imagedata from string variable

                >> On Sat, 17 Jul 2004 13:32:15 +0200, Henri Schomäcker wrote:[color=blue][color=green]
                >>
                >>As I wrote, It works with perl, which really handles the data typeless,
                >>but when I recieve the Imagedata with php, php handles the received data
                >>like vbscript as a BSTR type, which really is send by the com object.
                >>
                >>Here's how I get the data with php, perhapst I should do this another way?
                >>8<--------8<--------8<--------8<--------8<--------8<--------8<--------
                >>$comObj = new COM("mytools.My Method") or Die ("Failed to connect to Com");
                >>$imgData = $comObj->getImage() or Die ("Couldn't get data from Object");
                >>8<--------8<--------8<--------8<--------8<--------8<--------8<--------[/color][/color]
                [color=blue]
                > Andy Hassall wrote:
                >
                > It's sounding like it's not a core PHP problem (since PHP strings and
                > print
                > can definitely handle binary data containing NUL bytes), but rather a
                > problem somewhere in the COM extension that's stopping the full data
                > reaching the PHP variable. I've not used the COM extension myself so can't
                > suggest much more on that. Perhaps try http://bugs.php.net ?
                >
                > --
                > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]

                Seems you're right. I also tried to set a Variant type of $imgData to VT_UI1
                before, but it had no effect.

                I also searched http://bugs.php.net but couldn't find a bug related to my
                problem. It also seems that not many developers really use com with php
                which makes searching for a solution really hard.

                I think I'll give up and use the time to recode the com component to return
                a Safearray so using it with other languages than perl will be made easier.

                Many thanks for all your answers,
                yours Henri

                Comment

                Working...