How to create and display image in PHP - php

I would like to display an image using PHP. Here is what I have tried, but it does not work.
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<div class="container">
<?php
$img_name = echo $row['img_name']; //I get it from the database
img_block($img_name); //Display the image here.
//Function to display image
function img_block(img_src) {
// e.g. img_src = cat.jpg;
$img_input = "images/" . img_src;
$set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
return $set_img;
}
?>
</div>
</body>
</html>
Thank you in advance.

Too long for a comment... You have a number of errors:
$img_name = echo $row['img_name'];
should be:
$img_name = $row['img_name'];
You are calling your function but not doing anything with the return value, you need to echo it:
img_block($img_name);
should be:
echo img_block($img_name);
Finally you have not put the required $ on the img_src variable in your function; it's definition should be:
function img_block($img_src) {
// e.g. img_src = cat.jpg;
$img_input = "images/" . $img_src;
$set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
return $set_img;
}
If you make all these changes, and (e.g.) $row['img_name'] = 'image1.jpg', your code will output:
<img class="media-object-ph" src="images/image1.jpg" width="380" height="290" alt="...">
Demo on 3v4l.org

Related

How to get image URL to display in PHP

Can't get image URL to display in my code; this is my URL: https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!DOCTYPE html>
<html>
<head>
<title>PHP Exercise 1: Links and Variables</title>
</head>
<body>
<h1>PHP Exercise 1: Links and Variables</h1>
<p>Use PHP echo and variables to output the
following link information:</p>
<hr>
<?php
$linkName ='<h1> Codecademy <h1>';
$linkURL = 'codecademy';
$linkImage =
'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
$linkDescription = 'Learn to code interactively, for free.';
$my_name = "peter";
echo "<img>" . $linkImage . "</img>";
echo $linkName;
echo "<br>";
echo $linkURL;
echo "<br>";
echo $linkImage;
echo "<br>";
echo $linkDescription;
$linkImage = 'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
echo '<img src="data:image/jpeg;base64,">';
?>
</body>
</html>
A link to an image should always be put in the src attribute when you're using the img tag in html.
That means you should be looking for something like "<img src=".$linkImage."></img>" instead of "<img>".$linkImage."</img>"
Of course, that only applies if the problem you're referring to is the fact that there should be an image after the sentence "Learn to code interactively, for free."
Works just fine for me. Try checking php permissions.

display image in my page using echo [duplicate]

