Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Need a PHP doctor...
Hi folks ;)
I need to preload all my images for a website, in purpose to speed up it, and try this :
<txp:php> $rep = hu.'img'; if ($dir = opendir($rep)) { while (false !== ($file = readdir($dir))) { echo '<style type="text/css">'; echo '#preloadedImages {'; echo 'width: 0px;'; echo 'height: 0px;'; echo 'display: inline;'; echo 'background-image: url('.$rep.$file.');'; } } closedir($dir); </txp:php>
This PHP rule come from PHP.net reference website… but it’s don’t work at all.
So do somebody know a method to list all images within a directory?
Thanks lot by advance for your helpfull answer.
Best regards,
Last edited by Pat64 (2008-01-06 17:11:20)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
#2 2008-01-06 17:26:40
- net-carver
- Archived Plugin Author
- Registered: 2006-03-08
- Posts: 1,648
Re: Need a PHP doctor...
Patrick
that won’t work as it takes your site URL (that’s the hu
bit) and tries to use it as a file path. Try this instead…
<txp:php>
global $prefs;
$rep = $prefs['path_to_site'].DS.'images'; # change to point to your images directory
if ($dir = opendir($rep))
{
while (false !== ($file = readdir($dir)))
{
echo '<style type="text/css">';
echo '#preloadedImages {';
echo 'width: 0px;';
echo 'height: 0px;';
echo 'display: inline;';
echo 'background-image: url('.$rep.$file.');';
}
}
closedir($dir);
</txp:php>
It’s untested and very late here so apologies if it doesn’t quite make the grade.
Last edited by net-carver (2008-01-06 17:35:43)
— Steve
Offline
Re: Need a PHP doctor...
Thanks Steve.
I’ll try your code, but before, don’t forget to sleep!
Cheers,
Edit : Thanks lot again, I understand the use of hu now… Tks mate!
Last edited by Pat64 (2008-01-06 17:36:04)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Need a PHP doctor...
Oops. I try it and…it’s doesn’t work. I’ve got only 2 points insteed images files.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Need a PHP doctor...
Patrick,
<txp:php>
echo '<style type="text/css">';
global $prefs;
$rep = $prefs['path_to_site'].DS.'img';
$i = 0;
if ($dir = opendir($rep)) {
while (false !== ($file = readdir($dir))) {
if($file != "." && $file != "..") {
$i = $i+1;
echo '#preloadedImages .a'.$i.' {';
echo 'width: 0px;';
echo 'height: 0px;';
echo 'display: inline;';
echo 'background-image: url('.$rep.'/'.$file.');';
echo '}';
}
}
}
closedir($dir);
echo '</style>';
</txp:php>
Fixed the code. Also made it to strip those dot out of it, as they are caused by filesystem.
PS. made sure that the directory is img and correct ;)
Cheers!
Last edited by Gocom (2008-01-06 19:20:58)
Offline
#6 2008-01-06 19:34:25
- guiguibonbon
- Member
- Registered: 2006-02-20
- Posts: 296
Re: Need a PHP doctor...
Are you sure you want all your images to preload ? The css alone is making the page heavier, let alone the impact it has on the brandwith to transfer all those images. But it depends of course on the number of images you uploaded and the traffic you have.
Oh, and currently, it will preload the original image and the thumbnail.
Oh, wait a second, it won’t preload anything because you still need to write all those links in the #preloadedImages span.
Offline
Re: Need a PHP doctor...
Oh, wait a second, it won’t preload anything because you still need to write all those links in the #preloadedImages span.
<txp:php>
echo '<div id="preloadedImages">';
global $prefs;
$rep = $prefs['path_to_site'].DS.'img';
$i = 0;
if ($dir = opendir($rep)) {
while (false !== ($file = readdir($dir))) {
if($file != "." && $file != "..") {
$i = $i+1;
echo '<span class="a'.$i.'"></span>';
}
}
}
closedir($dir);
echo '</div>';
</txp:php>
Oh, and currently, it will preload the original image and the thumbnail.
So do somebody know a method to list all images within a directory?
Cheers!
Last edited by Gocom (2008-01-06 19:57:15)
Offline
Re: Need a PHP doctor...
Jukka, your first code works fine. I’ve got this :
#preloadedImages .a1 {width: 0px;height: 0px;display: inline;background-image: url(/var/www/vhosts/my-site.com/httpdocs/img/image.png);} (...)
Tks lot! Great!
It’s a pitty we can’t put all this styles line into a css style sheets file.
Cheers, mate!
Last edited by Pat64 (2008-01-06 20:10:14)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Need a PHP doctor...
Pat64 wrote:
It’s a pitty we can’t put all this styles line into a css style sheets file.
You could create a file, “styles.php,” with the following:
<?php
header('content-type: text/css; charset=utf-8');
//Jukka's code
?>
html {
background: #fff;
}
/*rest of css*/
Last edited by jm (2008-01-06 20:18:37)
Offline
Re: Need a PHP doctor...
Or you could use ako_cssparse
And place the code inside Textpatterns CSS-mechanism ;). Althought it works absolutely same way as jm’s suggestion, only difference is that then it’s stored inside database, and it can be edited via Textpattern.
Cheers!
Offline
Re: Need a PHP doctor...
ALL
Many thanks for your help :)
Cheers,
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
#12 2008-01-07 05:03:08
- net-carver
- Archived Plugin Author
- Registered: 2006-03-08
- Posts: 1,648
Re: Need a PHP doctor...
Patrick
sorry I couldn’t stay awake to help you out with the initial code. As usual the rest of the gang solved it!
— Steve
Offline