I get a broken image icon for the output - php

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.

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

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

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 />';
}

Echo out all images in a directory?

I am trying to echo out all of the images in a folder directory with a couple of exceptions/ignores.
This is working ok apart from it also echoes out a blank photo for every photo it echoes out?
why is this happening can someone please show me where I'm going wrong thanks.
<?php
$dirname = "./data/photos/".$profile_id."/";
$images = scandir($dirname);
$ignore = Array("_cover.jpg", "_default.jpg");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
};
}
?>
You appear to have an extra slash after your image source:
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
//--------------------------------------------------------^ here
This may be interefering with how the browser parses the DOM and causing an extra image to appear.
Also, small suggestion, try using this line instead:
echo "<img src='".$dirname.$curimg."' class=\"profile_photos\"><br>\n";
You should ensure that $curimg is actually a jpg file:
if(!in_array($curimg, $ignore) && preg_match("/\.jpg$/i", $curimg)) {
scandir returns not just files but subdirectories, including . and ...

echoing thumbnails that link to the larger image

I have a script that scans a directory of thumbnails and echoes them to the page. It works nicely, but the thumbnails are not clickable, and i would really like this to be the case. echo "<img src='$thumbnail' class='resizesmall'>"; is the line where the thumbnails are echoed. I'm not sure how to write the path to the larger image inside the php without breaking it. Maybe this should be done inside the foreach statement? thanks for your help?
$dir = "../mysite/thumbnails/";
$dh = opendir($dir);
// echo "$dh";
$gallery = array();
while($filename = readdir($dh))
{
$filepath = $dir.$filename;
//pregmatch used to be ereg
if (is_file($filepath) and preg_match("/\.png/",$filename))
{
$gallery[] = $filepath;
}
}
sort($gallery);
foreach($gallery as $thumbnail)
{
echo "<img src='$thumbnail' class='resizesmall'>";
}
?>
</div>
<??>
The easiest way would be to setup a situation where your thumbs and your full size images were named the same. So you may have thumbs/image1.png and full/image1.png. Then instead of using $thumbnail use a variable $image, or something similar just so the code reads better. You'll also want to leave the $filepath out of the mix so that $image ends up as just the file name.
foreach($gallery as $image)
{
echo "<a href='full/$image'><img src='thumb/$image' class='resizesmall'></a>";
}
You may want to throw in some checks to make sure there is a matching image just to prevent errors or bad UX. However, the code above should work.

printing a UL from an array of images

I am trying to print a UL list of images ending with _th.jpg returned from a specific folder. All i get back is array()
<?php
require '../../config.php';
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);
$id = (int)$_GET['id'];
$q = $conn->query("SELECT * FROM cms_page WHERE id=$id");
$project = $q->fetch(PDO::FETCH_ASSOC);
$q->closeCursor();
$imagesDir = 'public/images/portfolio/'.$project['slug'].'/';
$images = glob($imagesDir . '*_th.{jpg,jpeg,png,gif}', GLOB_BRACE);
echo $imagesDir ;
print_r($images);
?>
<h1><?=htmlspecialchars($project['title'])?></h1>
<?php
if ($images < 1) {
echo '<div id="slider">';
echo '<ul>';
foreach($images as $key => $value) {
echo '<li><img src="public/images/portfolio/'.$project['title'].'/'.$value.'.jpg" width="160" height="96" /></li>';
}
echo '</ul>';
echo '</div>';
} else {
echo 'There are currently no images to display for tihis project.';
}
?>
Anyone got any ideas?
First of all : are you sure you are reading from the right directory ?
What I mean, is : does 'public/images/portfolio/'.$project['slug'].'/' really exist, and is accessible from your script, using this relative path, in this PHP script ?
(You can test that with is_dir, for instance)
As a precaution, when working with files and directories, I like using absolute paths, so I always know to which directory exactly I'm accessing.
For instance, something like this :
$imagesDir = '/var/www/public/images/portfolio/'.$project['slug'].'/';
If you do not know the absolute path to your directories, you can use dirname(__FILE__), to get the absolute path to the current PHP file -- and then, starting from that file, use a relative path ; for instance :
$imagesDir = dirname(__FILE__) . '/public/images/portfolio/'.$project['slug'].'/';
This way, you don't have to care about the current working directory : your path will always be the same, as it's not relative.
If that doesn't work, then first of all, make sure you are accessing the right directory.
<h1><?=htmlspecialchars($project['title'])?></h1>
Is <? a valid PHP opening tag?
Since your last block of PHP code isn't apparently executed (since it would either print an empty ul or the text from the else block) I suspect the PHP doesn't get parsed correctly.

Categories