?
Solved

having trouble displaying ph generated image

Posted on 2011-09-06
20
Medium Priority
?
301 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:paries
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
  • +1
20 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36490115
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36490123
Oops, never mind.  I see you are consistent about JPEG formatting.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36490131
What is this going to be used for?  Can you describe the application needs in non-technical terms?  Thanks, ~Ray
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36490139
what does the image look like when you just save it normally and don't do the imagedestroy on it?
0
 

Author Comment

by:paries
ID: 36490145
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
 

Author Comment

by:paries
ID: 36490158
if i do
print "<img src = 'generateTicket.php?\$text=foo'>";
and in generateTicket.php commented out imagedestroy no difference
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36490162
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
 

Author Comment

by:paries
ID: 36490198
haloexpertsexchange
if i call this page directly
http://mydomain/inc/utils/generateTicket.php
i get an image displayed in the browser
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36490215
sorry i was typing out my response when you yours went up so I didn't see that until afterwards.
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36490221
are you triggering any errors? If have error logging turned out I would check those out.
0
 

Author Comment

by:paries
ID: 36490224
no errors
0
 

Author Comment

by:paries
ID: 36490256
FYI, if i open up the popup in an IFRAME it displays ok
I guess that is kind of a workaround for now
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36490419
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
 
LVL 82

Expert Comment

by:hielo
ID: 36490801
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
 

Author Comment

by:paries
ID: 36491010
it is finding the file, because i have debugs to a logfile
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36491072
Is the image generated for the AJAX script on error: or on success: ?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36491079
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
 
LVL 82

Expert Comment

by:hielo
ID: 36491171
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
 

Accepted Solution

by:
paries earned 0 total points
ID: 36498400
ended up using the iframe and moving on
0
 

Author Closing Comment

by:paries
ID: 36899019
did not get an answer that worked
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 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