开发者

How do I allow images in the HTMLPurifier?

I want to allow images w开发者_开发百科ithin my HTML Purifier filter. Unfortunately they are still being filtered. WHat is wrong with this code?

$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$config->set('URI.DisableExternalResources', false);
$config->set('URI.DisableResources', false);
$config->set('HTML.Allowed', 'u,p,b,i,span[style],p,strong,em,li,ul,ol,div[align],br,img');

Thanks for your help.


You need to allow the src and alt attributes. HTML Purifier should probably warn you if you fail to allow a required attribute for an element/


adding this line of code to yours should fix the problem:

$config->set('HTML.AllowedAttributes', 'src, height, width, alt');


To accept b64 images you must add a new URIScheme:

 if(!class_exists("HTMLPurifier_URIScheme_data")){
        class HTMLPurifier_URIScheme_data extends HTMLPurifier_URIScheme {

            public $default_port = null;
            public $browsable = false;
            public $hierarchical = true;

            public function validate(&$uri, $config, $context) {
                return true;
            }

        }
    }

HTMLPurifier_URISchemeRegistry::instance()->register("data", new HTMLPurifier_URIScheme_data());

Source: http://htmlpurifier.org/phorum/read.php?3,4316,4318,quote=1

"Nick", the guy who made this code, said that had to set:

$browsable = true;
$hierarchical = false;

But in my case i didn't.


HTML Purifier seek http:// or https:// after src=" which may be missing while posting data (inline src="data:image/png;base64,.... ) or your link. Hope this will work for you too as it worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