Viewed   78 times

I have one directory called images/tips.

Now in that directory I have many images which can change.

I want the PHP script to read the directory, to find the images, and out of those images found to pick a random image.

Any idea on how to do this?

 Answers

2
$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments

You can send a 2nd argument to array_rand() to get more than 1.

Saturday, August 6, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
4

Assuming you know how to run the AJAX call, you'll want your .php file to output the path to the featured image. Wordpress has built in functions to retrieve the featured image, but the tricky part is being able to use those functions via AJAX.

You'll want your php file to look something like this

<?php//get-image.php 
//file that gets called from $.post(), $.get(), or $.ajax() function
//first load up wordpress
require_once('../../../../wp-load.php');//assuming get-image.php resides in /themes/yourtheme/ajax/
//get the post id
$post_id = url_to_postid($url_sent_from_ajax);
//now get the featured image
$image_url = get_the_post_thumbnail($post_id , 'post-thumbnail' );
echo $image_url;
?>

Then your ajax return function can do what it wants with image URL

Hope this helps

Monday, November 21, 2022
 
anurupr
 
2

The problem is BLOB which is limited to 65535 bytes.

Bitmaps are roughly 4-8x the size of a compressed image and will most probably need more space to be put into the database.

I suggest altering the column to LONGBLOB or at least MEDIUMBLOB.

You don't need the library from github either, if the image is fully stored, you can use file_put_contents() (which is binary safe, yea!) the same way as with png or jpeg.

Wednesday, November 2, 2022
 
johnw
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :