Pull all images from a URL folder and display in Boostrap HTML - php

I need to pull all images from a URL directory (they are not displayed...just sitting in a folder on a server that I do not have access to) and display them within a Bootstrap Image gallery.
http://www.electrictoolbox.com/extract-images-web-page-php/
<?php
require_once('./simple_html_dom.php');
require_once('./url_to_absolute.php');
$url = 'http://www.bbc.co.uk';
$html = file_get_html($url);
foreach($html->find('img') as $element) {
echo url_to_absolute($url, $element->src), "\n";
}
?>
The URL for the folder where all the images are stored is:
http://masterplan.imgix.net/Slimming_Book/
Is it possible for php to scan this URL directory and pull the images to another website that is being hosted on another server?

Bit late but I figured I'd answer this. The below PHP code loads all ".png" images from the directory and then echos the image tag. You would replace the plain html tag for the equivalent bootstrap one.
dirname = "media/images/cats/";
$images = glob($dirname."*.png");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}

Related

Get image from PHP displaying using an img src

The code I have should output a jpg from a list of files in a directory however it is not. I have trawled this site and tried different methods but not helped. I am a relative beginner at php so looking for any help at all.
I have tried using img src in the php code but I am trying to get the image to display within a Wordpress post so I cannot echo the img src within the script. I have tried file_get_contents and read file as well but it may be my lack of knowledge holding me back.
<?php
$imagepath = htmlspecialchars($_GET["image"]);
$imagenum = htmlspecialchars($_GET["num"]);
define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );
If(LOCALHOST){
define('PATH_IMAGES', 'this_path');
}else{
define('PATH_IMAGES', '../../../Images/');
}
$arrnum = $GLOBALS[imagenum] - 1;
$dirname = PATH_IMAGES . $GLOBALS[imagepath]."/";
$images = scandir($dirname);
rsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
header('Content-type: image/jpeg');
file_get_contents('$dirname$images[$arrnum]');
}
}
?>
Have you tried readfile(...); should read and output the file. In your example you are not outputting the image data
http://php.net/manual/en/function.readfile.php

PHP doesn't get image

This is the code I'm using to get and display images from a folder on my server:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$dirname = "$root/Folder/".$row['city']."/";
$images = glob($dirname."".$row['id']."#*.jpg");
foreach($images as $image) {
echo "<img src=\"".$image."\">";
}
The "normal" path of a picture is /Folder/Berlin/1#1.jpg. In the rendered HTML source code I can see PHP makes this link: /var/www/user_name/html/Folder/Berlin/1#1.jpg
But unfortunately the image doesn't get loaded.
What am I doing wrong?

Display the uploaded image after uploading on the web page

