Jump to content

getting image to show from a php script


9ball

Recommended Posts

I am just testing how image works with php and here is the code:

 

<?php

$myImage=ImageCreate(150,150);

 

$black=ImageColorAllocate($myImage, 0, 0, 0);

$white=ImageColorAllocate($myImage, 255, 255, 255);

$red=ImageColorAllocate($myImage, 255, 0, 0);

$green=ImageColorAllocate($myImage, 0, 255, 0);

$blue=ImageColorAllocate($myImage, 0, 0, 255);

 

ImageRectangle($myImage, 15, 15, 55, 85, $red);

ImageRectangle($myImage, 55, 85, 125, 135, $white);

 

header ("Content-type: image/jpeg");

ImageJpeg($myImage);

 

ImageDestroy($myImage);

?>

 

with any browsers, I only get back ÿØÿà some unreadable characters.

Is it because content type didn't get change to image/jpeg?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/181741-getting-image-to-show-from-a-php-script/
Share on other sites

Sorry, missed that part of the code :) It was fine where it was, as far as why it is outputting the binary and not the actual image....hmmm not sure, looking at imagejpeg what you are doing seems correct.

 

Maybe try to output buffer the contents instead?

 

<?php
$myImage=ImageCreate(150,150);

$black=ImageColorAllocate($myImage, 0, 0, 0);
$white=ImageColorAllocate($myImage, 255, 255, 255);
$red=ImageColorAllocate($myImage, 255, 0, 0);
$green=ImageColorAllocate($myImage, 0, 255, 0);
$blue=ImageColorAllocate($myImage, 0, 0, 255);

ImageRectangle($myImage, 15, 15, 55, 85, $red);
ImageRectangle($myImage, 55, 85, 125, 135, $white);

header ("Content-type: image/jpeg");

ob_start();
ImageJpeg($myImage);
$image = ob_get_contents();
ob_end_clean();

echo $image;
ImageDestroy($myImage);
?>

 

I highly doubt that it will work, but worth a shot...

You likely have a header error due to how the .php file is saved or something before the <?php tag.

 

Add the following two lines of code immediately after the first opening <?php tag and temporarily comment out the header() statement -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.