Marc
Marc

Reputation: 377

How do I allow images in the HTMLPurifier?

I want to allow images within 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.

Upvotes: 1

Views: 4482

Answers (4)

FrakyDale
FrakyDale

Reputation: 727

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.

Upvotes: 1

Santosraj
Santosraj

Reputation: 61

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.

Upvotes: 0

Stoffo
Stoffo

Reputation: 177

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

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

Upvotes: 2

Edward Z. Yang
Edward Z. Yang

Reputation: 26762

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/

Upvotes: 2

Related Questions