Jump to content

Recommended Posts

Hello,

I have a web service returning me an Array of Bytes in the form of a Jpg.

I am trying to us the GD lib to create the image.

 

Here is my code ($imageBytes is the array of Bytes)

//Create the image from Bytes to JPEG.
$img = imagecreatefromstring($imageBytes);
header('Content-Type: image/JPEG');
header("Content-Length: " .strlen($imageBytes));
imagejpeg($img);

 

But the image is not displaying.

 

Should I use GD, or should I use something else.

Any ideas what I am doing wrong.

Link to comment
https://forums.phpfreaks.com/topic/47753-solved-create-image-from-bytes/
Share on other sites

Well I get it from a web service

Like so..

 

$fixedSizeReturn = $client->WarehouseExtractRadiusFixedSize_v1($radiusSize);
$imageBytes = $fixedSizeReturn->WarehouseExtractRadiusFixedSize_v1Result->Result->Image->ImageBytes;

 

The documentation says

"Returns an image as a JPEG as an array of bytes"

 

I have done it in ASP.NET like so

        Response.ContentType = "image/JPEG";
        Response.AppendHeader("Content-Length", imageBytes.Length.ToString());
        Response.Cache.SetLastModified(DateTime.Now);
        Response.Cache.SetCacheability(HttpCacheability.Public);
        Response.BinaryWrite(imageBytes);

 

But, now I need to do the same thing in PHP

 

Drew

well if imagecreatefromstring isn't working then maybe try building a string from the array

 

ie

<?php
$newimage = "";
foreach ($imageBytes as $b)
{
$newimage .= $b;
}

$img = imagecreatefromstring($newimage);

?>

 

 

this is a pure guess

You know what my problem was.....

 

I had white space in my php file at the top..

So <php should have been on line 0 but I had it on line 10

 

I never thought that would matter,  but it did.

Maybe passes extra bytes  to the file.

 

Thanks for all your help.

 

Drew

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.