473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 11335
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

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

Similar topics

1
by: Swarup | last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling it into a byte arry. Now i have to convert it into a string to store it in the database. I use...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
3
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
2
by: Howard Weiss | last post by:
I am reading a file (containing short integers). To read the file, I use the following FileStream *myFile = new FileStream(FileName, FileMode::Open, FileAccess::Read); __int64 myFileSize =...
3
by: Dennis | last post by:
I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte array containing the resulting JPEG Image as follows: Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
5
by: bbb | last post by:
Hi, I need to convert XML files from Japanese encoding to UTF-8. I was using the following code: using ( FileStream fs = File.OpenRead(fromFile) ) { int fileSize = (int)fs.Length; int buffer...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.