I'm trying to display an image in an html file. Up to now i used an url : http://placehold.it/400x300, it worked fine, now when i try to replace it with image from database it doesn't display anything. the image path is : D:/uwamp/www/project/upload/tcf_animal17.jpg i tried with different path but it doesn't work. i'm not sure where is the problem. i printed the data , this is the good path
html
<?php foreach ($data[0] as $film):?>
<div class = <?= $film['id_film'] ?> >
<div class= "col-lg-3 col-md-4 col-xs-6 thumb filmDiv" >
<a class="thumbnail " id = "filmShow" href= <?= "/project/admin/showFilm/" . $film['id_film']?>>
<p> <?= $film['title_film'] ?> </p>
<img class="img-responsive overlay" src= <?= $film['img'] ?> alt="">
</a>
<a class="" href =<?= "/project/admin/update/" . $film['id_film'] ?> >
<button name="upd" id="upd" type="button" class="btn btn-default">Update</button>
</a>
<a class="delete"name= <?= $film['id_film'] ?> >
<button id="delete" type="button" class="btn btn-default">Delete</button>
</a>
</div>
</div>
<?php endforeach; ?>
Controler :
public function GetLastFilms()
{
$films = $this->film->getFilms();
$lastFilms = $this->film ->getLastFilm();
$dict[0] = $films;
$dict[1] = $lastFilms;
return $dict;
}
public function index()
{
if(isset($_SESSION['login']))
{
$this->generateView($this->GetLastFilms());
}else
{
header('Location: /project/admin/login');
}
}
protected function generateView($data = array())
{
$classeControler = get_class($this);
$controler = str_replace("Controler", "", $classeControler);
$view = new View($this->action, $controler);
$view->generate($data);
}
View :
public function generate($data)
{
$contenu = $this->generateFile($this->viewFile, $data);
$racineWeb = Configuration::get("racineWeb", "/");
$view = $this->generateFile('View/Template/index.php',
array('title' => $this->viewTitle, 'contenu' => $contenu,
'racineWeb' => $racineWeb));
echo $view;
}
private function generateFile($viewFile, $data)
{
if (file_exists($viewFile))
{
extract($data);
ob_start();
require $viewFile;
return ob_get_clean();
}
else
{
throw new Exception("can't find '$viewFile'");
}
}
Your problem is that the path to the image in the database is stored as local path from your machine (D:/uwamp/www/project/upload/tcf_animal17.jpg), where for it to display correctly it needs to be a path relative to the root of your web server documents.
For example, if your web server document root is D:/uwamp/www, then the path of the image you need to store should be project/upload/tcf_animal17.jpg.
Of course if you can't get the image to be stored within the web root, then you always have an option to just dump the content directly, however it's really not a good idea, for performance reasons:
<?php
$mime = image_type_to_mime_type(exif_imagetype(string $image_path));
$data = "data:$mime;base64," . base64_encode(file_get_contents($image_path)); ?>
?>
<img src="<?= $data ?>">
This code is very crude and does no error checking - you'll need to add that for a production-level application.
Related
Good day everyone, so I have a code here for my site for uploading images to customer profile photos, but if they haven't uploaded yet it shows a broken image, how do I put a placeholder instead of a broken image.
<div class="panel-body">
<a data-target="#myModal" data-toggle="modal" href=
""><img class="img-hover" src="<?php echo web_root. "customer/".$res->CUSPHOTO; ?>"
style="width:100%; height:100%;text-align:center" title=
"profile image"></a>
</div>
This is my code for uploading the image
function doupdateimage(){
$errofile = $_FILES['photo']['error'];
$type = $_FILES['photo']['type'];
$temp = $_FILES['photo']['tmp_name'];
$myfile =$_FILES['photo']['name'];
$location="customer_image/".$myfile;
if ( $errofile > 0) {
message("No Image Selected!", "error");
redirect(web_root. "index.php?q=profile");
}else{
#$file=$_FILES['photo']['tmp_name'];
#$image= addslashes(file_get_contents($_FILES['photo']['tmp_name']));
#$image_name= addslashes($_FILES['photo']['name']);
#$image_size= getimagesize($_FILES['photo']['tmp_name']);
if ($image_size==FALSE ) {
message(web_root. "Uploaded file is not an image!", "error");
redirect(web_root. "index.php?q=profile");
}else{
//uploading the file
move_uploaded_file($temp,"customer_image/" . $myfile);
$customer = New Customer();
$customer->CUSPHOTO = $location;
$customer->update($_SESSION['CUSID']);
redirect(web_root. "index.php?q=profile");
Assuming that "basically has no value" means NULL you could check for that value:
<?php
if (is_null($res->CUSPHOTO)) {
$url = web_root. "customer/anonymous.jpg";
}
else {
$url = web_root. "customer/".$res->CUSPHOTO;
}
?>
<div class="panel-body">
<a data-target="#myModal" data-toggle="modal" href="">
<img class="img-hover"
src=<?php echo '"'.$url.'"' ?>
style="width:100%; height:100%;text-align:center"
title="profile image">
</a>
</div>
Here anonymous.jpg is the image to be displayed when no photo has been uploaded.
Opinion: Personally I don't like mixing the PHP tags into HTML like that. You never know exactly what will happen. I always write either HTML or PHP, not mixed, but I know it can be done. That's the reason I do the strange thing with the quotes around $url, I really don't like a PHP tag inside a HTML string.
I have two include files:
1.draw_images.php
<?
function practice_area_img($img_path)
{
$items = "";
$files = glob($img_path . "/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
$items .= <<<HTML
<div class="item">
<img src="$num" class="img-responsive" alt="">
</div>
HTML;
}
return $items;
}
?>
The above code snippet goes through a folder and draws all the images therein into stylized div tags.
Please note here that I get the div blog-slider drawn, but the images fail. The image path is of this kind:
/opt/lampp/htdocs/my_project/images/my_pic.jpg
2.definitions.php
<?php
$rev_args['images_path'] = dirname(__FILE__) . '/images';
?>
I call them as follows:
<?php
include(dirname(__FILE__) . '/definitions.php');
include(dirname(__FILE__) . '/draw_images.php');
<div id="blog-slider" class="owl-carousel owl-theme">
<?= practice_area_img($rev_args['images_path']); ?>
</div>
How can I go about getting the image drawn?
UPDATE
Please nothe that <?= $num; ?> prints a something like /opt/lampp/htdocs/my_project/images/my_pic.jpg
so:
<img src="$num" class="img-responsive" alt="">
is actually :
<img src="/opt/lampp/htdocs/my_project/images/my_pic.jpg" class="img-responsive" alt="">
behind the scenes.
Without seeing the source of the generated html, the only thing I can see, is that the image paths are probably wrong.
You display the image using:
<img src="$num" class="img-responsive" alt="">
However, you look in the images/ directory of the script path to get them, so instead of just echoing the filenames, you should probably use something like:
<img src="images/$num" class="img-responsive" alt="">
Edit: Based on your comment, you would need something like:
$num = basename($files[$i]);
...
<img src="images/$num" class="img-responsive" alt="">
It is a absolute path problem in your img tag
<?
function practice_area_img($img_path)
{
$items = "";
$files = glob($img_path . "/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
// remove absolute path
$relPath = str_replace(dirname(__FILE__)."/", "", $num);
$items .= <<<HTML
<div class="item">
<img src="$relPath" class="img-responsive" alt="">
</div>
HTML;
}
return $items;
}
?>
This will produce:
<img src="images/my_pic.jpg" class="img-responsive" alt="">
which it will search the image under your server you can put in front of it
http(s)://localhost (servername) if you want to.
I'm attempting to add social follow icons to a BuddyPress site using a section of code that I found here:
https://buddypress.org/support/topic/display-users-social-follow-buttons-in-profile/
I followed the suggestion downthread and added the code to a bp-custom.php file in my plugins directory and the icons are showing up where they should but the link to the social profile is showing as text and when I click on the link it returns a 404 page.
I'm sure I have something wrong but I'm just too clueless new to spot it.
<?php
//Social Media Icons based on the profile user info
function member_social_extend(){
$dmember_id = $bp->displayed_user->id;
$fb_info = xprofile_get_field_data('facebook', $dmember_id);
$google_info = xprofile_get_field_data('googleplus', $dmember_id);
$linkedin_info = xprofile_get_field_data('linkedin', $dmember_id);
$twitter_info = xprofile_get_field_data('twitter', $dmember_id);
echo '<div class="member-social">';
if($fb_info||$google_info||$linkedin_info||$twitter_info){
echo 'My Social: ';
}
if ($fb_info) {
?>
<span class="fb-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/facebook.png" /></span>
<?php
}
?>
<?php
if ($google_info) {
?>
<span class="google-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/googleplus.png" /></span>
<?php
}
?>
<?php
if ($linkedin_info) {
?>
<span class="linkedin-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/linkedin.png" /></span>
<?php
}
?>
<?php
if ($twitter_info) {
?>
<span class="twitter-info"><img src="<?php bloginfo('wpurl'); ?>/wp-content/themes/family-openstrap-child/images/twitter.png" /></span>
<?php
}
echo '</div>';
}
add_filter( 'bp_before_member_header_meta', 'member_social_extend' );
?>
Thanks!
Looks like xprofile_get_field_data() creates its own <a> tag, so you don't have to write one out like you've done there. Try this instead:
$fb_info = xprofile_get_field_data(
'<img src="' . bloginfo('wpurl') . '/wp-content/themes/family-openstrap-child/images/facebook.png" />',
$dmember_id
);
...
<span class="fb-info"><?php echo $fb_info; ?></span>
Not sure if xprofile_get_field_data() will let you pass HTML in the first parameter there, so if that doesn't work quite right, you could fall back to something simpler (no image) like:
$fb_info = xprofile_get_field_data('Facebook', $dmember_id);
I have a number of images in my db and they all correspond to the same project ID.
I want them to be displayed next to each other in the browser.
But the following code is output only the latest image but not all of them:
//get all the images in this project and add them to the content variable for output
if (!empty($FormProjectID)) {
$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");
if (mysqli_num_rows($DBQuery3) < 1) {
$content = '
<p>This project is empty. Upload some files to get started.</p>
';
} else {
while($row = mysqli_fetch_array($DBQuery3)) {
$DBImageID = $row['image_id'];
$DBProjectID = $row['project_id'];
$DBImageName = $row['image_name'];
$DBImageDescription = $row['image_description'];
$DBDateCreated = $row['date_created'];
$DBLinkToFile = $row['link_to_file'];
$DBGivenName = $row['given_name'];
//if the image was given a name by the user, display it
//otherwise display the generated name
if (strlen($DBGivenName) > 1) {
$FileName = $DBGivenName;
} else {
$FileName = $DBImageName;
}
$content = '
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
';
}
}
How do I get all the image so that in the end the html looks like this:
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
Later on in my html page I have:
<?php echo $content; ?>
change $content = ' to $content .= '
PHP String Operators
Note: set the $content variable to empty string or null before your while loop
What you're looking for is string concatenation (see string operators)
to fix your problem you'll need to first initialise an empty string, do this before your while loop
} else {
$content = '';
while($row = mysqli_fetch_array($DBQuery3)) {
and then use the concatenation operator . to append each image
$content .= '
<div class="image">
<img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/>
</div>
';
I'll try to describe this as best as possible, i haven't encountered this problem before but maybe i'm doing something wrong.
In my controller: i have:
public function indexAction() {
$this->view->projects = $this->projects->getProjects();
}
In the view file corresponding to this controller i have:
<?php echo $this->partial('partials/sidebar/_home.phtml', array($this->projects)); ?>
My _home.phtml contains the current code:
<div class="sidebar-content">
<p class="sidebar-title">Projects Portfolio</p>
<div id='coin-slider' style="margin:0 auto;">
<?php echo $this->partialLoop('partials/sidebar/_projects-slideshow.phtml', $this->projects);?>
</div>
</div>
And my _projects-slideshow.phtml has this code:
<a href="<?php echo $this->baseUrl($this->pimage); ?>">
<img src="<?php echo $this->baseUrl($this->pimage); ?>" alt="1" />
<span>
<?php echo $this->pname . ' by ' . $this->group . '. Client: ' . $this->client; ?>
</span>
</a>
The problem is that the variable is not passed to _home.phtml. I tried a Zend_Debug::dump($this->projects) and the result was NULL. I tried a Zend_Debug::dump($this) and I found the projects array. What am I doing wrong? The variable is not being passed, or maybe it is, to _home.phtml, not to mention that _projects-slideshow.phtml has no idea what $this->projects is.
If $this->projects in your _home.phtml is empty I think you should change
in index.phtml
<?php echo $this->partial('partials/sidebar/_home.phtml', array($this->projects)); ?>
into
<?php echo $this->partial('partials/sidebar/_home.phtml', array('projects' => $this->projects)); ?>
Here how it should has been:
$variables = array (
'records' => $result
);
$this->view->partial ("nodes/relations.php", $variables);
The variables in the array are named. What exactly is in your $this->projects->getProjects() ?