Solved

having trouble displaying ph generated image

Posted on 2011-09-06
20
289 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
  • 8
  • 6
  • 4
  • +1
20 Comments
 
LVL 108

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 108

Expert Comment

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

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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 108

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 108

Expert Comment

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now