Page 1 of 1

Optimizing this code...

Posted: Mon May 22, 2006 11:03 am
by powerPT
I have this code into a site, in every pages of them but it causes slow down.

Code: Select all

<?php
//Copyright by powerPT
$url_rand = array();
$url = "http:/www.xpto.com/";
$path = "/home/xpto/phpBB2/garage/upload";
$cnt=0;
foreach (glob("$path/garage_gallery-*_thumb.jpg") as $nome) {
$aux = "$nome";
$url = substr($aux,17,strlen($aux)-1);
$cnt = $cnt +1;
$url_rand[$cnt] = $url;
}
$valor = array_rand($url_rand,1);
echo "<img src=\"../../../../" . $url_rand[$valor] . " \" width=\"150\" height=\"112\" alt=\"\" />";
exit();
?>
This code shows a image from phpBB garage, from it folder...

I don´t have any ideia to solve this problem :roll:
A friend told me to improve the speed by save the image and just to show every hour or even per day. This can be a good solution but I don´t know how implement them, any ideia is appreciated . :oops:

Posted: Mon May 22, 2006 12:51 pm
by psychotomus

Code: Select all

<?php 
//Copyright by powerPT 
$url_rand = array(); 
$url = "http:/www.xpto.com/"; 
$path = "/home/xpto/phpBB2/garage/upload"; 
$cnt=0; 
foreach (glob("$path/garage_gallery-*_thumb.jpg") as $nome) { 
$aux = "$nome"; 
$num = strlen($aux); //added
$num1 = $num - 1; //added
$url = substr($aux,17,$num1); 
$cnt = $cnt +1; 
$url_rand[$cnt] = $url; 
} 
$valor = array_rand($url_rand,1); 
echo "<img src=\"../../../../" . $url_rand[$valor] . " \" width=\"150\" height=\"112\" alt=\"\" />"; 
exit(); 
?>
optimized a little bette r=). math equations should be done on there own lines.