Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts

    PHP echo alternative for html

    the reason why i'm asking this is question is my php echo tags gets nullified by the CMS m currently using.

    PHP Code:
    <?php

    error_reporting
    (E_ALL & ~E_NOTICE);

    // Random Image function
    function random_image($directory)
    {
        
    $leading substr($directory01);
        
    $trailing substr($directory, -11);
        
        if(
    $leading == '/')
        {
            
    $directory substr($directory1);
        }
        if(
    $trailing != '/')
        {
            
    $directory $directory 'images/icons/';
        }
        
        if(empty(
    $directory) or !is_dir($directory))
        {
            die(
    'Directory: ' $directory ' not found.');
        }
        
        
    $files scandir($directory1);
        
        
    $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];

    }

    ?>
    Here's the code below that is used to display the image but in php tag. Is there any alternate way to display image in html format? say by simply calling <img src="image.php"> tag? i am a little lost here. thank you.

    PHP Code:
    <?php
    require 'hello.php';
    ?>
    Random image 1<?php
    echo "<img src=" random_image('') . " />";
    ?>
    Random image 2<?php
    echo "<img src=" random_image('') . " />";
    ?>
    Can anyone help edit the code so that it can be shown with just :

    PHP Code:
    <img src="hello.php" /> 
    Last edited by hackerzlab; 02-16-2012 at 05:39 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Will the image(s) all be the same type ... like .jpg ?

    You'll be using GD.
    Will give example when I know the image type.

    And by using GD like this, you can also resize it
    at the same time, or create a watermark, etc.

    EDIT:
    Also define random.
    Random for each user, random with each page refresh?
    Can the same image appear after a refresh, because it's possible to randomly get the same image first?


    .
    Last edited by mlseim; 02-16-2012 at 06:31 PM.

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    All the images in the folder are .png

    By random i mean, this script shows, image1.png image2.png image1.png image3.png and i'm fine with it. Yes, the same image can appear after refresh - the script already does it.

    i just need to know if its possible to show the image using html tags like
    PHP Code:
    <img src="hello.php" /> and <img src="hello.php" /> 
    instead of using:
    PHP Code:
    <?php
    require 'hello.php';
    ?>

    image 1
    <?php
    echo "<img src=" random_image('') . " />";
    ?>

    image 2
    <?php
    echo "<img src=" random_image('') . " />";
    ?>
    Thank You.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Your "hello.php" script would look something like this:

    "hello.php":
    PHP Code:
    <?php

    // folder name where they are stored.
    $folder_name="images/icons";

    // put these types of images (PNG) into an array.
    $images array_merge(
    glob("$folder_name/*.png")
    );

    // Randomize the array.
    shuffle($images);

    // Display the 1st random image.
    <?php
    $imgPng 
    imageCreateFromPng($images[0]);
    // Output image to browser.
    header("Content-type: image/png");
    imagePng($imgPng);
    // Clear memory
    imagedestroy($imgPng);
    ?>

    When you do this, it should show a random PNG image from your $folder_name.

    <img src="hello.php" />


    .

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    This works for one image. i'm sorry but then when i try to show two images by using:
    PHP Code:
    <img src="hello.php" /> and <img src="hello.php" /> 
    its shows me the same image twice (img1.png). I needed it to be different (img1.png and img2.png) in any sequence.

    Also, the quality of the .png file decreased like as if its some 8bit file. Thank You for your effort. Really appreciate it.

    btw, the original code i posted works just the way i wanted it. I only have issue with the output tag using php tags since my CMS can't understand php.
    Last edited by hackerzlab; 02-17-2012 at 04:44 PM.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    " ... since my CMS can't understand php ... "

    ?? what can your CMS understand? Is it ASP or Perl?

  • #7
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    i'm trying to call the function from within an editor of my CMS. it works on PHP of course.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    What CMS are you using where you can't use PHP?

  • #9
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    thanx. never mind.


  •  

    Posting Permissions

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