Solved

Unable to get PHP code to repeat

Posted on 2014-03-02
6
493 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 41
Wordpress Query 1 35
Should you use {} for a variable in heredoc 3 27
PHP JSON Clean up 5 10
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now