Pulling an image from directory based on file name? - php

I'm trying to pull an image from a directory based on the file name matching the id number given in the URL. (ie: php?id=1 being the same as 1.png)
I've tried several different methods, and while I'm not getting any errors, I'm still getting no image that shows up when I type in the id tag.
Here's the most recent version of the code I've been working with:
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$img = $id;
foreach(glob("gallery3/var/albums/" . $img) as $filename)
?>
<img src = " <?php echo $filename; ?> " />
I'm at a loss. I've tried everything I can find and nothing seems to be working. I really don't want to pull the images from the database, and would prefer to pull them straight from the directory so that future uploaded images will be accessible directly from their id tag.

I think you're over-complicating it. All you need is this
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$img = $id;
?>
<img src="gallery3/var/albums/<?php echo $img; ?>.png" />

You could also go for:
<?php
if(isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {
echo "<img src='gallery3/var/albums/".$_GET['id'].".png'/>";
}
else {
echo "Not Valid!";
}
?>
In the else statement at the bottom you could implement some form of error checking to present to the user in the event that a non valid input is received.

Related

WordPress IF statement isn't outputting ELSE

I have a straightforward if statement to evaluate whether or not an image is set to appear as the logo on a WordPress build. If it returns empty/false, it displays a default image whose location is set as an absolute value.
The problem is when an image ISN'T set, the else statement is failing. I'm not receiving an error, but the code returned is simply an image tag without any source i.e. "< img src >".
Here is the statement:
<?php
$logo = $wp_options['header_logo'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo['url']; ?>">
<?php } else { ?>
<img src="wp-content/themes/wpdev/images/logo.png">
<?php } ;?>
I guess that if statement is failing, because you are treating $logo variable as a string.
It seems you're using $logo variable as an array, if you want to check out if it is empty you can use is_null() function of PHP.
By the way, we can't understand your problem this way, you should be more specific. Share your error or warning messages, the way it's behaving, etc..
Found a solution to this by specifically referring to the url of the image array when the variable $logo was defined. This way, the IF is evaluating the URL of the image.
The problem seemed to be that the initial IF was being evaluated as TRUE but obviously not returning the URL as this wasn't originally specified in $logo. By looking for the URL in the 'header_logo' array, it corrected the problem.
<?php
$logo = $wp_options['header_logo']['url'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo; ?>">
<?php } else { ?>
<img src="<!-- Image Location -->">
<?php } ;?>

how do i echo an image in another page based on its id?

How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)

PHP pull specific sets of images from a directory

I'm pulling an image from a directory (images) and placing it in an html table row along with my mysql query. PHP finds the image as the mysql DB id matches the image name. i.e. if id = 12 then 12.jpg is pulled from directory using $imgnum in the path.
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$imgnum = $row["id"];
?>
<tr>
<td>
<img src="images/<?php echo $imgnum ?>.jpg"/>
The issue is there may be a series of images all associated with that row, i.e. 1-1.jpg 1-2.jpg and so on, sometimes there may be only one and the code above works fine.
Is there a way to output all images with the name equalling the id but with a dash afterwards so it picks up other associated images? Even if someone can tell me what to learn to achieve this?
cheers
You can use php's glob() to find similar files. Something along the lines of this:
foreach (glob("/path/to/images/folder/" . $imgnum . '-*') as $filename) {
// do something with $filename
}
Thought I'd post what I used from the help above. It now outputs a list of all of the images I needed.
<?php
$imgnum = $id;
foreach (glob("images/" . $imgnum . '-*') as $filename) {
echo "<a href=''><img src='$filename' class='imgsize'></a> <br>";
}
?>

PHP if image name with this extension doesn't exist in the directory, then show XXX.png

I'm pretty new in PHP and what I would like to do is for PHP to check if a certain image file with a specific name exists in a specific directory, then echo the name of the file, but if it doesn't exist, then just show XXX.png.
I currently have a page (http://powerplantv2.jehzlau.net/ppm-deals) that echoes all product names from a certain attribute in Magento.
This page calls all images based on the attribute name. For example in my page there's an attribute name called "cool haan". So it automatically calls the image named "coolhaan.png". If there's an attribute name called "levis" then it will show an image named levis.png.
But I don't know how to add a condition if levis.png doesn't exist in the directory, I just want to call XXX.png.
How can I let PHP check first if the image exists that matches the certain attribute in the directory, then show attributename.png, if now, XXX.png.
Currently, below is my code:
<?php
$name ='deals';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem(); $attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId); $ppmtenants =
$attribute ->getSource()->getAllOptions(false); ?>
<?php foreach ($ppmtenants as $ppmtenant): ?>
<img src="<?php echo $this->getUrl() ?>media/wysiwyg/Deals/<?php echo strtolower($ppmtenantclean); ?>.png">
<?php endforeach; ?>
The code above gets all the options from a certain attribute then displays all the images with the attribute name. What I want to do is to check for the image name first before showing it.
To simplify my question, I just want to add a placeholder image with the name XXX.png for attributes with no images yet. :D
You can use file_exists() http://php.net/manual/en/function.file-exists.php to check if the file exists and then display the image, otherwise output the placeholder image.
<?php if(file_exists(path_to_your_file)) {
// Image does exist, fetch the image
} else {
// Image doesn't exist, output xxx.png
}
?>
Both the answers posted so far are correct, but to make things easier for you, you can do something like this:
<?php foreach ($ppmtenants as $ppmtenant):
if(file_exists($this->getUrl()."media/wysiwyg/Deals/".strtolower($ppmtenantclean).".png"))
{ ?>
<img src="<?php echo $this->getUrl() ?>media/wysiwyg/Deals/<?php echo strtolower($ppmtenantclean); ?>.png">
<?php
}
else
{
echo '<img src="xxx.png" alt="No image" />';
}
?>
<?php endforeach; ?>
<?php
if (!file_exists("PATH_TO_IMAGE") {
//display xxx.png
} else {
//load the image
}

echo images using php

what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.

Categories