News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Easily and transparently use default images for missing images...

Started by [Unknown], December 28, 2004, 08:25:19 PM

Previous topic - Next topic

[Unknown]

Sources/Subs.php:
// Start up the session URL fixer.
ob_start('ob_sessrewrite');


Add below:


function ob_check_images($buffer)
{
global $settings;

if (@version_compare(PHP_VERSION, '4.2.3') == -1)
return $buffer;

preg_match_all('~<img[^>]+?src="' . preg_quote($settings['actual_theme_url'] . '/images', '~') . '(/[^"]+?)"~', $buffer, $matches, PREG_SET_ORDER);

$replace = array();
foreach ($matches as $match)
{
if (!file_exists($settings['actual_theme_dir'] . '/images' . $match[1]))
$replace[$match[0]] = strtr($match[0], array($settings['actual_theme_url'] . '/images' . $match[1] => $settings['default_theme_url'] . '/images' . $match[1]));
}

return strtr($buffer, array_unique($replace));
}
ob_start('ob_check_images');


Make sure your theme's images URL is not set to the default's images directory.

-[Unknown]

Anguz

Maybe an array_unique would be good on $matches before doing all the file_exists.

It'll add some time to the page generation, but it's a nice safety net for those missing images in themes.

Thanks [Unknown]. :)
Cristián Lávaque http://cristianlavaque.com

Anguz

Thinking about it, to avoid the preg functions on the whole buffer, why not do something like get all the image file names from the custom theme's dir, compare the list to the default theme's, then eliminating matches you'll be left with the missing images to do the strtr.
Cristián Lávaque http://cristianlavaque.com

[Unknown]

Quote from: Anguz on December 28, 2004, 09:00:16 PM
Thinking about it, to avoid the preg functions on the whole buffer, why not do something like get all the image file names from the custom theme's dir, compare the list to the default theme's, then eliminating matches you'll be left with the missing images to do the strtr.

Regular expressions may not be fast, but they still mop file access up and down.

-[Unknown]

Anguz

True, but I didn't mean to check the directory every time. It could probably be stored in the themes table or something, a CSV of the images in a theme. Then it's a quick thing, you could strpos the CSV in that function instead of file_exists, or something like that.
Cristián Lávaque http://cristianlavaque.com

Elijah Bliss


mytreo

Treo forum - Powered by SMF, of course
Treo news - powered by MovableType and integrated with SMF
Treo downloads - hacked from phpNuke and integrated with SMF
Treo knowledge base - powered by Wikka and integrated with SMF
Treo 650 | Treo 700w | Treo 700p

Anguz

Quote from: Elijah Bliss on December 30, 2004, 07:15:27 PM
What does this modification do exactly?

If you have a missing image file in your custom theme, it'll fix the img tag to use the one in the default theme, so that you don't get the broken image in your page.
Cristián Lávaque http://cristianlavaque.com

[Unknown]

Sorry, I screwed up the subject.  Does it make more sense now?

-[Unknown]

Elijah Bliss

Quote from: [Unknown] on December 30, 2004, 07:28:51 PM
Sorry, I screwed up the subject.  Does it make more sense now?

-[Unknown]

Now it makes sense. Cool mod.

Gregor

Works very nice, but can it be that now on every page laod all images are loaded again, and not fetched from the browser catch? Even the images that are not missing?


[Unknown]

What do you mean?  This doesn't affect how the browser caches things.

-[Unknown]

Gregor

Quote from: [Unknown] on May 01, 2005, 09:27:09 PM
What do you mean?  This doesn't affect how the browser caches things.

-[Unknown]

Yes, sorry, I made some tests, and it is obviously the same with or without the mod. I had the impression that even when a page is in the browser cache the images are reloaded.  ::)

So, it is only left to say that it works fine!

edi67

CrazyZone - My SMF Forum


From the difficult the hardening of the man you can see

Rumbaar

What did you get when you tried to add the code to your current post.php file edi67?

I would suggest you upgrade to 1.1.4 first, there have been a number of security fixes between your version and the current one.
"An important reward for a job well done is a personal sense of worthwhile achievement."

[ Themes ]

Advertisement: