Create Images with watermark in extbase - image

I have a Model that stores a FileReference. And now I want to create an Image with a Watermark in the Extbase Controller. Does anybody know where I have to look for? I only found solutions with typoscript.

I choosed the TypoScript way and it worked pretty well.
Fluid:
<f:cObject typoscriptObjectPath="lib.tx_myext.watermarkedImage" data="{imageUid:'{image.uid}',copyright:'{image.copyright}')}'}"/>
TypoScript:
lib.tx_myext.watermarkedImage = IMAGE
lib.tx_myext.watermarkedImage {
file = GIFBUILDER
file {
XY = [10.w],[10.h]
format = jpg
10 = IMAGE
10 {
file {
import.field = imageUid
treatIdAsReference = 1
width = 1162
height = 580c
}
}
20 = BOX
20 {
color = #FFFFFF
opacity = 70
dimensions = [10.w]-[30.w]-10,[10.h]-20,[30.w]+20,[30.h]+20
}
30 = TEXT
30 {
text.data = field:copyright
fontSize = 15
fontColor = #000000
fontFile = path/to/my/font.ttf
offset = [10.w]-[30.w]-5,[10.h]-5
}
}
}
The result is an image with a white box and the copyright text on it at the right bottom corner.

I think the easiest way will be to use a typoscript solution. This must not be as pure typoscript, but the datastructure of the typoscript might be needed as parameter to the core functions of the GifBuilder class. TYPO3 7.6 API
The GifBuilder class is inherited from the class GraphicalFunctions which can be used also, as there are only a few additions and the main functionality is here.
The last problem are the examples in the net according this task: they are pibased and all of them end up in something like
$img = $this->cObj->IMAGE($typoScriptArray);
in modern (namespaced) notation using current API this would be:
$gifCreator = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\GifBuilder::class);
$gifCreator->init();
$theImage='';
if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
$gifCreator->start($fileArray, $typoScriptArray);
$theImage = $gifCreator->gifBuild();
}

Related

Why I am unable to change the QR Code's size in iText7?

How to change QR Code size?
using (iText.Kernel.Pdf.PdfReader _pdf_reader =
new iText.Kernel.Pdf.PdfReader("tmp/example.pdf"))
{
using (iText.Kernel.Pdf.PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(_pdf_reader, new iText.Kernel.Pdf.PdfWriter("tmp/output.pdf").SetSmartMode(true)))
{
BarcodeQRCode qrc = new BarcodeQRCode("https://google.com");
PdfFormXObject xObject = qrc.CreateFormXObject(ColorConstants.BLACK, pdfDoc);
float _w = pdfDoc.GetPage(1).GetPageSize().GetWidth();
float _h = pdfDoc.GetPage(1).GetPageSize().GetHeight();
PdfCanvas canvas = new PdfCanvas(pdfDoc.GetPage(1));
canvas.SaveState();
canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
//canvas.Rectangle(_w - 90, _h - 90, 100, 100);
canvas.Fill();
canvas.RestoreState();
canvas.AddXObject(xObject, _w - qrc.GetBarcodeSize().GetWidth(), _h - qrc.GetBarcodeSize().GetHeight());
}
}
I try:
qrc.GetBarcodeSize().GetHeight();
qrc.GetBarcodeSize().GetWidth();
it returns 33
I try to set Height & Width to 100 like below:
qrc.GetBarcodeSize().SetHeight(100);
qrc.GetBarcodeSize().SetWidth(100);
and then check the size again, but it keeps returning 33, is it a bug? or Did I miss something?
please help
thanks
Don
I try to set Height & Width to 100 like below:
Actually, you can`t change the QrCode side this way.
In fact, QRcode is an n*n grid where n depends on some parameters as a QR code version and the error correction level.
When generating, iText uses the smallest version that can fit the content. This is version 4 (33*33) in your case.
The easiest way to change the size of QrCode in a document is by using the version of the createFormXObject method which accepts the moduleSide parameter.
float moduleSize = 100/qrc.GetBarcodeSize().GetHeight();
qrc.createFormXObject(foreground, moduleSize, document)
Module size here is size of the barcode`s grid cell (1 by default).
iTxt 7 Qrcode size effected by three parameter in hints, example example for your reference.
//C# code
//Prepare all necessary properties to create the qrcode
IDictionary<EncodeHintType, Object> hints = new Dictionary<EncodeHintType, object>();
//default character set (ISO-8859-1)
hints[EncodeHintType.CHARACTER_SET] = "UTF-8";
//Qrcode Error correction level L,M,Q,H
//default ErrorCorrectionLevel.L
hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L;
//Qrcode minimal version level
//default 4
hints[EncodeHintType.MIN_VERSION_NR] = 6;
string code = "Qrcode content here";
BarcodeQRCode qrcode = new BarcodeQRCode(code, hints);

