getting image with php and displaying in html - php

i know it is so simple problem but eventually it isn't working and i am a newbie. in the index.html, a swf sends an image and displayImage.php(below code) should display it on another page. why isn't it working??
<?php
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$no=0;
while (file_exists("images/$no.jpg"))
$no++;
header('Content-Type: image/jpeg');
$image = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents("images/".$no.".jpg", $image);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml2/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-9" />
<title>Your Image</title>
<link href= "style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="logo"></div>
<div id="body"></div>
/////////display image//////////
<img src="images/<?$no.".jpg?>">
</body>
</html>

You don't echo the filename (and you have a quoting error, but this could be a typo):
<img src="images/<?php echo $no ?>.jpg" />
Assuming that storing the file actually works.

<img src="images/<?=$no.".jpg"?>">

This might be the issue. The line below the "display image" comment, should probably read:
<img src="images/<?= $no ?>.jpg">

Could be that you're using XHTML strict. IMG tags can't be unclosed like in normal HTML. You have to end it with a /> instead. (it's supposed to fail catastrophically when you make an error but it only does that when you send the right MIME type header to treat it as XML, so treated as text you may get unpredictable results.....). If it's printing the name of the file (like images/1.jpg) then you know that it's parsing the inline PHP correctly....

i deleted the line "header('Content-Type: image/jpeg');" and it works!

Related

How to print variable which contains HTML

One field of my mysql table contains body of an email received, now when I fetch this value in variable of PHP and just try to echo it just shows actual code.
How could i make to display as HTML-:
HTML stored in mysql-:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table width="200" border="1">
<tr>
<td><img src="http://myntra.myntassets.com/assets/banners/2014/3/12/1394610923636-footwear_micro-banner_160_120_mini.jpg" width="700" height="154" /></td>
</tr>
</table>
</body>
</html>
PHP-:
<?php
///......get value from DB and assign it to variable $body.
$body="$content";
echo $body;
?>
When you insert the data into your database I bet you are using html_entities over it.
Which is quite a good thing actually.
To get your html back, try using html_entity_decode() on the content you need to serve as html.
Beware of XSS attacks though.
I am guessing that you want to display the HTML content of your email.htmlentities should work. Try this-
echo htmlentities($body);
try to do this
$body="'".$content."'";
echo $body;
cause when you add single quotation to the html code it's executed before printed
or you have to add \ before every "
for example
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
i tried the both ways and they worked fine

Whats the mistake here?

It is probably a silly mistake but i'm into splits because of it :(
Language : PHP 5.4.7 \n Framework: CodeIgniter 2.1.3 , SDK: Facebook PHP SDK 3.2.2
Please consider the following Controller function:-
public function index()
{
// $this->__construct();
$data['profile'] = $this->_facebook->api('/me?fields=id');
$this->load->view('user_profile',$data);
}
And the corresponding view (user_profile.php) :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Showing the user profile</title>
</head>
<body>
<?php
//echo print_r($user_profile,TRUE);
echo '<IMG SRC="http://graph.facebook.com/'. $profile['id'] . '/picture" HEIGHT=32 WIDTH=32 /> ';
?>
</body>
</html>
The above code shows the profile picture in Internet Explorer but in Chrome the page is blank. On viewing page source, it is blank.
I would start with writing cleaner html.
<img src="http://graph.facebook.com/<?php echo $profile['id']; ?>/picture" alt="Profile Photo" height="32" width="32" />
Don't capitalize tags and img requires an alt attribute. I always quote attribute values, even when they're numbers.

How to properly include a header file in a webpage

So I have a header file:
<html>
<head>
<link href="/design_header.css" rel="stylesheet" type="text/css" />
</head>
<body>
content
</body>
</html>
And I want to place this header in my file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
The problem is now I have regular <html>, <head>, <body> tags inside the <body> tag of my webpage. In the html validator I receive errors like
Stray start tag html.
Stray end tag head.
An body start tag seen but an element of th
e same type was already open.
How to do it properly? Btw. I also place a footer to the bottom of the webpage the same way.
Your header file should only contain the HTML text you want for the header.
As it will be inserted into another webpage, it should not be a full HTML document.
Having only HTML for Header in Header
One option is to instead include in your header file only the HTML that is for the header (used by all pages that include it). However this has the downside that your not following the recommendation to have CSS loading before is rendered.
See: https://stackoverflow.com/a/1642259/1688441
Header File
<link href="/design_header.css" rel="stylesheet" type="text/css" />
content
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
Having only HTML for Header in HeaderFile and Separate HeadFile
You could have have a separate global_head_include.html (or txt, php, etc) file and put your CSS code there.
Header File (for include)
Header content
File with CSS includes (for include)
<link href="/design_header.css" rel="stylesheet" type="text/css" />
Whatever else is global...
Other files
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
meta tags etc.
<?php include 'global_head_include.php'; ?>
</head>
<body>
<div id="container_big">
<?php include 'header_login.php'; ?>
content
</div>
</body>
</html>
This is how I set out my headers and footers in my current PHP site:
header.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>James Wright - Young Software Developer</title>
<link rel="stylesheet" href="style.css" />
<meta name="description" content="The online home of a young web designer and software developer." />
<link rel="icon" type="image/png" href="/img/ico.png" />
</head>
<body>
<div id="holder">
<div id="menu"></div>
<div id="mainContent">
footer.php:
</div>
<div id="mainReflect"></div>
<p class="footer">© James Wright <?php echo date("Y"); ?>. Minimum resolution: 1024x768 <a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" style="vertical-align: middle;"/></a>
<a href='http://www.powermapper.com/products/sortsite/'><img src='http://www.powermapper.com/images/badge-v1/sortsite-badge-small-5.png' width="80" height="15" alt='Broken link checker and accessibility checker top 5% - SortSite' style='vertical-align: middle;'/></a>
</p>
</div>
</body>
</html>
Then whenever I create a new page, all I need to do is this:
<?php include("header.php"); ?>
<p>Main body content!</p>
<?php include("footer.php"); ?>

pChart does not render to web browser, only shows missing image

pCharts documentation says that you should be able to render the image to the browser using this code.
mypic.php
$myPicture->stroke;
mypage.html
<IMG SRC=‘mypic.php‘>
The img tag is supposed to invoke the php script. Within the PHP script, the stroke function sets the content-type: image/png.
So here is what I have:
netsales.php
<?php
include('../class/pData.class.php');
include('../class/pDraw.class.php');
include('../class/pImage.class.php');
/* query sales and create new image */
$myPicture->stroke;
?>
index.php
<?php
include ('netsales.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>
<img src="netsales.php" />
</div>
</body>
</html>
I'm not getting any errors, just the red X for a missing image.
try removing the
<?php
include ('netsales.php');
?>
From index.php, and add
header('Content-Type: image/png');
Before the Stroke call.
I try something like this and works:
//html file//////////////////////
<?php include ('my.php'); ?>
<html>
<head></head>
<body>
<div> <img src="my.php" /> </div>
</body>
</html>
///////////////////////////////////
//my.php file/////////////////////////
<?php
/* pChart library inclusions */
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pImage.class.php");
//...
$myPicture->autoOutput("picture.png");
?>
///////////////////////////////////
I just got it working for me I had comment include function in HTML file:
<?php //include ('my.php'); ?>

Creating custom "html"-tags for CMS?

I am working with a CMS for a web app in PHP, that has the needs of shortening the process for inserting (embedding) stuff, like a video from youtube or vimeo by wroting the following, which are stored in the database:
<youtube id="wfI0Z6YJhL0" />
Which would output the following after some sort of replace:
<!-- Custom formatting before object !-->
<object width="640" height="385"><param name="movie" value="http://www.youtube-nocookie.com/v/wfI0Z6YJhL0&hl=sv_SE&fs=1?rel=0&color1=0xe1600f&color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/wfI0Z6YJhL0&hl=sv_SE&fs=1?rel=0&color1=0xe1600f&color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
<!-- Custom formatting after object !-->
How could I do this in PHP?
I've written a class that does exactly what you ask for my own cms. I've uploaded the src for you as although I've never released it the source is released under a BSD style license. Custom Tags
It basically allows you do do exactly what you ask for. In the class there are some example custom tags so I won't paste code here. Let me know how you go.
Edit 1: Example Code as requested. :-)
Edit 2: I should add it supports buried custom tags.
Edit 3: It also supports inline templating and tag substitution, ie
<ct:inline some="attribute">
This is an in line template. <br />
This is a #{tag} that can be accessed by the callback function
</ct:inline>
PHP/HTML: example.php
<?php
$current_dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
require_once dirname($current_dir).DIRECTORY_SEPARATOR.'customtags.php';
$ct = new CustomTags(array(
'parse_on_shutdown' => true,
'tag_directory' => $current_dir.'tags'.DIRECTORY_SEPARATOR,
'sniff_for_buried_tags' => true
));
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Oliver Lillie">
<!-- Date: 2010-07-10 -->
</head>
<body>
<ct:youtube id="wfI0Z6YJhL0" />
</body>
</html>
Custom Tag PHP Function: tags/youtube/tag.php:
function ct_youtube($tag)
{
return '<object id="'.$tag['attributes']->id.'" value="http://www.youtube.com/v/'.$tag['attributes']->id.'" /><param ......>';
}
Output:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Oliver Lillie">
<!-- Date: 2010-07-10 -->
</head>
<body>
<object id="wfI0Z6YJhL0" value="http://www.youtube.com/v/wfI0Z6YJhL0" /><param ......>
</body>
</html>
I'm not 100% sure how it will react to non-standard tags, but if it works, simpleHTMLDom will be perfect for this.
$html = str_get_html('....');
then something along the lines of ...
$element = $html->find('youtube',0 ); // Finds first element
// - use "foreach" loop for final version
$element->tag = 'object';
$element->value = "http://www.youtube.com/v/".$element->id;
$element->innertext= "<param ......>"
....
echo $html;
you get the drift.
The beauty of this approach would be that every specific extension could add its data in clean HTML notation <tagname attribute="value">, with even the possibility of adding sub-tags for structured info, instead of kludgy {placeholder}s and regexes and so on.
I have never tried this and I don't have the time to test this right now, but if you decide to give it a try, I'd be interested to know whether this way turned out to be useful.

Categories