...

View Full Version : PHP echo alternative for html



hackerzlab
02-16-2012, 04:58 PM
the reason why i'm asking this is question is my php echo tags gets nullified by the CMS m currently using.


<?php

error_reporting(E_ALL & ~E_NOTICE);

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

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

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];

}

?>

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
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 :


<img src="hello.php" />

mlseim
02-16-2012, 05:45 PM
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?


.

hackerzlab
02-17-2012, 11:44 AM
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

<img src="hello.php" /> and <img src="hello.php" />

instead of using:

<?php
require 'hello.php';
?>

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

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

Thank You.

mlseim
02-17-2012, 12:27 PM
Your "hello.php" script would look something like this:

"hello.php":


<?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" />


.

hackerzlab
02-17-2012, 04:41 PM
This works for one image. i'm sorry but then when i try to show two images by using:

<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.

mlseim
02-17-2012, 07:11 PM
" ... since my CMS can't understand php ... "

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

hackerzlab
02-17-2012, 07:21 PM
i'm trying to call the function from within an editor of my CMS. it works on PHP of course.

mlseim
02-18-2012, 05:21 AM
What CMS are you using where you can't use PHP?

hackerzlab
02-18-2012, 06:05 AM
thanx. never mind.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum