Retrieve Image from database, display and attach it to a link - php

I have Images stored in my DB, I want to retrieve them all and display them in the browser. It works just fine, but I want to give the user the ability to click on the picture so that they can have a better view of the picture (so display it with its original size). I tried many things but didn't work. second question: if I want my code to support multiple images type (such gif, jpg, etc..), if there a way to do it without having to save the image type (when I insert the images) and play with a whole bunch of if/else (when I retrieve them)?
This is my code
$count = 0;
echo " <div class=\"row\">";
while($row = $result->fetch_assoc()) {
$imagename = base64_encode( $row['Image'] );
if(($count%3) ==0){
echo "</div>";
echo " <div class=\"row\">";
echo " <div class=\"col-sm-2\">";
echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}else{
echo " <div class=\"col-sm-2\">";
echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}
}
echo "</div>" ;

The answer to your first question is "You have to use light-box jQuery plugin", in order to display larger (original size) of a thumbnail you need to use any lightbox plugin, I recommend using this one
http://www.jqueryscript.net/zoom/Responsive-jQuery-Lightbox-Like-Image-Zooming-Plugin-Lighter.html
Or you can search for any other plugin, there are thousands of them.
Regarding your second question, If you will use data as an images source, you have to specify a type, no way you can do that without mentioning types.

Related

Pass image url from one page to another in PHP

I am fetching data from database table and showing result in HTML table. What I want to achieve is that, there is image column in every row, and every image is fetched from its URL (stored in database). I am opening an image in new tab whenever it is clicked.
My question is that, how can I store url of image so that on the next page I don't want to show that real url?
The next page will look like this link:
www.example.com/full_size_image.php
and add
img tag
there to show that page
how can I store that specific image url when someone click on it?
<img src= "<?php echo $_SESSION['link'] ;">
My current code is :
while($result = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>".$result['c']."</td>";
echo "<td>".$result['UserName']."</td>";
echo "<td>".$result['UserProblemKeyword']."</td>";
echo "<td> <a href ='".$result['UserProblemPicture']."' target='_blank'><img src='".$result['UserProblemPicture']."' height='62' width='62'> </a> </td>";
echo "</tr>";
}
Use a parameter via the GET Request?
So instead of directing to just /full_size_image.php direct to
/full_size_image.php?img=filename.png
then in your php code for /full_size_image.php
You can fetch the file name via
<?php
$img = isset($_GET['img']) ? htmlspecialchars($_GET['img']) : "";
if ($img == "") {
//img not set
}
//do whatever with the filename

Displays an image from the database, Picture does not Show using php

Good evening experts :), i am learning to display images from database using php, i got problem. No error message but image not showing, please help.
this my code headline.php :
<?php
include 'koneksi.php';
$tabel = 'isiberita';
$sql=$conn->query("SELECT * from $tabel order by tanggal desc limit 3");
while ($tampil = $sql->fetch(PDO::FETCH_ASSOC)) {
echo "<p align='justify>'";
echo "<img src='$tampil[gambar]' aligen='left'>";
echo "<font valign='top'>";
echo "<strong>";
echo $tampil['judul'];
echo "</strong>";
echo $tampil['tanggal'];
$data=substr($tampil['isi'],0,200);
echo $data;
echo "<a href='index.php?menu=detail_headline&id=$tampil[id_berita]'> baca lengkap>>>></a>";
echo "</font></p><br>";
}
?>
This my image directory :
C:\xampp\htdocs\portalberita\gambar
This my headline.php directory :
C:\xampp\htdocs\portalberita
This my value database :
This output :
To show image from a directory in <img> tag, you have to specify the image path and name like:
<img src="/images/imagename.ext"> <!-- If image folder exist on the same path where the code file exists -->
or
<img src="../images/imagename.ext"> <!-- If image folder exist one level up where the code file exists -->
so check the image folder and your code directory and fix the path accordingly.
Try replacing
echo "<img src='$tampil[gambar]' aligen='left'>";
with
echo '<img src="' . $tampil['gambar'] . '" align="left" />';

Wordpress PHP replace image with site title if default value is empty

