Solved

Unable to get PHP code to repeat

Posted on 2014-03-02
6
516 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
[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
  • 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 35

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
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!

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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.

738 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