Jump to content

Returning Data From Php Back To Ajax Without Using Echo


thecommonthread

Recommended Posts

So traditionally you use "echo json_encode(array of data to return)" to return data to AJAX, but the problem is my PHP file is a PNG image -

 

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

imagepng($image);

 

 

So I can't echo anything or else the image based from a PHP file will just fail. Is there an alternative way to return data to AJAX without echoing anything? I sure appreciate any help anyone could give me. Thanks!

Link to comment
Share on other sites

Lol, thanks for the reply. So you're saying I can use an echo in an image then and I'm missing something? Let's just use the example from the PHP Manual (php.net/imagettftext) to create a very simple image with only a text phrase in it:

 

<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

 

If the $font you are loading in this code exists on the server, this will just load a PNG image with the word "Testing..." Now try using a simple echo statement. Echo anything. Now try and run the PHP and it will just load as a broken image. This is why I'm looking for an alternative to echoing a json_encode(). Thanks.

Link to comment
Share on other sites

No, what she's saying is that there is no way to mix image-data and non-image data. Your PHP code outputs into one format, and one format only, at a time.

What you'll need to do is to shell out the image generation into a script of its own, then set that script as the source of the image element you want to show it in. The JSON bit, and anything else, needs to come from a separate file. Normally you'll call that file with the AJAX.

 

If you want to generate an image based upon the AJAX call, then you will still need to to this in two operations: First the AJAX call, to get the data you need (including the image name). Then set the image src to the image-generation script, using the data you've just retrieved from the AJAX call.

Link to comment
Share on other sites

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.