using print_r to print image path, instead of image itself - php

Ok more details:
Line with print_r outputs rendered images because in database
they are stored like <img src="" etc..
I want to see the path not image.
echo '<pre>';
foreach ($rows as $row){
if ($row->fulltext != ''){
print_r ( $row->fulltext );
echo "\n";
}
}
echo '</pre>';

Render image in browser:
$img = // get image path
echo '<img src='.$img.' />';
Output image path in browser:
$img = // get image path
echo $img;

Related

Echoing image tag in PHP using path

<?php
$dir= 'C:\xampp\htdocs\img';
echo "<img src='".$dir."\america.jpg' alt='icon'>";
?>
I just written this code for test and I couldn't get able to display image on browser.
The path you are using is not correct, try this:
$path = '/img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present in the same directory
or
$path = '../img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present one directory upwards from current directory

PHP: Use local image if it exsists

I have a script to cache images locally. It works perfectly;
<?
$image = file_get_contents("$bg");
$filename = basename($bg);
file_put_contents("images/$filename", $image);
?>
<img src="<? echo $bg; ?>"><br>
However, I want my script to use the local image in the img tag if it's already downloaded and present in my folder. If the image isn't already downloaded, then save the external image and then use the local image.
In other words; Check if the image is already in the folder, if it's not - then download it and display the local file. If it's already in the folder, just display the local image without downloading the image again.
You can test the existance of file with file_exists function please see documentation :
http://php.net/manual/fr/function.file-exists.php
<?php
$filename = basename($bg);
if(!file_exists($filename))
{
$image = file_get_contents("$bg");
file_put_contents("images/$filename", $image);
}
?>
<img src="images/<?php print $filename?>"><br />
You can use file_exists():
<?php
$filename = basename($bg);
if(!file_exists("images/$filename")){
// we don't have it, Cache it first
$image = file_get_contents("$bg");
file_put_contents("images/$filename", $image);
}
?>
<img src="<? echo $bg; ?>"><br>
You can use file_exists function to check already exits or not.
Try example
<?php
$filename = basename($bg);
if(!file_exists($filename))
{
$image = file_get_contents("$bg");
file_put_contents("images/$filename", $image);
}
?>
<img src="images/<?=$filename?>"><br>

how to get image caption using php

I'm using PHP to put all the images in a given folder into a slideshow.
This works fine in the following code:
<?php
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($images as $image){ $imgs[] = "$image"; }
/****** Display Images ******/
foreach ($imgs as $img) {
//I would like to get the image title here, to put in the echo below
echo "<div><img src='$img' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
This all goes quite easy, but since I would now also like to add the Titles of the pictures as captions, I need to extract/get this information from the image.
I'm thinking I can get it with something along the lines of the function exif_read_data , but I'm not quite sure how to get the title and not all the meta data . . .
With a little help from the smart people using stackoverflow, this is the final and functional result, as seen in my answer below as well and again, made with bits and pieces from several answers.
<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
$exif_description = "";
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
thats everything that can be done, because most pictures are not supported with this function, though, you are doing it wrong because the error that you are getting is because the file is not found, the error you get when the file is found and is not supported is this:
Warning: exif_read_data(file.png): File not supported in
/path/to/file/file.php on line x
and its because you single quoted the variable in
$exif_data = exif_read_data($img, 'IFD0');
the code could be less characters, this is my solution:
<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imgs = glob($directory . "*.jpg");
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src=\"$img\" alt=\"$exif_description\" border=\"0\" width=\"100%\" height=\"100%\"/ /></div>";
}
?>
Try this :-
foreach (glob("*.jpg") as $filename) {
$name = basename($filename, '.jpg');
echo "<div><img src='$filename' title='$name' alt='$name' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
To solve the problem, I basically used the Windows file browser, selected the image file and entered the text in the document comments section.
Then I printed the $exif_data as suggested above by #RamRaider and found out this text ends up in the ImageDescription of the file.
Once I nailed down the errors in my script, along with help from #Abdallah Samman, there wasn't much to it anymore!
Thank you everyone...
The following works beautifully:
<?php
/*
This script will print the image files in a given folder, along with their image description.
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
$exif_description = "";
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>

What is wrong with my code on printing a random image in a website?

this is the code in order to display a random image on my website,
but for some reason the images are not popping up and there is an error on the line where $rand_image was declared. The error says undefined index. I have 7 images in the directory.
$imagesDir = '\socimages\Badminton'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
$rand_image = $images[array_rand($images)]; // applying the random function
...
...
<img src="<?php echo $rand_image[0];?>"< alt="" height="246" width="246"></p></div>
$rand_image is the image already you don't need $rand_image[0];
<img src="<?php echo htmlspecialchars($rand_image);?>"< alt="" height="246" width="246"></p></div>
Also, $rand_image will probably be the path on disk, not the web accessible path so you'll need to map it.
I'd do something like this.
$imagesDir = '/socimages/Badminton/'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
if ($images !== FALSE) {
$rand_image = $images[array_rand($images)]; // applying the random function
} else {
die("This thang ain't working, yo.");
}
...
...
<img src="<?php echo $rand_image;?>"< alt="" height="246" width="246"></p></div>

PHP Random image not working

i want use php page(random image) in img src(<img src="ads.php"/>)
my ads.php page:
<?php
header('Content-Type: image/png');
$img = array();
$img[] = '<img src="images/1.png" />';
$img[] = '<img src="images/2.png" />';
shuffle($img);
readfile ($img[0]);
?>
The values you put into your $img array need to be pathnames to graphics, not img tags. When you're using readfile() it's trying to find '<img src="images/1.png" />' in the local filesystem.
<?php
header('Content-Type: image/png');
$img = array();
//$img[] = '<img src="images/1.png" />';
//$img[] = '<img src="images/2.png" />';
$img[] = '/path/to/images/1.png';
$img[] = '/path/to/images/2.png';
shuffle($img);
readfile ($img[0]);
?>
Consider how the browser is seeing your code. It's parsing the page and encounters:
<img src="ads.php" />
and goes "ahah, I have to go hit the ads.php script on the site and it'll feed me an image". This is NO different than if you had
<img src="ads.jpg" />
The url you're telling the browser to hit on the server MUST serve up an IMAGE.
But instead of an image, you're serving up more html.
if you want to out the image you have to use
<?php
header('Content-Type: image/png');
$img = array();
$img[] = '/path/to/images/1.png';
$img[] = '/path/to/images/2.png';
shuffle($img);
readfile ($img[0]);
?>

Categories