I have a folder named 'Folder'. There are several photos inside it.
One of them is "1.jpg"..
I need to retrieve all the photos from this folder, except "1.jpg" ($first)..
As I understand, I need something like if ($image=$first) { . . . } inside of foreach.
$first="1.jpg";
$dirname="folder";
$images = glob($dirname.'*');
foreach($images as $image) {
$html="<img src='".$image."'><br />";
echo $html;
}
Thanks for attention
You can skip the echo when $image is not equal (!=) to $first:
foreach($images as $image) {
if ($image != $first) {
$html="<img src='".$image."'><br />";
echo $html;
}
}
Or you can use continue to skip to the next image, when $image is equal to $first, if you have more complex code in the foreach:
foreach($images as $image) {
if ($image == $first) {
continue;
}
$html="<img src='".$image."'><br />";
echo $html;
}
$first="1.jpg";
$dirname="folder";
$images = glob($dirname.'*');
unset($images[$first]);
foreach($images as $image) {
echo "<img src='".$image."'><br />";
}
Related
I tried sort, Ksort, multiSort, nothing is working and I'm not sure why.I can use print_r and see that it is an array but it won't sort just keeps giving errors. I think it is because the values are floats but I may be wrong.
Here's a page with the array shown using print_r function:
http://forcedchange.testdomain.pw/gallery/
Here is my code:
<?php
$uploads = wp_upload_dir(); //Path to my gallery uploads folder
if ($dir = opendir($uploads['basedir'].'/gallery-2')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
$images = ksort($images); /* not working */
// echo '<pre>';
// echo print_r($images);
// echo '</pre>';
foreach($images as $image) {
echo '<figure><img src="';
echo $uploads['baseurl'].'/gallery-2/'. $image;
echo '" alt="" /></li>';
echo '<figcaption>';
echo '<p>' . erq_shortcode() . '</p>';
echo '</figcaption>';
echo '</figure>';
}
?>
Try using natsort($images) (don't know which result you want). It should sort the array like:
1.png
2.png
...
9.png
10.png
...
20.png
Assigning won't work because the sort-funcs return a bool... the sorting is done direct inside the given array.
$images=glob("/path/*.{jpg,png,gif}");
ksort($images);
foreach($images as $image)
{
...
... do something with basename($image);
...
}
Files are
AAA_1.jpg AAA_2.jpg AAA_3.jpg BBB_1.jpg BBB_2.jpg CCC_1.jpg
foreach ($carousel as $image) {
echo "<img src='images/$image'>";
}
How to filter image names, just to display AAA_ images.
result to see
<img src='images/AAA_1.jpg'>
<img src='images/AAA_2.jpg'>
<img src='images/AAA_3.jpg'>
There are a probalby a million ways to do this, but I would just check the name of the image to see it begins with 'AAA_'
foreach ($carousel as $image) {
if(strpos($image,'AAA_') === 0){
echo "<img src='images/carousel/$image'>";
}
}
Or more complex, but still fun, entertainingly worth the extra performance hit of explode:
foreach ($carousel as $image) {
$image_parts = explode('_', $image);
if($image_parts[0] == 'AAA'){
echo "<img src='images/carousel/$image'>";
}
}
foreach ($carousel as $image) {
if(substr($image, 0, 3) == 'AAA'( {
echo "<img src='images/carousel/$image'>";
}
}
If you know all your file, you can use that code, also use in_array ;-)
$carousel = array('AAA_1.jpg', 'AAA_2.jpg', 'AAA_3.jpg','BBB_1.jpg','BBB_2.jpg','CCC_1.jpg');
$entry = array('AAA_1.jpg', 'AAA_2.jpg', 'AAA_3.jpg');
foreach ($carousel as $image) {
if(in_array($image, $entry)) {
echo "<img src='images/carousel/$image'>";
}
}
By using substr and strlen you will be able to get the images you want starting with the prefix variables value.
$prefix = 'AAA_';
$carousel = array(
"AAA_1.jpg",
"AAA_2.jpg",
"AAA_3.jpg",
"BBB_1.jpg",
"BBB_2.jpg",
"CCC_1.jpg"
);
foreach ($carousel as $k => $v) {
if (substr($v, 0, strlen($prefix)) == $prefix) {
echo "<img src=\"images/{$v}\" alt=\"image {$k}\" />";
}
}
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
I have the following code that reads the content of a folder
PHP Code:
<?
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');
//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname="./images") {
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$file .'";' . "\n";
$curimage++;
}
}
closedir($handle);
}
sort($files);
return($files);
}
echo 'var galleryarray=new Array();' . "\n"; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names
?>
which spits out this the following code:
var galleryarray = new Array();
galleryarray[0] = "image1.jpg";
galleryarray[1] = "image5.jpg";
galleryarray[2] = "image2.jpg";
galleryarray[3] = "image4.jpg";
galleryarray[4] = "image8.jpg";
galleryarray[5] = "image7.jpg";
galleryarray[6] = "image0.jpg";
galleryarray[7] = "image3.jpg";
galleryarray[8] = "image6.jpg";
Now in my html file I want to echo something like
<img src="images/image0.jpg" />
<img src="images/image1.jpg"/>
...
...
How can I do that?
foreach($galleryarray as $img) {
echo "<img src='images/$img' />";
}
maybe this help u
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
if($handle = opendir("./images")) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ ?>
<img src="/images/<?php echo $file; ?>">
<?php
}
}
}
assuming that you want to sort image name by value and then show the images. you first need to sort values by using function "asort"
asort($galleryarray);
foreach ($galleryarray as $key => $val) {
echo "<img src='images/{$val}'/>"
}
if you want to print and array
1) print_r($array); or if you want nicely formatted array then
echo '<pre>'; print_r($array); echo '<pre/>';
2) use var_dump($array) to get more information of the content in the array like datatype and length.
3) you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website http://in3.php.net/manual/en/control-structures.foreach.php
Try changing the echo line in the function to this:
echo '<img src="images/' . $file . '" />' ;
Hope this will help :)
OR
Outside the function, use it like this:
$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
echo '<img src="images/' . $img . '" />';
}
Also, you have to include this line inside the if condition in the while loop:
$files[] = $file; //insert the file into the array. This array is what we are going to return from the function
Update
I have tested this code and it's working:
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');
function returnimages($dirname="./images") {
$pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"; // http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(preg_match($pattern, $file)){ //if this file is a valid image
$files[] = $file;
}
}
closedir($handle);
}
//sort($files); // http://php.net/manual/en/function.sort.php
natcasesort($files); // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php
return($files);
}
$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
echo '<img src="images/' . $img . '" />';
}
In my images folder, I have put 5 files for testing. And upon running it, it would give the following:
<img src="images/a.jpg" />
<img src="images/ba.jpg" />
<img src="images/ca.jpg" />
<img src="images/Copy (3) of a.jpg" />
<img src="images/Copy (4) of a.jpg" />
I have previously asked a question on how to echo the url of images from an html page. I can do this successfully but how can I narrow this down further so only images urls beginning with a certain phrase are shown, furthermore how can I add an image tag around them so the images are displayed as images and not just text?
e.g I only want to list images beginning with http://photos.website.com.
edit: I forgot to mention this is the code used to iterate through the images:
foreach($images as $image) {
echo $image->getAttribute('src') . '<br />';
}
You will have to add a condition that tests the content of $image->getAttribute('src').
To test if a string beings by another, a possibility is to use the strpos function, which returns the position of the needle in the haystack -- here, you want that position to be 0 (i.e. the first character of the string).
foreach($images as $image) {
$url = $image->getAttribute('src');
if (strpos($url, 'http://photos.website.com') === 0) {
echo $url . '<br />';
}
}
Simple:
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (stripos($src, 'http://photos.website.com') === 0)
{
echo $src . '<br />';
}
}
And to add tags:
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (stripos($src, 'http://photos.website.com') === 0)
{
echo sprintf('<img src="%s" alt="" />', $src) . "\n";
}
}