Solved

Unable to get PHP code to repeat

Posted on 2014-03-02
6
509 Views
Last Modified: 2014-03-02
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
Comment
Question by:rwinnick
  • 3
  • 2
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39899164
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
 

Author Comment

by:rwinnick
ID: 39899271
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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39899276
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39899280
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
 

Author Comment

by:rwinnick
ID: 39899291
Hi Cathal -
I will try to break the code up as you suggest -
May take a minute or two :-)
R
0
 

Author Comment

by:rwinnick
ID: 39899569
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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