Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Thema gestartet von: [Unknown] in Dezember 28, 2004, 08:25:19 NACHMITTAGS

Titel: Easily and transparently use default images for missing images...
Beitrag von: [Unknown] in Dezember 28, 2004, 08:25:19 NACHMITTAGS
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]
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: Anguz in Dezember 28, 2004, 08:56:07 NACHMITTAGS
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]. :)
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: Anguz in Dezember 28, 2004, 09:00:16 NACHMITTAGS
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.
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: [Unknown] in Dezember 29, 2004, 11:11:20 VORMITTAG
Zitat von: Anguz in Dezember 28, 2004, 09:00:16 NACHMITTAGS
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]
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: Anguz in Dezember 29, 2004, 02:12:53 NACHMITTAGS
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.
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: Elijah Bliss in Dezember 30, 2004, 07:15:27 NACHMITTAGS
What does this modification do exactly?
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: mytreo in Dezember 30, 2004, 07:19:08 NACHMITTAGS
Elijah I was wondering the very same.

Chris
Titel: Re: Easily and transparently use the default theme for default images...
Beitrag von: Anguz in Dezember 30, 2004, 07:25:39 NACHMITTAGS
Zitat von: Elijah Bliss in Dezember 30, 2004, 07:15:27 NACHMITTAGS
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.
Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: [Unknown] in Dezember 30, 2004, 07:28:51 NACHMITTAGS
Sorry, I screwed up the subject.  Does it make more sense now?

-[Unknown]
Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: Elijah Bliss in Dezember 30, 2004, 07:32:40 NACHMITTAGS
Zitat von: [Unknown] in Dezember 30, 2004, 07:28:51 NACHMITTAGS
Sorry, I screwed up the subject.  Does it make more sense now?

-[Unknown]

Now it makes sense. Cool mod.
Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: Gregor in Mai 01, 2005, 09:20:58 NACHMITTAGS
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?

Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: [Unknown] in Mai 01, 2005, 09:27:09 NACHMITTAGS
What do you mean?  This doesn't affect how the browser caches things.

-[Unknown]
Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: Gregor in Mai 01, 2005, 09:58:40 NACHMITTAGS
Zitat von: [Unknown] in Mai 01, 2005, 09:27:09 NACHMITTAGS
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!
Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: edi67 in November 08, 2007, 10:15:19 VORMITTAG
how make it work with smf 1.1.1 ?
Titel: Re: Easily and transparently use default images for missing images...
Beitrag von: Rumbaar in Dezember 09, 2007, 04:47:27 VORMITTAG
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.