Insert image into Primefaces editor - image

I'm using JSF2.2, Tomcat 8 and MySQL DB to create a simple CMS. In my back pages I use Primefaces p:editor to create/edit my page content. I would like to insert an image somewhere into the text. It would be great if I could upload the image and insert it at the current cursor position. Ideally, the image should be saved as a blob, but if it's easier for the implementation it could instead be stored as a local file.
I can see that p:editor already has an option to insert a URL of an image, resulting in the <img> tag in the text. But I would really like the possibility to upload an image from my local drive. I haven't been able to Google anything useful so far.

this is how i did it:
.xhtml:
<p:editor id="editor"
widgetVar="editWidget"
value="#AnnouncementBean.text}" />
<p:fileUpload id="upload"
fileUploadListener="#{AnnouncementBean.uploadListener}"
label="Insert image"
mode="advanced"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
update="editor, growl">
</p:fileUpload>
An in my Backing Bean in the uploadListener after file upload:
String value = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap().get("editor_input");
setText(value + "<img src=\"/uploads/" + result.getName()+"\" />");
RequestContext.getCurrentInstance().update("editor_input");
where "editor_input" referes to the actual form field submitted for the editor (PF adds the_input to the id of your editor)
Notice how I update the editor_input not the actual editor. Only problem now is that the image is appended to the end of the text. sou you must move it manually within the editor

You can use a string to receive the editor (or textEditor) value, then use regex to find all img elements.
This is my code.
String regex="<img src="data:image/(gif|jpe?g|png);base64,([^"]*")[^<>]*>";
Matcher m = Pattern.compile(regex).matcher(your_textEditor_value);
while(m.find()){
String imageFileType=m.group(1);
String imageDataString=m.group(2);
byte[] imageData=Base64.decodeBase64(imageDataString);
}
The imageFileType is your file type, and imageData is data of the file. You can use it to do other thing.

Related

Thymeleaf parse text and execute in-text expressions

I have text string, that contains links, for example, like <a th:href="'someLink'">Download</a> .
I need to process that text and replace th:href="'someLink'" with correct links to show text with Download.
The text with links is stored in variable textThatContainsLinks.
My code to show text is <div th:utext="${textThatContainsLinks}">. I also tried to use preprocessing like <div th:utext="${__textThatContainsLinks__}">.
Currently this code shows links not as I expected, but non-preprocessed, ie, output is <a th:href="'someLink'">Download</a> now.
How to pre-process expressions in text, before showing it?
Thank you very much!
Take the context path and directly attach it to the relative path of a pure html5 attribute e.g LINK, <img src="/contextPath/relative/path/image.jpg" width="50" height="50" alt="logo"/>.
Notice how simple the accessibility to the resource is: /contextPath/relativePath, so the most important path there is the relative path. This is similar to Thymeleaf was unable to render <img> tag when sent from database table. I observed that once thymeleaf's namespace th: qualifies a href or src attribute that resides inside a text/String the absolute path is not properly resolved.

How to add and render multiple images/files in typo3 Fluid content element(flux)?

I am using flux and fluid content element for making content editable by user. i added field for image which allows multiple images to upload.
But now i am not able to show these images.
my value of field image is like :
image => 'kip.jpg,772_Visteon_010.jpg'
normally if i have only one value then i can show it by <f:image> or <img src="{image}" /> Tag.
so, anybody have idea how can i display multiple images or files.
Thanks in advance.
you seem to use the old style of image inclusion: comma separated list of names with copies below uploads/.
Then you need to split (https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Split/Index.html) the field and work the resulting array as before.
in the long time you should use FAL, so the handling is a little bit more complex
You should use flux inline fal for multiple images in fluid content element,
below is a syntax for inline fal,
<flux:field.inline.fal name="settings.image" label="Image" />
After that you can render it by following code,
<f:for each="{v:content.resources.fal(field: 'settings.image')}" as="image">
<f:image treatIdAsReference="1" src="{image.id}" title="{image.title}" alt="{image.alternative}"/><br/>
</f:for>
you can find detail in below url,
https://fluidtypo3.org/viewhelpers/flux/master/Field/Inline/FalViewHelper.html
hope this will help you.

How to search the contents in the string With CKEditor?

there is a label applied CKEditor on my page, HTML code as follows:
<textarea id="MyHtmlEditor" class="ckeditor" cols="80" rows="10" name="MyHtmlEditor"></textarea>
When I upload and then insert a picture, you can see the pictures in normal CKEditor, the viewing source will appear <img alt = "" src = File Name /> tag code.
Because it will put more than one picture, if I want to do in a certain period of time to execute javascript, you can get all the content in the editing area where CKEditor <img alt = "" src = filename /> src attribute values
That is made all the pictures of connecting position.
I found a CKEditor stall, it seems that only the editorial content replace () method can be used.
Some people know how to reach this level?

Paste image URL to html text box instead of 'browsing for file'

How can I convert the following into an editable text box, with a submit button to carry out the code below?
<input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" />
The use case is that an image URL is in the memory, and just needs to be pasted for submission, instead of choosing a local file. (since the OS grabs the remote file, makes a local copy of it then passes this on)
I'm essentially tweaking this element from a nice jcrop tutorial:
http://www.script-tutorials.com/html5-image-uploader-with-jcrop/
Many thanks!

Fancybox shows blob as string not image (Ruby issue?)

I am displaying an image obtained from a database (stored as a long blob) in an img tag using the following method:
<img src="http://localhost:3000/show_image/265" />
The show_image function takes the image data from the database and renders it to the img tag using the send_data method.
When applying Fancybox onto the img tag, the data is displayed as BLOB data not the image..
Can anyone suggest a reason why? Or how I can solve this?
1) You have to apply fancyBox to the links (not the images) and the structure should be like this -
<a class="fancybox" href="big_image"><img src="small_image" /></a>
Example - http://jsfiddle.net/CYCeM/.
Well, it is actually possible to have only images, but then you have to use "data-fancybox-href" attribute to specify the large one - http://jsfiddle.net/6ZSWB/
2) Looks like the script would not be able to guess the content type from your hrefs. You have to either -
a) Create links having extension, e.g., "http://localhost:3000/show_image/265.jpg"
b) Set content type for fancybox, example - $('.fancybox').fancybox(type : 'image');
setting :type => "image/jpg" fixed the issue. I looked up the MIME content types and realised it was incorrectly written.

Resources