Download link over random php image












0















I am displaying a number of random images from a folder, however I'm not very good with PHP (Code sourced from the internet), how would I go about having a "download" link display on top of the image?



Display random image from folder using PHP:



function random_image($directory)
{
$leading = substr($directory, 0, 1);
$trailing = substr($directory, -1, 1);

if($leading == '/')
{
$directory = substr($directory, 1);
}
if($trailing != '/')
{
$directory = $directory . '/';
}

if(empty($directory) or !is_dir($directory))
{
die('Directory: ' . $directory . ' not found.');
}

$files = scandir($directory, 1);

$make_array = array();

foreach($files AS $id => $file)
{
$info = pathinfo($dir . $file);

$image_extensions = array('jpg', 'jpeg', 'gif', 'png', 'ico');
if(!in_array($info['extension'], $image_extensions))
{
unset($file);
}
else
{
$file = str_replace(' ', '%20', $file);
$temp = array($id => $file);
array_push($make_array, $temp);
}
}

if(sizeof($make_array) == 0)
{
die('No images in ' . $directory . ' Directory');
}

$total = count($make_array) - 1;

$random_image = rand(0, $total);
return $directory . $make_array[$random_image][$random_image];

}


Markup:



echo "<img src=" . random_image('css/images/avatars') . " />";


I've tried looking around google for an answer but I can't find anything, any help would be appreciated










share|improve this question

























  • You just make a link with the same URL as the image. Like, <a> instead of <img>.

    – miken32
    Nov 23 '18 at 22:12
















0















I am displaying a number of random images from a folder, however I'm not very good with PHP (Code sourced from the internet), how would I go about having a "download" link display on top of the image?



Display random image from folder using PHP:



function random_image($directory)
{
$leading = substr($directory, 0, 1);
$trailing = substr($directory, -1, 1);

if($leading == '/')
{
$directory = substr($directory, 1);
}
if($trailing != '/')
{
$directory = $directory . '/';
}

if(empty($directory) or !is_dir($directory))
{
die('Directory: ' . $directory . ' not found.');
}

$files = scandir($directory, 1);

$make_array = array();

foreach($files AS $id => $file)
{
$info = pathinfo($dir . $file);

$image_extensions = array('jpg', 'jpeg', 'gif', 'png', 'ico');
if(!in_array($info['extension'], $image_extensions))
{
unset($file);
}
else
{
$file = str_replace(' ', '%20', $file);
$temp = array($id => $file);
array_push($make_array, $temp);
}
}

if(sizeof($make_array) == 0)
{
die('No images in ' . $directory . ' Directory');
}

$total = count($make_array) - 1;

$random_image = rand(0, $total);
return $directory . $make_array[$random_image][$random_image];

}


Markup:



echo "<img src=" . random_image('css/images/avatars') . " />";


I've tried looking around google for an answer but I can't find anything, any help would be appreciated










share|improve this question

























  • You just make a link with the same URL as the image. Like, <a> instead of <img>.

    – miken32
    Nov 23 '18 at 22:12














0












0








0








I am displaying a number of random images from a folder, however I'm not very good with PHP (Code sourced from the internet), how would I go about having a "download" link display on top of the image?



Display random image from folder using PHP:



function random_image($directory)
{
$leading = substr($directory, 0, 1);
$trailing = substr($directory, -1, 1);

if($leading == '/')
{
$directory = substr($directory, 1);
}
if($trailing != '/')
{
$directory = $directory . '/';
}

if(empty($directory) or !is_dir($directory))
{
die('Directory: ' . $directory . ' not found.');
}

$files = scandir($directory, 1);

$make_array = array();

foreach($files AS $id => $file)
{
$info = pathinfo($dir . $file);

$image_extensions = array('jpg', 'jpeg', 'gif', 'png', 'ico');
if(!in_array($info['extension'], $image_extensions))
{
unset($file);
}
else
{
$file = str_replace(' ', '%20', $file);
$temp = array($id => $file);
array_push($make_array, $temp);
}
}

if(sizeof($make_array) == 0)
{
die('No images in ' . $directory . ' Directory');
}

$total = count($make_array) - 1;

$random_image = rand(0, $total);
return $directory . $make_array[$random_image][$random_image];

}


Markup:



echo "<img src=" . random_image('css/images/avatars') . " />";


I've tried looking around google for an answer but I can't find anything, any help would be appreciated










share|improve this question
















I am displaying a number of random images from a folder, however I'm not very good with PHP (Code sourced from the internet), how would I go about having a "download" link display on top of the image?



Display random image from folder using PHP:



function random_image($directory)
{
$leading = substr($directory, 0, 1);
$trailing = substr($directory, -1, 1);

if($leading == '/')
{
$directory = substr($directory, 1);
}
if($trailing != '/')
{
$directory = $directory . '/';
}

if(empty($directory) or !is_dir($directory))
{
die('Directory: ' . $directory . ' not found.');
}

$files = scandir($directory, 1);

$make_array = array();

foreach($files AS $id => $file)
{
$info = pathinfo($dir . $file);

$image_extensions = array('jpg', 'jpeg', 'gif', 'png', 'ico');
if(!in_array($info['extension'], $image_extensions))
{
unset($file);
}
else
{
$file = str_replace(' ', '%20', $file);
$temp = array($id => $file);
array_push($make_array, $temp);
}
}

if(sizeof($make_array) == 0)
{
die('No images in ' . $directory . ' Directory');
}

$total = count($make_array) - 1;

$random_image = rand(0, $total);
return $directory . $make_array[$random_image][$random_image];

}


