having trouble displaying ph generated image

hello,
if i call this page directly
http://mydomain/inc/utils/generateTicket.php
i get an image displayed in the browser

now i am trying to display this image with a jquery/ajax call.

if i call printTicket and the URL is generateTicket.php
the display is a bunch of unprintable characters

so then i tried
url: "/inc/utils/eticket.php", which is basically ::
<?php
    print "<img src = 'generateTicket.php'>";
?>
this displayed nothing

then i tried
<?php
    require_once 'generateTicket.php';
?>
and got the garbage again (see image )

thanks for any help





<?php

// PHP File used to quickly put some text over a poster image - for video or other.

// To use:  Use this file as the source file of an image.  Pass it two GET variables:  pat=path of image&text=text to overlay

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

// Open the image:

$image = imagecreatefromjpeg('/home/events/html/templates/ticket.jpg');

$width = imagesx($image); // Gets the width - better to use imagesx here as we don't need to feed it a path, just a handle.

$rectCol = imagecolorallocatealpha($image, 0, 0, 0, 100); // Create a black colour, slightly transparent
$textCol = imagecolorallocate($image, 255, 255, 255); // Create a white colour

// Fill the top of the image with this rectangle...

//imagefilledrectangle($image, 350, 10, 730, 150, $rectCol);

//imagefilledrectangle($image, 0, 0, $width, 50, $rectCol);

//Title
$textX = 350;
$textY = 50; // X and Y values for the text generation

$font = '/home/events/html/templates/verdanab.ttf'; // Replace this with the path to your font file.

// Write Text:
imagettftext($image, 18, 0, $textX, $textY, $textCol, $font, 'test');

// Now we're done editing... output the final image:

imagejpeg($image);

// Free up some space:

imagedestroy($image);


?>

Open in new window

function printTicket(e, f){
   $('#shadowBox').height($('#mainBody').height()).fadeIn('fast',
     function(){
       $.ajax({
          url: "/inc/utils/eticket.php",
          type: 'GET',
          data:'e='+e,
          beforeSend: function(){
            $('#popup').animate({width: "750px"}, 330);
          },
          error: function(jqXHR, textStatus, errorThrown){
            $('#popupContent').html(
              '<div class="error">'
                +'<div class="errorLine">'+textStatus+': '+errorThrown+'</div>'
              +'</div>'
            ).fadeIn(250);
          },
          success: function(data){
             $('#popupContent').html(data).fadeIn(250);
          }
       });
    });
}

Open in new window

Capture2a.PNG
pariesAsked:
Who is Participating?
 
pariesAuthor Commented:
ended up using the iframe and moving on
0
 
Ray PaseurCommented:
I think the stuff you see in the Capture2a.PNG is a JPG image, not a PNG image.  So the first thing I might try is (1) generate a PNG image or (2) use a JPG header.
0
 
Ray PaseurCommented:
Oops, never mind.  I see you are consistent about JPEG formatting.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Ray PaseurCommented:
What is this going to be used for?  Can you describe the application needs in non-technical terms?  Thanks, ~Ray
0
 
haloexpertsexchangeCommented:
what does the image look like when you just save it normally and don't do the imagedestroy on it?
0
 
pariesAuthor Commented:
When the user clicks on a link, i open up a popup and display an image that is a ticket.
the php that is displayed in the popup will build the image based on DB Info
Not really sure what you are asking
0
 
pariesAuthor Commented:
if i do
print "<img src = 'generateTicket.php?\$text=foo'>";
and in generateTicket.php commented out imagedestroy no difference
0
 
haloexpertsexchangeCommented:
can you save that image to the server somewhere and then look at image itself instead of doing the ajax call. I want to make sure that it is not the ajax display of the imagejpeg  that is messing things up instead of the image creation being the problem.
0
 
pariesAuthor Commented:
haloexpertsexchange
if i call this page directly
http://mydomain/inc/utils/generateTicket.php
i get an image displayed in the browser
0
 
haloexpertsexchangeCommented:
sorry i was typing out my response when you yours went up so I didn't see that until afterwards.
0
 
haloexpertsexchangeCommented:
are you triggering any errors? If have error logging turned out I would check those out.
0
 
pariesAuthor Commented:
no errors
0
 
pariesAuthor Commented:
FYI, if i open up the popup in an IFRAME it displays ok
I guess that is kind of a workaround for now
0
 
Ray PaseurCommented:
Lines 35-39 of the original post.

imagejpeg($image);

// Free up some space:

imagedestroy($image);


Here is what I might try instead.  

1. Use something like md5(time()) to generate a pseudo-random name with a minimal risk of name collision.
   Assign a variable like this: $random_name = md5(time()) . 'jpg';
2. Save the file with imagejpeg($image, $random_name);
3. Return the $random_name string to the AJAX request, so it can be injected into an <img> tag.

I am not certain that will work, but it might help you get rid of the iframe.
0
 
hieloCommented:
so then i tried
url: "/inc/utils/eticket.php", which is basically ::
<?php
    print "<img src = 'generateTicket.php'>";
?>
this displayed nothing

Try it with:
 print "<img src='/inc/utils/generateTicket.php' />";
0
 
pariesAuthor Commented:
it is finding the file, because i have debugs to a logfile
0
 
Ray PaseurCommented:
Is the image generated for the AJAX script on error: or on success: ?
0
 
Ray PaseurCommented:
Sidebar note, if you generate the image into a file instead of into the browser output stream you would not need the header() shown on line 7 of the PHP script.
0
 
hieloCommented:
OK, then have you verified that:
'#popupContent'

actually exists by the time the ajax request finishes AND that it is visible?

What does this show?

success: function(data){alert(data);
             $('#popupContent').html(data).fadeIn(250);
          }

Open in new window

0
 
pariesAuthor Commented:
did not get an answer that worked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.