469,339 Members | 8,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,339 developers. It's quick & easy.

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

i can read a gif to a string,

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

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

thanks in advance!
May 6 '09 #1
9 10654
code green
1,726 Expert 1GB
Not sure whar you are trying to do but what about
Expand|Select|Wrap|Line Numbers
  1. file_get_contents()
May 6 '09 #2
i hope it can return a byte array from gif file.
like $image = array(\x0d,\x08,\x02,\x0b,\x0c).
file_get_contents() 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?
May 6 '09 #3
Ciary
247 Expert 100+
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
May 6 '09 #4
code green
1,726 Expert 1GB
Sorry I meant
Expand|Select|Wrap|Line Numbers
  1. file();
May 6 '09 #5
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?
May 6 '09 #6
Ciary
247 Expert 100+
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?
May 6 '09 #7
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?
May 6 '09 #8
code green
1,726 Expert 1GB
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#
Expand|Select|Wrap|Line Numbers
  1. public static byte[] StrToByteArray(string str)
  2. {
  3.     System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
  4.     return encoding.GetBytes(str);
  5. }
May 6 '09 #9
Ciary
247 Expert 100+
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
May 6 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Swarup | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
5 posts views Thread by bbb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.