String "unsafe" comes from contenteditable="true" div to where it was pasted as image from clipboard
// neeeds to be escaped. It is HTML5 valid
String unsafe = ""<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABaklEQVQokZWSXYuCQBSG+xdCa5nRRZgzUlmDNlmCxSCjZg4Y9OkwMxf9Sn/aXrRERrC779U5L+fhfHBa9T/Vek3m8/kztm3bcRzXdTebjed50+m0AWy3W4RQXdcAgCcAIcyyTEpZlqVlWQ1ASnm9XpMksW3bsiwAAISQECKEuN/vlNL1et0ALpeLUkoIked5EAS+7xNCqqpSSlFKwzB838FxnCiKzuezEOJ4PJZlebvdOOdxHGOMPyytaRqEEGPMGOOcSymVUoyx5+gfrjQYDB5zn04nznlVVYyxyWRimuYHoN/vD4fD5XKZpmlRFGma7na7w+GQ5zlCaDQavQOu64ZhmCRJlmUYYwDAeDwOw7Aoiv1+v1gsHMdpAIQQSiml1Pd9wzC63a5hGL1eDyEURVEQBJ1OpwFQSuM49jxP13Vd1x+maZpfL2oAq9VqNpu1221N097O8lpdv/3Sj9X6YDaA1p/V6PBr6UPfrxpWT8DSD68AAAAASUVORK5CYII=" alt="">
"
org.jsoup.safety.Whitelist whitelist = Whitelist.relaxed();
whitelist.addEnforcedAttribute("a", "rel", "nofollow");
String safe = Jsoup.clean(unsafe, whitelist);
//and safe becomes: "<img alt="">"
//entire src lost !?
Note: randome surrouning html has no effect. Src is lost in any case.
The basic problem here is that if one quick looks at relaxed here:
http://jsoup.org/apidocs/org/jsoup/safety/Whitelist.html#relaxed
assumes only tags are in, without attributes. Did not look into source, but here claims some attributes are also in: How to make a Jsoup whitelist to accept certain attribute content.
And image is also already in and src also.
the problem that causes my src to disapear is at
preserveRelativeLinks
Which is set to false,for relaxed, hidden somewhere in JSoup code
https://github.com/jhy/jsoup/issues/333
--> should be set to true:
System.out.println(Jsoup.clean("<img src='imgFile.png' />","http://www.somedomain.com", Whitelist.relaxed().preserveRelativeLinks(true)));
This is how to allow basic text with inline images like src="data:image/png;base64,...":
String safe = Jsoup.clean(unsafe, Whitelist.basic()
.addTags("img")
.addAttributes("img", "height", "src", "width")
.addProtocols("img", "src", "http", "https", "data"));
Related
I used to ask this question a few years ago, and back then it was not possible to apply a style to all images when exporting to docx using Pandoc.
Today, I wonder whether this still is true? I tried a bit using Pandoc 2.9.x, and images still seem to have applied the generic "Normal" style:
It would be nice to have a style like "Picture" which is applied to each image in the docx. Any way to do this?
Images by themselves cannot have any style, you have to wrap an image into a block, that can have custom attributes. I replace figure elements with a pair of an image and a caption, something like this:
funFigure = function(element)
local attrfigure = pandoc.Attr( "", { }, {{"custom-style", "Picture" }} )
local attrfiguretext = pandoc.Attr( "", { }, {{"custom-style", "Picture Caption" }} )
local figuretext = pandoc.Div(pandoc.Para(element.content[1].caption), attrfiguretext)
element.content[1].caption = pandoc.Null
local figure = pandoc.Div(element, attrfigure)
return { figure, figuretext }
end
So this is really frustrating... on the mymaths website: https://www.mymaths.co.uk/, there's an image of a primary school child on a computer with this image address: https://www.mymaths.co.uk/assets/images/big/primary-school-photo-2.jpg.
I've tried so many things, but I can't seem to replace it.
Say I wanted to replace it with a picture of a hamburger, with this address: https://images.ctfassets.net/sd2voc54sjgs/5L6livQvCw28S04IUSAcm6/6482ea1819e86be1b4f7e85bfbbfe9a6/Blog_Header_Hamburger_History_Option.png?fm=jpg&q=80&fl=progressive&w=1100.
So far I've tried lots of threads, but this image seems to be different from images on other websites, which is why my code isn't working on it:
var images3 = document.getElementsByTagName ("img");
var i3=0;
while(i3<images3.length)
{
if(images[i3].src == "https://www.mymaths.co.uk/assets/images/big/primary-school-photo-2.jpg")
{
images[i3].src = "https://images.ctfassets.net/sd2voc54sjgs/5L6livQvCw28S04IUSAcm6/6482ea1819e86be1b4f7e85bfbbfe9a6/Blog_Header_Hamburger_History_Option.png?fm=jpg&q=80&fl=progressive&w=1100";
}
i3=i3+1;
}
Can somebody help me please? Thank you.
Well, this was new for me too. Apparently, the <picture> tag is not just a wrapper - it's a smarter version of <img>.
It allows to chose different URLs for the image tag depending on screen size and type. For example, try to do this in developper tools:
I replaced srcset for the <source> that has (max-width: 767px), which means it is active when browser window is smaller than 767px. Now if you resize browser window to make it smaller, at some point the original image will be raplaced with burger image.
So what you want to do is to replace all <source>'s srcset. This worked for me:
// Limit the list of omages on those that are under `<picture>` tag
const images = document.querySelectorAll("picture img, picture source");
// RegExp to check if we want to replace the URL
const replaceChecker = /primary-school-photo-2\.jpg$/i;
// The replacement URL
const replaceWith = "https://images.ctfassets.net/sd2voc54sjgs/5L6livQvCw28S04IUSAcm6/6482ea1819e86be1b4f7e85bfbbfe9a6/Blog_Header_Hamburger_History_Option.png?fm=jpg&q=80&fl=progressive&w=1100";
for(const image of images) {
// Pick the name of the attribute we want to change based on whether it's <img> or <source>
const srcAttributeName = image.tagName.toLowerCase() == "img" ? "src" : "srcset";
const oldURL = image[srcAttributeName] + "";
if(replaceChecker.test(oldURL)) {
image[srcAttributeName] = replaceWith;
}
}
You could improve that by checking the media attribute and if it says minimum screen width, use URL for smaller image of the hamburger. That is set by the w GET param in the hamburger image's URL.
I want to add an image in header using TCPDF in my Magento store.
I am doing this:
$tcpdf = new TCPDF_TCPDF();
$img = file_get_contents(Mage::getBaseDir('media') . '/dhl/logo.jpg');
$PDF_HEADER_LOGO = $tcpdf->Image('#' . $img);//any image file. check correct path.
$PDF_HEADER_LOGO_WIDTH = "20";
$PDF_HEADER_TITLE = "This is my Title";
$PDF_HEADER_STRING = "This is Header Part";
$tcpdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);
$tcpdf->Output('report_per_route_'.time().'.pdf', 'I');
What steps I have to follow if I want to add my store name (left corner) and logo (right corner)?
If you are trying to generate the pdf using the WriteHTML() here is a little trick to add image without use of image() function.
Simply use the HTML <img> as below,
$image_path = 'path/to/image';
$print = '<p>some text here...</p>';
$print .= '<img src=" '. $image_path .' ">';
and you can use inline css to apply height, width etc.
TCPDF is tricky about inserting images as HTML. It implements few hacks to tell what is being loaded:
inserting image with src attribute as absolute path - must have star * prefix:
<img src="*/var/www/my-image.png">
inserting image with src attribute as relative path - both examples are treated as relative paths:
<img src="/var/www/my-image.png">
<img src="var/www/my-image.png">
Note, that relative paths are calculated differently on linux and windows - what works correctly on windows may not work well on linux. That is caused by checking first character in a path string as a forward slash /, which is considered a linux root and the path will be recalculated - relative path will append to a global variable DOCUMENT_ROOT.
Loading base-64 encoded string - must have # prefix in src attribute:
<img src="#iVBORw0KGgoAAggfd0000555....">
<img src="#'.base64_encode(file_get_contents($path)).'" width=50 height=35>
This is safe bet if you want to avoid issues with calculating correct path, but adds extra I/O overhead, because TCPDF will attempt to store supplied data as temporary image file in order to determine image width & height.
Ok. First of all $PDF_HEADER_LOGO is suppose to be an image file name, not image data - as in default implementation of Header() function. There is, however, one important thing to remember, exact location depends on K_PATH_IMAGES constant, which should contain path to images folder. If its defined before including TCPDF library its ok, if not TCPDF checks some default paths and first existing is used as images directory. Those directories are:
./examples/images/
./images/
/usr/share/doc/php-tcpdf/examples/images/
/usr/share/doc/tcpdf/examples/images/
/usr/share/doc/php/tcpdf/examples/images/
/var/www/tcpdf/images/
/var/www/html/tcpdf/images/
/usr/local/apache2/htdocs/tcpdf/images/
K_PATH_MAIN (which is root tcpdf folder)
So either define constant before, or put your file to one of above directories, and then pass only file name as first argument to SetHeaderData and it should work.
To have something similar for Footer you need to extend base TCPDF_TCPDF class and overwrite its Footer method.
Example:
class MYPDF extends TCPDF_TCPDF {
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'COMPANY NAME', 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Image('/path/to/image.jpg', 500)
}
}
You'll probably need to work out exact coordinates. Especially in Image it depends on your dimensions, you can add another parameter to Image function being y coordinate, and two others - width and height of image.
And most importantly I recommend checking great examples section on TCPDF page:
http://www.tcpdf.org/examples.php
I'm working on my first TYPO3 project and I'm done with the template, I just can't figure out how to make this work:
My page content is one column with header, text and title in every field.
I don't have any problems showing header and text on my website, but the image just won't work.
My image-path is fileadmin/user_upload/ and I can show it by setting the filename in my code, but thats obviously not what I want.
This is my Content Object Array, the code for the image is one of the versions I found when searching, but none of them worked:
page.20 = CONTENT
page.20.table = tt_content
page.20.wrap = <div id="content">|</div>
page.20.renderObj = COA
page.20.renderObj.wrap = <div id="news">|</div>
page.20.renderObj {
10 = TEXT
10.stdWrap.field = header
10.stdWrap.wrap = <div id="newstitle"><span>|</span></div>
20 = IMAGE
20.stdWrap.field = image
20.stdWrap.wrap = <div id="newsimage><img src="fileadmin/user_upload/|"</img></div>
30 = TEXT
30.stdWrap.field = bodytext
30.stdWrap.wrap = <div id="newstext"><p>|</p></div>
}
Hope someone could help me out, so I can finally finish this project!
Maybe I don't understood correctly, but... you don't need to write it yourself, just in your main TS template include CSS Styled Content from extension, so you can render whole column like:
page.20 < styles.content.get
If you need to change some wrappings (i.e.: to add your news CSS classes) you can also override them in setup field.
edit: Go to Templates > Template analyzer and check the EXT:css_styled_content/static/ code to find declaration you'd like to change (prepare for looong reading ;)) when you finally find it, just you can overwrite it in your TS, ie: to wrap text section of Text w/ image CE you can use:
tt_content.textpic.20.text.wrap = <div class="myNewsClass"> | </div>
Finally I'd suggest not to replace original classes but add your own so the above sample would be like:
tt_content.textpic.20.text.wrap = <div class="csc-textpic-text myNewsClass"> | </div>
de facto all these classes csc- has some CSS default styling therefore you can choose to align the image to the left or right (etc) out of the box. If you'll remove it you'll need to handle these things yourself.
Our site is using tags like <# TAGNAME #> but CKEditor converts < and > to < and > which breaks these tags for use in our software.
I've discovered this option: config.protectedSource.push( /<#[\s\S]*##>/g ); which seems to stop the conversion if the data is saved from Source mode, but in WYSIWYG mode I can't find a way to stop the conversion. I've tried many options in their API but none of them seem to have helped, how can I fix this problem?
Were were looking at using CKEDitor to edit Smarty templates. The problem we were hitting was that it was replacing all the angle brackets and ampersands within the curly brackets, which messed everything up. This came up in a Google search so our solution should help anyone with similar issues.
CKEditor rebuilds the HTML every time you switch to Source mode and when you save, so you need to add to the HTML http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor htmlFilter.
This worked for us:
//replace Form_content with whatever your editor's id is.
htmlParser = CKEDITOR.instances.Form_content.dataProcessor.htmlFilter;
//We don't want HTML encoding on smarty tags
//so we need to change things in curly brackets
htmlParser.onText = function(text) {
//find all bits in curly brackets
var matches = text.match(/\{([^}]+)\}/g);
//go through each match and replace the encoded characters
if (matches!=null) {
for (match in matches) {
var replacedString=matches[match];
replacedString = matches[match].replace(/>/g,'>');
replacedString = replacedString.replace(/</g,'<');
replacedString = replacedString.replace(/&/g,'&');
text = text.replace(matches[match],replacedString);
}
}
return text;
}
The onText function processes all the bits that aren't in tags or comments.
I'd imagine you can do something similar by altering the code above - I've left it as is as I think our problems and required solutions are very similar.
editor.on( 'mode', function(ev) {
if ( ev.editor.mode == 'source' ) {
var str=ev.editor.getData();
str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
ev.editor.textarea.setValue(str);
}
});
http://cksource.com/forums/viewtopic.php?f=11&t=20647&start=10
If you type < or > in any WYSIWYG editor, they will be converted to their HTML entities in source mode.