Markup:



echo "<img src=" . random_image('css/images/avatars') . " />";


I've tried looking around google for an answer but I can't find anything, any help would be appreciated







php html image random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:15







Insignian

















asked Nov 23 '18 at 21:58









InsignianInsignian

349




349













  • You just make a link with the same URL as the image. Like, <a> instead of <img>.

    – miken32
    Nov 23 '18 at 22:12



















  • You just make a link with the same URL as the image. Like, <a> instead of <img>.

    – miken32
    Nov 23 '18 at 22:12

















You just make a link with the same URL as the image. Like, <a> instead of <img>.

– miken32
Nov 23 '18 at 22:12





You just make a link with the same URL as the image. Like, <a> instead of <img>.

– miken32
Nov 23 '18 at 22:12












2 Answers
2






active

oldest

votes


















1














You should save the image location in a variable then use it to create a link, plus display it.



$imageUrl = random_image('css/images/avatars');
echo "<a href=" . $imageUrl . ">";
echo "<img src=" . $imageUrl . " />";
echo "</a>";


or if you want to show the text link above, seperately then



$imageUrl = random_image('css/images/avatars');
echo "<a href=" . $imageUrl . ">Click Here</a><br />";
echo "<img src=" . $imageUrl . " />";





share|improve this answer



















  • 1





    Awesome, your second example was what I was looking for, thank you.

    – Insignian
    Nov 23 '18 at 22:40



















0














you could use simple javascript to do so, like onclick event for example :



just add this to img tag onclick='window.open('". random_image('css/images/avatars') ."')'



echo "<img onclick='window.open('". random_image('css/images/avatars') ."')' src='" . random_image('css/images/avatars') . "' />";





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453330%2fdownload-link-over-random-php-image%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You should save the image location in a variable then use it to create a link, plus display it.



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">";
    echo "<img src=" . $imageUrl . " />";
    echo "</a>";


    or if you want to show the text link above, seperately then



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">Click Here</a><br />";
    echo "<img src=" . $imageUrl . " />";





    share|improve this answer



















    • 1





      Awesome, your second example was what I was looking for, thank you.

      – Insignian
      Nov 23 '18 at 22:40
















    1














    You should save the image location in a variable then use it to create a link, plus display it.



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">";
    echo "<img src=" . $imageUrl . " />";
    echo "</a>";


    or if you want to show the text link above, seperately then



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">Click Here</a><br />";
    echo "<img src=" . $imageUrl . " />";





    share|improve this answer



















    • 1





      Awesome, your second example was what I was looking for, thank you.

      – Insignian
      Nov 23 '18 at 22:40














    1












    1








    1







    You should save the image location in a variable then use it to create a link, plus display it.



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">";
    echo "<img src=" . $imageUrl . " />";
    echo "</a>";


    or if you want to show the text link above, seperately then



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">Click Here</a><br />";
    echo "<img src=" . $imageUrl . " />";





    share|improve this answer













    You should save the image location in a variable then use it to create a link, plus display it.



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">";
    echo "<img src=" . $imageUrl . " />";
    echo "</a>";


    or if you want to show the text link above, seperately then



    $imageUrl = random_image('css/images/avatars');
    echo "<a href=" . $imageUrl . ">Click Here</a><br />";
    echo "<img src=" . $imageUrl . " />";






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 22:22









    Nawed KhanNawed Khan

    2,8141618




    2,8141618








    • 1





      Awesome, your second example was what I was looking for, thank you.

      – Insignian
      Nov 23 '18 at 22:40














    • 1





      Awesome, your second example was what I was looking for, thank you.

      – Insignian
      Nov 23 '18 at 22:40








    1




    1





    Awesome, your second example was what I was looking for, thank you.

    – Insignian
    Nov 23 '18 at 22:40





    Awesome, your second example was what I was looking for, thank you.

    – Insignian
    Nov 23 '18 at 22:40













    0














    you could use simple javascript to do so, like onclick event for example :



    just add this to img tag onclick='window.open('". random_image('css/images/avatars') ."')'



    echo "<img onclick='window.open('". random_image('css/images/avatars') ."')' src='" . random_image('css/images/avatars') . "' />";





    share|improve this answer




























      0














      you could use simple javascript to do so, like onclick event for example :



      just add this to img tag onclick='window.open('". random_image('css/images/avatars') ."')'



      echo "<img onclick='window.open('". random_image('css/images/avatars') ."')' src='" . random_image('css/images/avatars') . "' />";





      share|improve this answer


























        0












        0








        0







        you could use simple javascript to do so, like onclick event for example :



        just add this to img tag onclick='window.open('". random_image('css/images/avatars') ."')'



        echo "<img onclick='window.open('". random_image('css/images/avatars') ."')' src='" . random_image('css/images/avatars') . "' />";





        share|improve this answer













        you could use simple javascript to do so, like onclick event for example :



        just add this to img tag onclick='window.open('". random_image('css/images/avatars') ."')'



        echo "<img onclick='window.open('". random_image('css/images/avatars') ."')' src='" . random_image('css/images/avatars') . "' />";






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 22:23









        M0ns1fM0ns1f

        2,4912821




        2,4912821






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453330%2fdownload-link-over-random-php-image%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python