Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

Unable to get PHP code to repeat

I have a block of code that generates a qr code and combines it with another image and some text to create a png image that is output to the browser where it can be viewed and printed if desired. The code works for a single image. However, when I try to have it repeat to create multiple images on the same page it fails with the following error:
Warning: Cannot modify header information - headers already sent by (output started at xxx.php:62) in xxx.php on line 61

The formatting of the page appears correct but rather than the image there is a bunch of code symbols -

I am guessing that it is not being recognized as an image for some reason -
probably associated with the header image/png not be sent properly -

I have attempted to put the header info in various locations with no success -

Not sure what I am missing :-(

I have annotated the code below to indicate the portion that works properly for a single image -

All help appreciated -

- - - - - - - - - - -
<?php
session_start();

// can successfully connect to the database. 

//-- can successfully do query which returns multiple results --
      $query = "Select unit_id, unit_name From units where company_id=" . $cid . ";";
      $result = mysql_query($query);

 // -- set up url for qr code --
$add="http://www.my_site.com/form.php?x_unit_id=";      
$add2="&s_company_id=" . $cid;

//-- set counter for page table formatting --
$count=0;

//-- start table format for page – three images across then next line of three --
 print "<table><TR>";

 //----- the following code works to create single image --- fails when try to repeat for multiple images ----
  while($url = mysql_fetch_array($result)){

//-- create qr code using google api --
  	$qrurl = 'https://chart.googleapis.com/chart?'
  	    . 'cht=qr'
  	    . '&'
  	    . 'chs=300x300'
  	    . '&'
  	    . 'chl='
  	    . urlencode($add . $url{0} . $add2);
  	    ;

//--  the following combines the qr code image with second image and centers text  ---
$h = $qrurl;
$b = 'qr_body_150_75.png';
$headResource = imagecreatefrompng($h);
$bodyResource = imagecreatefrompng($b);
 $headWidth = 150;
$headHeight =150; 
$bodyWidth =150;
$bodyHeight= 75;
$previewHeight = 250;
$previewWidth  = 150;
$previewResource = imagecreatetruecolor($previewWidth, $previewHeight);

//make background white
$white = imagecolorallocate($previewResource, 255, 255, 255);
imagefill($previewResource, 0, 0, $white);

// Copy head image
imagecopyresized($previewResource,$headResource, 0,0,0,0,150,150,300,300);

//Add body image
imagecopy($previewResource,$bodyResource,0,$headHeight,0,0,$bodyWidth,$bodyHeight);
$color = imagecolorallocate($previewResource, 233, 14, 91);
$text = $url[1];
$font = 5;

//-- size and center text --
 while (strlen($text) * imagefontwidth($font) > 150) {
  if ($font > 1) { $font--; }
  else { break; }
 }
  imagestring($previewResource,$font,150 / 2 - strlen($text) * imagefontwidth($font) / 2, 230,$text,$color);
//-- end center text

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

imagepng($previewResource);

imagedestroy($headResource);			
imagedestroy($bodyResource);
imagedestroy($previewResource);

//--- works to here for single image -------
//-- fails when try for multiple images ---

//-- check if three across --
	++$count;
  	if ($count == 3) {
  		print "</TR><TR>";
  		$count = 0;
}
 }
 print "</tr></Table>";
 ?>

Open in new window

0
rwinnick
Asked:
rwinnick
  • 3
  • 2
1 Solution
 
GaryCommented:
If you are sending an image header then it expects an image and only one image and nothing else.

What you can do is create a normal HTML page with img tags and set the source of the image to your script above (minus the table html)
e.g.

<img src="my-qr-page.php?someparams=my-values" width="100" height="100">
<img src="my-qr-page.php?someparams=my-other-values" width="100" height="100">
0
 
rwinnickAuthor Commented:
Hi Cathal -
Thanks for the quick reply -
Are you suggesting that I put the img src within the while statement?
Could you show me a snippet of what the code would look like -
Thanks
R
0
 
Dan CraciunIT ConsultantCommented:
You could start by removing the line:
header('Content-type: image/png');

This is what causes the error.

After you remove it, refactor your code to get rid of the new errors and display your images as you want them.

HTH,
Dan
0
Industry Leaders: 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!

 
GaryCommented:
Kinda, you need to separate your QR image code from your HTML.
So take that QR image code and place it in a file by itself - you will need to amend the parameters it accepts to use a $_GET or $_POST as it will no longer be getting the values from the database.

Having removed the image code (from your sample above) continue with your loop but now you are just echoing an image and the source of that image will point to your imaging page.  But you will be adding a querystring to the path which includes the parameters from your db. i.e. $url{0} and $url{1} (your image code will call these parameters)

p.s.
Use the code tags for code in questions/comments - I amended your question to wrap it
0
 
rwinnickAuthor Commented:
Hi Cathal -
I will try to break the code up as you suggest -
May take a minute or two :-)
R
0
 
rwinnickAuthor Commented:
Thanks Cathal -
Was just the pointer I needed to get focused in the right direction -
Took a little trail and error - actually a lot of errors :-)
But now works fine and I learned quite a bit -
Thanks again -
Richard
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now