I am trying to display the image that i have uploaded and moved to the desired location. Here is the code below.
if(isset($_FILES['image']))
// image upload from upload.html
{
session_start();
$_SESSION['str'];
$_SESSION['img'];
$image = basename($_FILES["image"]["name"]);
move_uploaded_file($_FILES['image']['tmp_name'], $_SESSION['str'].'_5'.$_SESSION['img']);
//I am trying to display the uploaded pic
echo '<img src= "$image"/>';
}
The image is stored at the location $_SESSION['str']. How can i display this uploaded image.
You're using the wrong path to show the image. You're using the orginal name of the image $_FILES["image"]["name"] that was uploaded and then you use the move_uploaded_file function to move and save the file as $_SESSION['str'].'_5'.$_SESSION['img'] so that doesn't match (can't see how your session variables are created).
Also, is the location where you save the uploaded file to accessable by the client side? Move the file in the public area of your web application.
Update
I now understand from your comment that you want to save the file in a private location and then show that file in a <img> element in some HTML template.
I changed the example code to embed the uploaded image into the HTML with base64.
You can use this function for creating the embed code. I took it from the answer from this question How to embed images...
function dataUri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return 'data:' . $mime . ';base64,' . $base64;
}
So then you can use it like:
session_start();
// absolute path including the path to the public folder
$image_dest_path = './public/img/' . $_SESSION['str'] . '_5' . $_SESSION['img'];
// move file to server location
move_uploaded_file($_FILES['image']['tmp_name'], $image_dest_path);
// imbed the image into the HTML.
echo '<img src= "' . dataUri($image_dest_path, 'image/jpg') . '"/>';
First of you say the location is stored in $_SESSION['str'] but you try to place an image with the value of basename($_FILES["image"][""name]).
Second; you should be using session_start() at the top of your page.
Third; in order to use a variable in a string, you need to use double quotes (") in stead of single quotes ('). Like so:
echo "<img src='$_SESSION['str']">;
But I'd use this:
echo '<img src="'. $_SESSION['str'] .'" >';
Also, are you sure you're not getting any errors? If you don't see any try placing this at the top of your file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
iam assuming that $_SESSION['str'] session variable is the path till the folder where the image is stored
Use the below code, give the complete url of the image:
echo "<img src = '".$_SESSION['str'].DIRECTORY_SEPARATOR.$image."'"." />";
how to call the node js method in angular js?

I get a broken image icon for the output

I know it's accessing the directory because the correct number of broken icons are there. Even alt='Image not there' doesn't work, it only widens the element its in but doesn't show any text. I tried other scripts where it displayed the alt='Image not there' but still gave a broken image icon. I also tried to simply echo an image from the folder and still got a broken image icon so what am I doing wrong? Is it even a script thing or what is going on?
<?php
$dirname = "C:/uploads/";
$images = scandir($dirname);
shuffle($images);
$ignore = Array(".", "..");
foreach ($images as $curimg) {
if (!in_array($curimg, $ignore)) {
echo "<li><a href='".$dirname.$curimg."'>
<img src='img.php?src=".$dirname.$curimg."&w=300&zc=1' alt='Image not working' /></a></li>\n ";
}
}
?>
You should provide an URL and not a local path here:
echo "<li><a href='".$dirname.$curimg."'>
<img src='img.php?src=".$dirname.$curimg."&w=300&zc=1' alt='Image not working' /></a>
$dirname is not accessable to the outside world and should be something like:
www.my.domain.com/uploads/
You need to create a folder into your (if you are working in local machine)local server site folder. Because PHP is a server side scripting language. So only the files move into your folder correctly. then you can simply call
<?php
$dirname = "uploads/";
$images = scandir($dirname);
shuffle($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li><a href='".$dirname.$curimg."'>
<img src='img.php?src=".$dirname.$curimg."&w=300&zc=1' alt='Image not working' /></a></li>\n ";
}
}
?>
if you call images from local machine its not a logic. because you are building website. this website not show only your system. it shows all system. also all systems are not contain a uploads folder.

Get list of all image files in a directory

I am trying to generate some HTML code to list some images for a slide show.
I arrived at the following idea:
function galGetTopPhotos()
{
//path to directory to scan
$directory = SITE_ROOT_PATH."/gallery/best/";
//get all files
$images = glob($directory . "*.*");
//print each file name
$ret = "";
$ret .= '<div id="myslides">';
foreach($images as $image)
{
$ret .= '<img src="'.$image.'" />';
}
$ret .= '</div>';
return $ret;
}
The problem is that it only works when I use root path for $directory...if I use URL it will not work. And it causes the images to not load. Here is what this code generates:
<div id="myslides">
<img src="D:/xampp/htdocs/mrasti/gallery/best/1.jpg" />
<img src="D:/xampp/htdocs/mrasti/gallery/best/10.jpg" />
</div>
So the question is, how to get the list of files so it generates img sources in http://127.0.0.1/.... format?
What I mean if I use the code like this it returns no file!
$directory ="http://127.0.0.1/mrasti/gallery/best/";
This looks like a job for PHP function basename. This takes a file path and returns only the final element of the path - in this case the actual name of the jpeg image.
You could amend your code so that it looks something like this:
$urlPath = "http://127.0.0.1/mrasti/gallery/best/";
...
...
foreach($images as $image)
{
$relative_path = $urlPath.basename($image);
$ret .= '<img src="'.$relative_path.'" />';
}
The above takes the path and appends the filename "example.jpg" to your image directory url
glob does only work for local files and not on remote files. Have a look here:
http://php.net/manual/en/function.glob.php
For remote files have a look here:
http://www.php.net/manual/en/features.remote-files.php
But i do not think that you need remote files. It seems like you want to go through a local directory and display this images.
Try something like this
...
$ret .= '<img src="http://127.0.0.1/my/path/'.basename($image).'" />';
...
You need to have some functionality to translate the file path on disk to the correct URI so that your browser can understand it.
In your specific case as outlined and with the exact data given in your question, the following could work:
foreach($images as $image)
{
$src = '/mrasti/gallery/best/'.substr($image, strlen($directory));
$ret .= '<img src="'.$src.'" />';
}

Categories