I am making options in the 'customization api' for Wordpress. However for things such as the logo image, if no image has been uploaded (default value is blank) i want it to display the title of the Wordpress site. I have searched google a lot, but I just cant seem to get it to work.
I know the PHP code for the Wordpress site title, but I can't get the php 'if' and 'else' to work.
current code:
<img src="<?php echo get_theme_mod('logo_image'); ?>" alt="<?php bloginfo('name'); ?>"/>
..obviously this is just the option to upload the image, which WORKS, but I want it to display the site title if the default value is empty.
Thank you!
I don't know how you are setting the theme mod. I.e. are you using 'set_theme_mod' ?
But assuming you are complying to WordPress standards the code snippet below should be what you are seeking.
if (!empty(get_theme_mod('logo_image'))) {
echo "<img src='" . get_theme_mod('logo_image') . "' title='" . bloginfo('name') ."'>"
} else {
echo "<h2>" . bloginfo('name') . "</h2>";
}
Hope this helps you out,
Dave.
Can you try this?
<img src="<?php if (!isset(get_theme_mod['logo_image'])) {
echo ('Your custom image');
}
else{
echo get_theme_mod('logo_image');
}
?>" alt="<?php bloginfo('name'); ?
>"/>

Fancybox multiple duplicate images

I have read the other threads on this topic but have not found an answer to my particular situation. I have modified the code from http://jsfiddle.net/g9R4H/ to produce an example of what I'm trying to accomplish. In my implementation I may have multiple duplicates of multiple images. One image may have two thumbnails and another may have 3 all on the same page. I have narrowed down the problem to the eq(0) setting and have determined that changing that value to 1 selects a different image when fancy box opens. I have not been able to determine how to set that value based on the image that is clicked. In my actual scripts the links are being generated via php and I do know when I create the trigger links what image value it relates to. I'm quite the rookie when it comes to javascript so keep that in mind when you answer. TIA
<a data-trigger-rel="gallery" class="fancybox-trigger" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
<a data-trigger-rel="gallery" class="fancybox-trigger" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
<br />
<br />
<a rel="gallery" class="fancybox" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
<a rel="gallery" class="fancybox" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
$(".fancybox-trigger").click(function() {
$("a[rel='" + $(this).data('trigger-rel') + "']").eq(0).trigger('click');
return false;
});
$(".fancybox").fancybox();
I haven't had any responses, but after some thought I come to a solution using PHP. Basically I am using php to create a separate trigger for each occurrence of duplicate images. When the trigger links are created the trigger array is updated. After all the links have been added, the trigger array is processed and a separate trigger created for each. If someone else is having this issue, hopefully this will help.
<?php
$a=0;
$image_id_array=array();
$trigger_array=array();
$result_set= get_collection_standards($sel_collection);
while ($standard = mysqli_fetch_array($result_set)) {
$image_id=$result["image_id"];
$path=$result["path"].$result["file_name"];
$caption = htmlentities($result["caption"]);
If (!array_key_exists($image_id,$image_id_array)){
$image_id_array[$image_id]=$a;//image_id receives the key value and the value is the photo number in the gallery - 1.
$a+=1;
$link="<a rel=\"feature_gallery\" class=\"fancybox\" href=\"{$path}-l.jpg\"";
} Else { //if the image does exist create a trigger link.
$link="<a data-trigger-rel=\"feature_gallery\" class=\"fancybox-trigger". $image_id_array[$image_id] .
"\" href=\"{$path}-l.jpg\" ";
if (!array_key_exists($image_id,$trigger_array)) { //update the trigger array to be used below.
$trigger_array[$image_id]=$image_id_array[$image_id];
}
}
$link .= " title=\"{$caption}\"><img src=\"{$path}-s.jpg\" ></a>";
echo $link;
}
foreach ($trigger_array as $position) {
echo "<script>";
echo " $(\"a.fancybox-trigger{$position}\").click(function() {";
echo " $(\"a[rel='\" + $(this).data('trigger-rel') + \"']\").eq(" . $position . ").trigger('click');";
echo " $.fancybox.open($(this).attr('tirgger-rel'));";
echo " return false;";
echo "});";
echo "</script>";
}
?>

How to echo images from a separate directory?

So I have now worked out how to echo images from MySQL.
Now I want to work out a way to echo images from a separate directory?...In this case WordPress.
My goal is to grab any posts on my WordPress blog which have a featured image and echo them onto a page of my choice.
So here is what I have so far:
echo "
<div class=\"pagination\" style=\"display:inline\"><ul style=\"background-color:#\"><li><div class=\"span3_search\"><h2><a href='$link'><b>$title</b></a></h2><br><a href='$link'><img id=\"result_img\" src=\"$image\" /></a><br /><p>$description</p><br />
<a href='$link'>$link<br /><br /></a><p></div></li></ul></div>
";
how do I echo a image src which is the source to where my featured images are.
i.e
echo "<img src="PHP WHICH SAYS GRAB WORDPRESS FEATURED IMAGES FROM THIS DIRECTORY etc">";
You can fetch the image of the path and then append a new path;
$image = explode('/');
$image = $image[count($image)];
echo 'mynew/path/toimage/'.$image;
To see al parts of the path, do;
echo '<pre>';
print_r($images);
echo '</pre>;

Categories