How to convert a gif file to byte array? thanks!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • barmimor
    New Member
    • May 2009
    • 4

    How to convert a gif file to byte array? thanks!

    i can read a gif to a string,

    $file = 'IconValid.gif' ;
    $image = fread(fopen($fi le,r),filesize( $file));

    but how can i get a byte array from gif file.

    thanks in advance!
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Not sure whar you are trying to do but what about
    Code:
    file_get_contents()

    Comment

    • barmimor
      New Member
      • May 2009
      • 4

      #3
      i hope it can return a byte array from gif file.
      like $image = array(\x0d,\x08 ,\x02,\x0b,\x0c ).
      file_get_conten ts() only can return string, or how can i convert the string to array.

      i need to access a web service to invoke a method, the method has a argument which type is byte[], so i need the array $image. any idea?

      Comment

      • Ciary
        Recognized Expert New Member
        • Apr 2009
        • 247

        #4
        i guess you want to alter the image after that?

        problem with gif, jpg and png files is that they are encrypted to reduce size. if you really want every bit in an array, you'll need bitmaps. this means that you'll need to convert your image first.

        then you can read the file as a string and use substring and split to get the data into an array.

        even so, im not sure if thats possible

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          Sorry I meant
          Code:
          file();

          Comment

          • barmimor
            New Member
            • May 2009
            • 4

            #6
            i've tried the function file(), it can return an string array, but it's invalid value for the web service. it need binary value.
            Hi Ciary, can you show me some sample for your solution?

            Comment

            • Ciary
              Recognized Expert New Member
              • Apr 2009
              • 247

              #7
              like i said, i don't think it's possible. to see why: open a bmp-file with notepad or wordpad. you'll see it isnt that easy to read what color every pixel has. even if you do know what it looks like.

              if you use fread(), you'll read the file as if it was a txt-file. therefore i dont think you can split it up.

              what are you going to do with it on the webservice?

              Comment

              • barmimor
                New Member
                • May 2009
                • 4

                #8
                webservice can save the byte[] value to DB, and it only can be byte[]. i can't change t he webservice. so there is any other method to get the byte[] with php. can i load the gif file to memory, and then return the byte[] from memory?

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  Are you tring to do a string to byte conversion as in .NET.
                  I don't think PHP has such low level control.
                  It is like this in C#
                  Code:
                  public static byte[] StrToByteArray(string str)
                  {
                      System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
                      return encoding.GetBytes(str);
                  }

                  Comment

                  • Ciary
                    Recognized Expert New Member
                    • Apr 2009
                    • 247

                    #10
                    if you don't have access to the webservice, i don't think i can help you. i can't see a way to convert an image into a byte array using php. there was another way in which you stream your image to your webservice using SOAP and then do the conversion there.

                    you can always write your own webservice with the conversion to the array and then use a webservice in the webservice to write it into the DB. its a huge detour but it should work

                    Comment

                    Working...