Displaying the picture on the browser - php

The following code returns the urls of the pics that has to be displayed:
$link = "http://example.com/ext/lib/exe/process.phpt=87458+52&w=466&h=471&:forum_pic:queue_hasb_bunny_khd_tyun_02300.jpg, Imm processing,http://example.com/storage/high resolution/i-white bunny-tyun.jpg,only cash payment,process after christmas, handle with care notification, date: 2-5-2015";
$extraFieldData = explode(',',$link);
$images= array();
foreach($extraFieldData as $efd){
if(strpos($efd,'jpg')!== false){
$images[] = $efd;}
}
foreach ($images as $i){
echo $i;
}
<img src= "<?php echo $i;?>"/>
The code works fine to echo the urls of the pictures but when I try to display the urls in the browser, only one picture is displayed and not the other one. How to get both the urls to display?

If it shows only one image (and the image URLs are correct), you're doing it outside of the loop and $i has the value of the last image iterated from the $images array.
For example:
$arr = ['one', 'two', 'three', 'four'];
foreach($arr as $a) {
}
echo $a;
This will output four.

Related

Display pictures from a folder in the specific way - PHP

I am trying to create a dynamic gallery in php with specific order of pictures on the page, but I can't find the function or piece of php code to do so.
Conditions:
The gallery must be dynamic, pictures will be coming from a folder via php, because when I'll add/upload more pictures in the folder they must be displayed on the page without adding manually the html tags in. ( this part is easy, problem is the condition 2 ).
The first row will have 5 pictures, the second - 4 pictures (important for the specific effect).
My code:
$files = glob("layout/gallery/*.jpg");
rsort($files, SORT_NATURAL);
for ($i=0; $i < count($files); $i++) {
for( ; $i<5; $i++){
$one = $files[$i];
echo '<img src="'.$one.'">' . '<br><br>';
}
echo '<br>';
for( ; $i<9; $i++){
$two = $files[$i];
echo '<img src="'.$two.'">' . '<br><br>';
}
}
The code works well, but it just displays 9 pictures obviously. I was unable to make it dynamic displaying 5 pictures first, 4 pictures after and stay this way in a loop till displays all pictures from that folder.
You can take advantage of the array_splice function that removes elements from the array everytime it returns those elements :
$files = glob("layout/gallery/*.jpg");
rsort($files, SORT_NATURAL);
// split the files in rows
$rows = [];
while(count($files) != 0) {
// even rows have 5 elements, odd ones have 4
$num_files_to_splice = count($rows) % 2 == 0 ? 5 : 4;
$rows[] = array_splice($files, 0, $num_files_to_splice);
}
// display them accordingly
foreach($rows as $row) {
foreach($row as $file) {
echo '<img src="'.$file.'">';
}
echo '<br><br>';
}

Display random images from folder with PHP without duplicates