This question already has an answer here:
upload image to mysql database php
(1 answer)
Closed 9 years ago.
this is code of the page that I will get the image from(work perfectly)
<?php
ob_start();
session_start();
include('connect.php');
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM news WHERE id=$id");
$row = mysql_fetch_assoc($query);
header("Content-type: image/jpeg");
echo $row['image'];
?>
and this is my page that i get the image to in
<?php
ob_start();
session_start();
include('includes/connect.php');
include('includes/phpCodes.php');
$id = $_GET['id'];
function showNews() {
$data = array( 'id' => $id );
$base = "includes/getImage.php";
$url = $base. "?" . "id=36";
echo $url;
echo '<img src=includes/getImage.php class="newsImage">';
echo '<h1><p class="subjecTitle">هنا العنوان</p></h1>
<div class="newsContent">
hihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihi
</div>
';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>عينٌ على الحقيقة</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/mainstyle.css">
<link rel="stylesheet" type="text/css" href="css/showstyle.css">
<script lang="javascript">
function logout( myFrame ) {
myFram.submit();
}
</script>
</head>
<body>
<div class="wrapper">
<?php headerCode(); ?>
<div class="content" dir="rtl">
<?php showNews(); ?>
</div>
</div>
</body>
</html>
i think my wrong is in , can someone tell me how can I solve it?, sorry for my bad english
Cleared it up for you:
echo '<img src="includes/getImage.php?id=' . $id . '" class="newsImage">';
Should 100% work (if the $id param has a value of course).
Update to fix the missing $id var:
<?php
ob_start();
session_start();
include('includes/connect.php');
include('includes/phpCodes.php');
$id = $_GET['id'];
function showNews(){
$id = $_GET['id'];
$base = "includes/getImage.php";
$url = $base. "?" . "id=36";
echo $url;
echo '<img src="includes/getImage.php?id=' . $id . '" class="newsImage">';
echo '
<h1><p class="subjecTitle">??? ???????</p></h1>
<div class="newsContent"></div>
';
}
?>
why are you using two different pages?
put both code in single page and simply do this
<img src=includes/<?php echo $row['image']; ?> class="newsImage">
Change :
echo ' <img src=includes/getImage.php class="newsImage">';
To :
echo ' <img src="includes/getImage.php?id='.$id.'" class="newsImage">';
Please note src has " and also make sure that includes/getImage.php return a image path

Try to show image but shows a broken link instead

I want to show a picture in my code but for some reason I get a big white almost blank page with a broken link img. I want to show the image and along with any other information.
I also want to resize the image(so it doesnt cover the whole page).
The code:
<?php
session_start()
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0" />
<link rel="stylesheet" href="css/style.css" />
</head>
<div id="Container">
<div id="header">
<h1>Welcome!</h1>
</div>
<div id="navigation">
<?php
include_once "navigation.php";
?>
</div>
<div id="Content">
<?php
if (isset($_GET['search'])) {
$Name = $_GET['search'];
mysql_connect("localhost","root","") or die ("Could not connect to the server!");
mysql_select_db("pictures") or die ("That database could not be found!");
$query = mysql_query("SELECT * FROM picture WHERE Name='$Name'") or die ("The query could not be completed, please try again later!");
if (mysql_num_rows($wepquery) !=1) {
die ("That name could not be found!");
}
while ($row = mysql_fetch_array($query, MYSQL_ASSOC, 0)) {
$dbName = $row['Name'];
$dbCreator = $row['Creator'];
$dbDescription = $row['Description'];
$imageData = $row['Image'];
}
header("Content-type: image/jpeg");
if($Name != $dbName) {
die ("There has been a fatal error. Please try again.");
}
?>
<h2><?php echo $Name; ?></h2>
<br />
<table>
<tr><td>Creator: <?php echo $dbCreator;?></td></tr>
<tr><td>Description:<br /><?php echo $dbDescription;?></td></tr>
<tr><td>Picture:<br /><?php echo $imageData;?></td></tr>
</table>
<?php
} else die ("You need to specify a submission!");
?>
</div>
</div>
</html>
The database:
id, Name, Creator, Description, Image_name, Image(mediumblob).
The last two fields as you can see is dedicated to pictures. Yes I know about PDO and MySQLi, but I just want to finish this code first. Any help?
Images need to either be served by URL or the blob needs to be converted to a data URI like so:
<table>
<tr><td>Creator: <?php echo $dbCreator;?></td></tr>
<tr><td>Description:<br /><?php echo $dbDescription;?></td></tr>
<tr><td>Picture:<br /><?php echo "<img src='data:image/jpeg;base64," . base64_encode( $imageData ) . "' />"; ?></td></tr>
</table>
if you want image you need to enclose it with image tags. assume if your $imageData contains the location of the image then you can do the following.
<img src='<?php echo $imageData;?>' ... />
if your $imageData contains actual binary data of an image you need to create a separate call to a php script to return the imageData. and make the call to be the src attribute of an img tag.
is your path exactly pointing toward the image? have checked "../../" ? another question, is your image really inside an image tag? there must be something like this:
<img src="<?php echo $imagePath; ? />

Check if file exist before print

Can someone help me connect some code??
What I want is a code that does this: If a picture exist then print it, else don't print anything.
(This code is used to find the pichure)
and I need help to conect the if statment to the one in the other code
<?php
$pathToFileOnDisk = 'inventory_images/' . $id . '.jpg';
if(file_exists($pathToFileOnDisk) && is_readable($pathToFileOnDisk)) {
<img src="inventory_images/' . $id . '.jpg" alt="' . $name . '" width="100%" height="260" border="1" />
}
else {
// NO IMG
}
?>
AND
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$dynamicList = "";
$sql = mysql_query("SELECT * FROM content ORDER BY id DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$name = $row["name"];
$content = $row["content"];
$date_added = strftime("%d %b, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div id="left">
<img src="inventory_images/' . $id . '.jpg" alt="' . $name . '" width="100%" height="260" border="1" /> <-- !!THIS IS WHAT I WANNA REPLACE!!
</div> <!-- End Left-->
</td>
<div id="right">
<td width="630px" valign="top"><h2><a id="overskrift" href="product.php?id=' . $id . '">' . $name . '</a></h2>
<p><b>
Skrevet den ' . $date_added . '<br />
' . $content . '</b></p>
</td>
</div>
</tr>
</table><br />';
}
} else {
$dynamicList = "Ingen inlegg enda..";
}
mysql_close();
?>
<!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" />
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper">
<header>
<?php include_once("template_header.php");?>
</header> <!-- End Header -->
<div id="banner"></div>
<div class="content test clearfix">
<?php echo $dynamicList; ?>
</div> <!-- End Content -->
<footer>
<?php include_once("template_footer.php");?>
</footer> <!-- End Footer -->
</div> <!-- End Wrapper -->
</body>
Use the file_exists() and is_readable() functions.
The first will tell if the file exists, and the second will tell if you have permissions to read the file, before actually doing it. Similarly, there's also the is_writable() function, which tells you if you have permissions to write to a file.
Simply use this:-
<?php
$filename = 'some_name';
if (file_exists($filename)) {
echo "File exists";
} else {
echo "File does not exist";
}
?>
The file_exists and is_readable functions are perfect for this purpose.
For example:
<?php
$pathToFileOnDisk = '/full/system/path/to/image.jpg';
if(file_exists($pathToFileOnDisk) && is_readable($pathToFileOnDisk)) {
// The file exists and can be read
}
else {
// The file doesn't exist (or does exist, but PHP can't read it).
// Perhaps output a placeholder image here.
}
?>
Incidentally, I'd recommend a quick glance at all of the filesystem functions, as the time spent on this now will pay dividends in the future.

Remove a slash after ?img=

I have this code
<html>
<body background="http://newevolutiondesigns.com/images/freebies/abstract-background-22.jpg">
<br><br><br><br><br>
<DIV align="center">
<?php
$img = $_GET["img"];
?>
<?php
if($img=="") { echo ""; } else { echo "<img src='" . $img . "' />"; } ?>
<br>
</body></html>
I'm using this for screensnapr so when i'm trying to see the picture with http://imagesnappper.co.cc/Images?img= it autoamtically adds a / after ?img=
so it's like http://imagesnappper.co.cc/Images?img=/8kd6lx.jpg and it won't show the image how to delete that slash?
Try using...
$img = ltrim($img, "/");
There are a number of ways, but the simplest and nastiest that comes to mind is substr():
$img=substr($_GET[img], 1);

Categories