pictures from XML file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    pictures from XML file.

    I've been able to parse the XML files used to update my database.

    Now, I receive image in XML files, like this:

    <PICTURES COUNT="3" CDATA="1">
    <PIC NR="1"><![CDATA[/9j/4AAQSkZJ.....=]]></PIC>
    <PIC NR="2"><![CDATA[/9j/4AAQSk......... ....></PIC>
    <PIC NR="3"><![CDATA[/9j/4AAQSk......... ....></PIC>
    </PICTURES>

    How to save those 3 images in 3 jpg files ???

    Thanks

    Bob

  • Janwillem Borleffs

    #2
    Re: pictures from XML file.

    Bob Bedford wrote:[color=blue]
    > I've been able to parse the XML files used to update my database.
    >
    > Now, I receive image in XML files, like this:
    >
    > <PICTURES COUNT="3" CDATA="1">
    > <PIC NR="1"><![CDATA[/9j/4AAQSkZJ.....=]]></PIC>
    > <PIC NR="2"><![CDATA[/9j/4AAQSk......... ....></PIC>
    > <PIC NR="3"><![CDATA[/9j/4AAQSk......... ....></PIC>
    > </PICTURES>
    >
    > How to save those 3 images in 3 jpg files ???
    >[/color]

    You said that you are able to parse the XML files, so I'm not going to
    suggest how to do this.

    There are someways to write the data to a file, one of the most simple ones
    is with:

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    This function is binary-safe. Before file creation, you should decode the
    data when it's encoded. When the data isn't encoded, I advise you to encode
    it before addition to the XML file. Otherwise, some binary data can get
    lost.

    A common way of encoding is base64, for which you can apply base64_decode()
    to decode the data and base64_encode() for encoding.


    JW



    Comment

    • Andy Hassall

      #3
      Re: pictures from XML file.

      On Wed, 15 Sep 2004 22:28:45 +0200, "Bob Bedford"
      <bedford1@YouKn owWhatToDoHereh otmail.com> wrote:
      [color=blue]
      >I've been able to parse the XML files used to update my database.
      >
      >Now, I receive image in XML files, like this:
      >
      ><PICTURES COUNT="3" CDATA="1">
      ><PIC NR="1"><![CDATA[/9j/4AAQSkZJ.....=]]></PIC>
      ><PIC NR="2"><![CDATA[/9j/4AAQSk......... ....></PIC>
      ><PIC NR="3"><![CDATA[/9j/4AAQSk......... ....></PIC>
      ></PICTURES>
      >
      >How to save those 3 images in 3 jpg files ???[/color]

      Since you've said you've got the parsing sorted, that must just leave decoding
      and saving.

      Looks like it's base64 encoded, so:






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

      Comment

      • Bob Bedford

        #4
        Re: pictures from XML file.

        > Since you've said you've got the parsing sorted, that must just leave
        decoding[color=blue]
        > and saving.
        >
        > Looks like it's base64 encoded, so:[/color]

        Here is the code, but probably the problem is somewhere else:
        if(isset($datas["PICTURES"])){
        while(list($k,$ v) = each($datas["PICTURES"])){
        if(is_array($da tas["PICTURES"][$k])){
        while(list($k1, $v1) = each($datas["PICTURES"][$k])){
        echo($k1."->".$v1."<br>" );
        if($k1 == 'PIC'){
        echo(base64_dec ode($v1)."<br>< br>"); //print to screen
        if($imgfile =
        fopen($UserID.'-'.$datas["NUM"].'-'.$num.'.jpg',' wb')){
        fwrite($imgfile ,base64_decode( $v1));
        fflush($imgfile );
        fclose($imgfile );
        }
        else
        echo('error creating output file');
        }else
        $num = $v1;
        }
        }
        }
        }

        Parsing: got the code on php.net:
        while ($data = fread($fp, 4096)) {
        if (!xml_parse($xm l_parser, $data, feof($fp))) {
        die(sprintf("XM L error: %s at line %d",
        xml_error_strin g(xml_get_error _code($xml_pars er)),
        xml_get_current _line_number($x ml_parser)));
        rename($XMLPath .$file, $XMLPath.$failp ath.$file); //move file in error
        path
        }
        }

        the XML structure
        <PICTURES COUNT="3">
        <PIC NR="1"><![CDATA[/9j/4AAQSkZJRg..... ..........

        if I try to print on the screen with "echo(base64_de code($v1)."<br> <br>");"
        I get line of different size and the created files are 1ko ! They should be
        bigger ! Any idea ??? any string limit, a problem in the parser ???

        Cheers

        Bob


        Comment

        • Alvaro G. Vicario

          #5
          Re: pictures from XML file.

          *** Bob Bedford escribió/wrote (Thu, 16 Sep 2004 00:00:19 +0200):[color=blue]
          > while ($data = fread($fp, 4096)) {[/color]

          Note that fread() uses a 4096 bytes buffer. That means that an image will
          probably be split in more than one while() looping. Just make sure your XML
          parser takes this case into account.


          --
          -+ Álvaro G. Vicario - Burgos, Spain
          +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
          ++ Las dudas informáticas recibidas por correo irán directas a la papelera
          -+ I'm not a free help desk, please don't e-mail me your questions
          --

          Comment

          Working...