Skip to content

Instantly share code, notes, and snippets.

@fastmover
Created October 2, 2015 17:17
Show Gist options
  • Select an option

  • Save fastmover/35b0431991254345399e to your computer and use it in GitHub Desktop.

Select an option

Save fastmover/35b0431991254345399e to your computer and use it in GitHub Desktop.
<?php
/**
* Created by Steven Kohlmeyer
* Date: 10/2/15
* Time: 11:35 AM
*/
/*
* This file will randomly pick an image in the folder specified below by $imagesDir, rename it to the new image file name specified by $newImageName.
*
* Make sure to not have any image files named similar to the $newImageName as it searches through the files and removes any possibility of using any images similarly named
*/
$imagesDir = 'images/'; // This folder is relatively located to the current file.
$newImageName = "publicImage"; // DO NOT SET A FILE EXTENSION IN THIS VARIABLE!!!
///////////////// DO NOT MODIFY ANYTHING BELOW THIS LINE //////////////////////////
$cwd = getcwd();
$fullImagePath = $cwd . "/" . $imagesDir;
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach( $images as $key => &$image ) {
if( false != strpos( $image, $newImageName ) ) {
unset( $images[ $key ] );
}
}
$randomImage = $images[array_rand($images)]; // See comments
$ext = pathinfo( $randomImage, PATHINFO_EXTENSION );
var_dump( $randomImage );
$https = isset( $_SERVER['HTTPS'] );
$http = "";
if( $https ) {
$http = "https://";
} else {
$http = "http://";
}
$currentFileURL = $http . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$currentDirURL = dirname( $currentFileURL );
$url = $currentDirURL . "/" . $imagesDir . $newImageName . "." . $ext;
copy( $cwd . "/" . $randomImage, $fullImagePath . $newImageName . '.' . $ext );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment