PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Sun Jun 16, 2019 3:39 pm

All times are UTC - 5 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: imagepng behaviour
PostPosted: Sat Dec 19, 2009 7:05 am 
Forum Newbie

Joined: Fri Oct 30, 2009 10:57 am
Posts: 10
Location: Wiltshire, UK
I've been struggling with overlaying a graphic template with some text and saving the result as a file which, later, will used as an attachment to an email. After creating the overlaid file I need to redirect to another page.
While developing the code I was displaying the template graphic (Line 18 of the code) in which case the redirection failed. But by, commenting out that line, redirection worked.

Can you explain that behaviour?


Syntax: [ Download ] [ Hide ]
<?php session_start();
   header('Content-Type: image/png');
   $serial  = "ABC123X";                    // normally passed as a session variable
   $imgname = 'blankorder.png';
   $img = @imagecreatefrompng($imgname);    // open image file
   if(!$img)                                // failed to open
      $img = imagecreatetruecolor(300, 30);
      $bak = imagecolorallocate($img, 255, 255, 255);
      $txt = imagecolorallocate($img, 255, 0, 0);
      imagefilledrectangle($img, 0, 0, 300, 30, $bak);
      imagestring($img, 5, 0, 0, 'Error loading ' . $imgname, $txt); //error message
//  imagepng($img);  // Puts blank form on screen but, if implemented, the redirection doesn't work
    $textcolour = imagecolorallocate($img, 0, 0, 0);
//  Font converted from ttf using
    $font = imageloadfont('Arial24.gdf');
    imagestring($img, $font, 535, 312, $serial, $textcolour);   // insert serial no. in required position
    imagepng($img,'overlaid.png');                              // save overlaid image as a file
//  header('Content-Type: text/html; charset=iso-8859-1');      // appears to be redundant
    header('refresh: 0; url=stamped.html');                     // redirect to specified page

 Post subject: Re: imagepng behaviour
PostPosted: Mon Jun 28, 2010 3:28 pm 
Forum Regular
User avatar

Joined: Mon Feb 11, 2008 5:22 am
Posts: 613
In a nutshell, don't send anything before your headers.

When you uncomment the relevant line you are sending the PNG image first, headers second - that won't work. If you need to display the form before redirecting then it would be better to implement the redirect with JS.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group