I am trying to display a certain amount of random images from one directory on my website without displaying duplicates.
I found this question: Display 2 random pictures PHP with no duplicates which partially answers my problem:
<?php
$pics = array('image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg');
$images = array_rand($pics, 2);
?>
<img src="images/<?php echo $pics[$images[0]]; ?>" />
<img src="images/<?php echo $pics[$images[1]]; ?>" />
The problem I have is that the images are uploaded to the folder with completely different/random names such as 1a2265fg65444.jpg 55v4423097ww6.jpg etc, so I can't manually add them to the $pics array. I need to somehow scrape the directory for *.jpg and have the array generated automatically
I did try Michal Robinsons answer on Show Random images from a folder without repeating using JS or PHP but couldn't get it to print anything for some reason:
$all_images = glob("/images/photos/{*.jpg}", GLOB_BRACE);
shuffle($all_images);
$images = array();
foreach ($all_images as $index => $image) {
if ($index == 15) break; // Only print 15 images
$image_name = basename($image);
echo "<img src='/images/photos/{$image_name}' />";
}
Perhaps I'm missing something?
Any pointers would be simply awesome.
Many thanks
try this
$scan = scandir("/images/photos/");
shuffle($scan);
$r = rand(2, count($scan)); // maybe (count($scan) - 1)
printf("<img src='/images/photos/%s' />", basename($scan[$r]));
Thanks Aron, but I managed to fix it by changing:
glob("/images/photos/ to
glob("images/photos/
To complicate things further though, I just realized I need to show 1 image from a 1st folder followed by another image from a 2nd folder, and repeat this about 5 times, still without showing any duplicate :/ This is as far as I have got, but seem to be digging a bigger hole that doesn't work, as I'm stuck at the foreach...:
$all_images1 = glob("images/photos/{*.jpg}", GLOB_BRACE);
$all_images2 = glob("images/photos1/{*.jpg}", GLOB_BRACE);
shuffle($all_images1);
shuffle($all_images2);
$images1 = array();
$images2 = array();
$class = 1;
foreach ($all_images1 as $index1 => $image1) {
if ($index1 == 10) break; // Only print 15 images
$image_name1 = basename($image1);
echo "<img src='/images/photos/{$image_name1}' class='image".$class++."' /> <img src='/images/photos1/{$image_name2}' class='image".$class++."' />";
}
Am I going in the wrong direction to do this?
Thanks Ted

Echo values from two foreach arrays without duplicates

I have this two arrays:
$files=glob("filepath/folder/*.*);
$thumbs=glob("filepath/folder/thumbs/*.*);
Main folder called in $files gets the full resolution images, while thumb has thumbnails for faster page loading, so it does not need to load the big images and resize them.
But my problem is now how to echo these two loops out?
I've tried nested foreach loops like
foreach ($files as $fl){
foreach ($thumbs as $tb) {
echo '<img src="<?=$tb?>">';
}
}
But this makes duplicates because it echoes for each element in files and for each in thumbs.
How can I echo them without duplicating?
Thanks everyone for help, this worked
$len = max(count($files), count($thumbs));
for($i=0; $i<$len; $i++){
$fl = isset($files[$i]) ? $files[$i] : '';
$tb = isset($thumbs[$i]) ? $thumbs[$i] : '';
echo <img src="<?=$tb?>">;
}

How to randomise a list of URL's in PHP using ACF?

I have a web page that calls in 7 images from wordpress using an ACF Repeater field.
What I want to do is get the list of URL's of the images and then shuffle them so that they images will appear randomly on the webpage. When I call the images with ($image['url']) , the only image that is displayed is the last image uploaded to the wordpress site.
<?php
// Call the ACF Gallery images
$imageArray = get_field('company_logos', 13);
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $imageArray ):
while( have_rows('company_logos', 13) ) : the_row();
$image = get_sub_field('image');
// $content = get_sub_field('content');
//shuffle ($image['url']);
$arr = $image['url'];
shuffle($arr);
print_r($arr);
endwhile;
endif; ?>
When I echo out the images URL's to the screen they come out in the format of:
http://localhost/wordpress/wp-content/uploads/2016/07/a.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/b.png
http://localhost/wordpress/wp-content/uploads/2016/07/c.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/d.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/e.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/f.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/g.jpg
Anyone know how to do this?
Thanks!
Do you get the list of images from $image['url']? Does it return an array in the first place? If it does then your solution should work. If it is not an array but a string of URLs separated by comma then, do the following before the 2nd statement. So, the new code would look like as follows,
$urls = $image['url'];
$arr = explode(" ", $urls);
shuffle($arr);
print_r($arr);

PHPQuery... trying to get all links of all images from page

I am trying to get all links of all images on a given page using PHPQuery. I am using the PHP support syntax of PHPQuery.
This is the code I have so far:
include('phpQuery-onefile.php');
$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');
// in theory this gives me all image sources
$images = $all->find('img')->attr('src');
// but if I do `echo $images;` what I get is the src to the first image
Out of curiosity I have tried
$images = $all->find('img:first')->attr('src');
and
$images = $all->find('img:last')->attr('src');
and it prints correctly the first and the last image's addresses, respectively, but how in hell can I get an array of all links?
Within your foreach loop, you need to wrap the $a with a pq().
For example:
$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');
$imgs = $all['img'];
foreach ($imgs as $img) {
// Note: $img must be used like "pq($img)"
echo pq($img)->attr('src');
}

Categories