I have this code that should rename images. It takes the title of the site and generates a name but if there is no title it just keeps the old name.
How can i make it so that instead of the title it just randoms the number insteed? so instead of "title.extension" it will be randomnumber.jpg
?
$tmp = download_url( $img );
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $img, $matches);
$newfn = str_replace(array("%2B", "%52", "%20", "%5"), "B", basename($matches[0]));
$oofnm = basename($matches[0]);
if($newfn != $oofnm) {
$newfn2 = str_replace(array(".jpg", ".png", ".gif"), "", $newfn);
$tmppath = pathinfo( $tmp ); // extract path parts
$newpth = $tmppath['dirname'] . "/". $newfn2 . "." . $tmppath['extension'];
rename($tmp, $newpth); // renames temp file on server
$tmp = $newpth;
}
$file_array['name'] = $newfn;
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
continue;
}