pdfbox - rotation issue

As part of a project I am realizing, there are given pdfdocuments which include forms as JPEG Images within A4 pages inside this documents. If have to extract those JPGs out of the PDF. Later on those JPGs are used to build PDF Documents again.
When I simply open up those Documents with any PDFViewer they seem to have no rotation at all, at least it is not visible. So like this icon the have vertical format.
but when I use this sample code to extract the images :
PDDocument doc = PDDocument.load("/path/to/file);
List pages = doc.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
int i = 0;
while (iter.hasNext()) {
PDPage page = (PDPage) iter.next();
System.out.println(page.getRotation());
System.out.println("ROTATION = " + page.getRotation());;
PDResources resources = page.getResources();
Map pageImages = resources.getXObjects();
if (pageImages != null) {
Iterator imageIter = pageImages.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
if(((PDXObjectImage) pageImages.get(key)) instanceof PDXObjectImage){
PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
image.write2file("/path/to/file" + i);
}
i ++;
}
}
}
all extracted JPGs are horizontal format. Further the sysout on the page.rotation tells me that the rotation is set to 270°.
How is that possible? 270 is set, but the PDF is shown vertical (I am no expert on PDF). I even did page.setRotate(0) before extracting the JPGs, but the images still remain horizontally. I read the following Thread telling how to rotate images before drawing them on the pdf. But i need to rotate them before writing them on the filesystem. What is the best way to achieve that?
Unfortunately, I can not attach any of the documents since they are confidential.

TYPO3: Images are rendered with unwanted Outline/Border

In one case i am rendering all images from all news entries from one specific category inside an unordered list. Each image is wrapped inside an anchor element which itself is wrapped inside a list-item. Each image is 100% white and shall be displayed transparent.
After solving a few transparency related issues i now stand before the problem that ALL images are given an outline which i am not aiming for - since in some cases it might clash with the background (the background Image is changing sometimes). Either i have something missing in my code or something is wrong with the images i am trying to use.
Here is the typoscript code used for rendering the images:
myMarker.20 < plugin.tt_news
myMarker.20
{
code >
code = LIST
templateFile = fileadmin/templates/ext/tt_news/myMarker_template.html
excludeAlreadyDisplayedNews = 0
limit = 6
categoryMode = 1
categorySelection = 5
catImageMode = 0
catTextMode = 1
listOrderBy = title asc
displayList
{
image >
image.stdWrap.cObject = IMAGE
image.stdWrap.cObject.linkWrap = |
image.stdWrap.cObject.linkWrap.insertData = 1
image.stdWrap.cObject.titleText.field = title
image.stdWrap.cObject.file = field:image
image.stdWrap.cObject.file = GIFBUILDER
image.stdWrap.cObject.file
{
format = png
XY = 130, 48
transparentBackground = 1
backColor = transparent
10 = IMAGE
10
{
offset = 0, 48-[10.h]/2
border = 0
file
{
import = uploads/pics/
import.data = field:image
import.listNum = 0
import.override.field = image
maxW = 130
maxH = 48
quality = 100
}
}
}
}
} # myMarker
Maybe someone has an idea? Thanks for reading/help in advance anyway :)
Solved by not using gifbuilder.

Corona SDK: The TabBar Doesn't Load #2x Images..?

