How to convert gif image to Hex using PHP code

Posted on 2009-05-02
Medium Priority
Last Modified: 2012-05-06
Hi, I need to convert gif or jpg image to OTA format and from OTA to  Hex code to send the picture sms using php code. Please help me to get the complete code.
Question by:Pinnacle411
  • 2
LVL 20

Expert Comment

ID: 24285397
Seeing the format described e.g. in http://en.wikipedia.org/wiki/OTA_bitmap, I'd first convert to an uncompressed b/w bitmap (hopefully with a width a multiple of 8). Then it's just a matter of outputing the simple header and then the bytes one by one.

Author Comment

ID: 24292765
I have tried all the links you give me, but I am not very good in other languages, Can someone  please  give me the complete php code.
LVL 20

Accepted Solution

thehagman earned 1500 total points
ID: 24295200
In the following code that uses the GD library, write an output(x) routine according to your needs: It may output x as binary octet or as two hex digits, according to whether you want binary or hex OTA data.
$im = imagecreatefromgif($imgname);
$width = imagesx($im); // must be < 256 (and >0)
$height = imagesy($im); // must be < 256 (and >0)
$byteac = 1;
for ($y=0; $y<$height; ++$y) {
  for ($x=0; $x<$width; ++%x) {
    $c = imagecolorat($im, $x, $y);
    $byteac += $byteac;
    if ($c > 0) { // or whatever color(s) shall become black
    if ($byteac >= 256) {
      output($byteac - 256);
      $byteac = 1;
while ($byteac < 256) {
  $byteac += $byteac;
if ($byteac > 256) {

Open in new window


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question