Solved

Unable to get PHP code to repeat

Posted on 2014-03-02
6
520 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

632 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