Why the tabbar doesn't load '#2x' image automatically on iPhone4 or 5?
Here is my code:
local function init()
--Create a group that contains the entire screen and tab bar
mainView = display.newGroup()
--Create a group that contains the screens beneath the tab bar
tabView = display.newGroup()
mainView:insert(tabView)
loadScreen("info-moregames")
tabBar = viewController.newTabBar{
background = "UI_INFO/tabBar.png", --tab bar background
default = {"UI_INFO/pogi_no.png","UI_INFO/noads_no.png","UI_INFO/share_no.png","UI_INFO/star_no.png","UI_INFO/restore_no.png","UI_INFO/back_no.png"},
over = {"UI_INFO/pogi_yes.png","UI_INFO/noads_yes.png","UI_INFO/share_yes.png","UI_INFO/star_yes.png","UI_INFO/restore_yes.png","UI_INFO/back_yes.png"},
tabs = {"More", "No Ads", "Share","Rate us","Restore","back"}, --names to appear under each tab icon
onRelease = showScreen --function to execute when pressed
}
mainView:insert(tabBar)
tabBar.selected()
return true
end
If you have a config.lua file like above, your images will be automaticaly scaled.
application = {
content = {
width = ***,
height = ***,
scale = "letterBox",
}
If you have a config.lua file like above, you can use different images for different resolutions. Note that, you should have two images for that. For example, you have an image temp.png which is 20x20. Then you should have another temp#2x.png which is 40x40. In this way you can get better images with better resolution. And also, 1.5 value means that if the device's screen is 1.5 times more then your desired resolution, use images with #2x ending.
application = {
content = {
width = ***,
height = ***,
scale = "letterBox",
imageSuffix = {
["#2x"] = 1.5,
}
},
}

Rendering smallest possible image size with MVC3 vs Webforms Library

I am in the process of moving a webforms app to MVC3. Ironically enough, everything is cool beans except one thing - images are served from a handler, specifically the Microsoft Generated Image Handler. It works really well - on average a 450kb photo gets output at roughly 20kb.
The actual photo on disk weighs in at 417kb, so i am getting a great reduction.
Moving over to MVC3 i would like to drop the handler and use a controller action. However i seem to be unable to achieve the same kind of file size reduction when rendering the image. I walked through the source and took an exact copy of their image transform code yet i am only achieving 230~kb, which is still a lot bigger than what the ms handler is outputting - 16kb.
You can see an example of both the controller and the handler here
I have walked through the handler source code and cannot see anything that is compressing the image further. If you examine both images you can see a difference - the handler rendered image is less clear, more grainy looking, but still what i would consider satisfactory for my needs.
Can anyone give me any pointers here? is output compression somehow being used? or am i overlooking something very obvious?
The code below is used in my home controller to render the image, and is an exact copy of the FitImage method in the Image Transform class that the handler uses ...
public ActionResult MvcImage()
{
var file = Server.MapPath("~/Content/test.jpg");
var img = System.Drawing.Image.FromFile(file);
var sizedImg = MsScale(img);
var newFile = Server.MapPath("~/App_Data/test.jpg");
if (System.IO.File.Exists(newFile))
{
System.IO.File.Delete(newFile);
}
sizedImg.Save(newFile);
return File(newFile, "image/jpeg");
}
private Image MsScale(Image img)
{
var scaled_height = 267;
var scaled_width = 400;
int resizeWidth = 400;
int resizeHeight = 267;
if (img.Height == 0)
{
resizeWidth = img.Width;
resizeHeight = scaled_height;
}
else if (img.Width == 0)
{
resizeWidth = scaled_width;
resizeHeight = img.Height;
}
else
{
if (((float)img.Width / (float)img.Width < img.Height / (float)img.Height))
{
resizeWidth = img.Width;
resizeHeight = scaled_height;
}
else
{
resizeWidth = scaled_width;
resizeHeight = img.Height;
}
}
Bitmap newimage = new Bitmap(resizeWidth, resizeHeight);
Graphics gra = Graphics.FromImage(newimage);
SetupGraphics(gra);
gra.DrawImage(img, 0, 0, resizeWidth, resizeHeight);
return newimage;
}
private void SetupGraphics(Graphics graphics)
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
}
If you don't set the quality on the encoder, it uses 100 by default. You'll never get a good size reduction by using 100 due to the way image formats like JPEG work. I've got a VB.net code example of how to set the quality parameter that you should be able to adapt.
80L here is the quality setting. 80 still gives you a fairly high quality image, but at DRASTIC size reduction over 100.
Dim graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage)
graphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
graphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
graphic.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality
graphic.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
graphic.DrawImage(sourceImage, 0, 0, width, height)
' now encode and send the new image
' This is the important part
Dim info() As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
Dim encoderParameters As New Drawing.Imaging.EncoderParameters(1)
encoderParameters.Param(0) = New Drawing.Imaging.EncoderParameter(Drawing.Imaging.Encoder.Quality, 80L)
ms = New System.IO.MemoryStream
newImage.Save(ms, info(1), encoderParameters)
When you save or otherwise write the image after setting the encoder parameters, it'll output it using the JPEG encoder (in this case) set to quality 80. That will get you the size savings you're looking for.
I believe it's defaulting to PNG format also, although Tridus' solution solves that also.
However, I highly suggest using this MVC-friendly library instead, as it avoids all the image resizing pitfalls and doesn't leak memory. It's very lightweight, free, and fully supported.

Resources