Results 1 to 2 of 2

Thread: Should I be using php for my site ?

  1. #1
    Join Date
    May 2014

    Red face Should I be using php for my site ?

    Hello I was looking up how to make a random image generator for a website I wanted to make. The two most popular results was javascript and php and the reason why I went with php is because I didn't like that you had to change the name of the image in javascript version, for example image1.jpg image2.jpg image3.jpg instead of having pool.jpg, girlslaughing.jpg, happydays.jpg.

    this is the code I'm using on my site that I found on the internet
    PHP Code:

    // This is the only portion of the code you may need to change.
    // Indicate the location of your images 
    $root '';
    // use if specifying path from root
    //$root = $_SERVER['DOCUMENT_ROOT'];

    $path 'images/';
    // End of user modified section 
    function getImagesFromDir($path) {
    $images = array();
        if ( 
    $img_dir = @opendir($path) ) {
            while ( 
    false !== ($img_file readdir($img_dir)) ) {
    // checks for gif, jpg, png
    if ( preg_match("/(\.gif|\.jpg|\.png)$/"$img_file) ) {
    $images[] = $img_file;

    getRandomFromArray($ar) {
    mt_srand( (double)microtime() * 1000000000 ); // php 4.2+ not needed
    $num array_rand($ar);

    // Obtain list of images from directory 
    $imgList getImagesFromDir($root $path);

    $img getRandomFromArray($imgList);

    <div><img src="<?php echo $path $img ?>" alt="<?php echo str_replace(array(.jpg’.png’.gif’), $images[$i]); ?>" /></div>

    Now my first question do I have to make the index.php I notice that If I put the php inside a html file the php doesn't work why do I care because I would like to use bootstrap to make my site. I never really worked with php before except for opencart and wordpress but I never really touch the code. But I just finish my first java class last term which ended 2 days ago so if php is anything like java I'm sure I can learn it enough to accomplish my goal.

    second question my end goal for this site is to achieve something like fukung.net. Do you see how in the url address bar the image name is there in the url bar this is important to me because of seo purposes. My php version the name stays the say mysite.com or mysite.com/image.php. the maker even made it so that anyone could add a tag to any image which seems cool. I've looked at this person code but I still can't figure out what language he or she is using is it php ?. So if anyone could tell me what the maker of this site is using it would be greatly appreciated.

  2. #2
    Join Date
    Dec 2002
    Seattle, WA
    I rewrote the PHP script so that it returns an image instead of an HTML tag for an image.
    PHP Code:
    $path 'images/';

    // append "/" if needed
    $path .= ($path[strlen($path) -1] !== '/') ? '/' '';
    $images glob($path '*.{gif,jpg,png}'GLOB_BRACE);
    $length count($images);

    // if there is no images, we need to do something,
        // sending a 500 server error seems appropriate.
    if (!$length) {
    header('HTTP/1.1 500 Internal Server Error');
    // pluck a random image out of array...
    $source $images[mt_rand(1$length) -1];

        if (
    function_exists('getimagesize')) {
    $mime_type getimagesize($source);
    $mime_type $mime_type['mime'];
        } else {
    $ext explode('.'$source);
    $ext array_pop($ext);

            switch (
    strtolower($ext)) {
    $mime_type 'image/gif';
    $mime_type 'image/jpeg';
    $mime_type 'image/png';
    // send image...
    header('Content-Type: ' $mime_type);
    header('Pragma: no-cache');
    get_random_image_from_directory($root $dir);
    Now, you can simply link to the script like so
    HTML Code:
    <img src="path/to/random-image.php" alt="">
    Do you see how in the url address bar the image name is there in the url bar this is important to me because of seo purposes.
    That is a favicon or favorites icon. The easiest way to add one to your site would be to make a 16x16 png and/or ico (icon file) and place it in your site's root directory. Some browsers will detect it even without linking to the file. But you can also add a link in your pages
    HTML Code:
    <link rel="shortcut icon" href="favicon.png">

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center