Fit text in to a shape using gd library - imagettftext

I am trying to put a text of any length (up to 15) in a shape like in below image using gd library. The width of shape should expand/reduce according to the text length. I know how to write simple text using gd library, but i am stuck on how to make this ?
I am trying something similar like this
<?php
// Set the content-type
header('Content-Type: image/png');
$im = imagecreate(400, 300);
// Background color
$white = imagecolorallocate($im, 255, 255, 255);
// font color
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// text string
$text = 'ABCDEFG';
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>
I tried creating text to image... now i want to fit that text in a shape... if the length of text increases the width of the shape has to be increased... and i m stuck on how to do this.
How can i achieve this ? any idea on this ?

Related

Render RGB pointcloud together with surface match result

(HalconDotNET)
I want to render an image from a visualized match result with a colored pointcloud.
In the example program find_surface_model_with_edges_simple.hdev after running find_surface_model() you receive a pose, with this pose you can visualize how the surface model matched in the scene using: visualize_object_model_3d(). From a visualization like this I want to create a rendered image to display the matching result in an application I am making.
To render a colored pointcloud I use:
render_object_model_3d (Image, ObjectModel3DSceneRaw_ccs, camPar, Pose_0, ['red_channel_attrib','green_channel_attrib','blue_channel_attrib'], ['&overlay_red','&overlay_green','&overlay_blue'])
To render a match result I use:
render_object_model_3d (Image, [ObjectModel3DSceneRaw_ccs, ObjectModel3D], camPar, [Pose_0, detectedPose], ['color_0', 'color_1'], ['white', 'red'])
I can not get the two objects in this function and still have the RGB attributes, Halcon gives parameter errors. I would also like to specify the color of the objectmodel.
I also tried to use 3D scene:
create_scene_3d (Scene3D)
add_scene_3d_camera (Scene3D, camPar, CameraIndex)
set_scene_3d_camera_pose (Scene3D, CameraIndex, detectedPose)
add_scene_3d_light (Scene3D, PoseInvert[0:2], 'point_light', LightIndex)
* The scene
add_scene_3d_instance (Scene3D, ObjectModel3DSceneRaw_ccs, detectedPose, InstanceIndex)
set_scene_3d_instance_param (Scene3D, InstanceIndex, ['red_channel_attrib','green_channel_attrib','blue_channel_attrib'], ['&overlay_red','&overlay_green','&overlay_blue'])
* The transformed objectModel
add_scene_3d_instance (Scene3D, ObjectModel3DRigidTrans, Pose_0, InstanceIndex2)
set_scene_3d_instance_param (Scene3D, InstanceIndex2, 'color', 'red')
* Display
display_scene_3d (WindowHandle, Scene3D, CameraIndex)
But this only shows the scene and not the matched objectmodel.
Anyone know what I'm doing wrong?
This hack worked well enough for my purposes:
HT empty = new HT();
hop.CreatePose(0, 0, 0, 0, 0, 0, "Rp+T", "gba", "point", out HT pose_0);
HT camParam = new HT(0.008, 0, 0, 0, 0, 0, 5.2e-006, 5.2e-006, 960, 600, 1920, 1200);
HT renderGenParam = new HT("red_channel_attrib", "green_channel_attrib", "blue_channel_attrib");
HT renderGenValue = new HT("red", "green", "blue");
hop.ReadObjectModel3d(om3Path, 'm', empty, empty, out HT om3, out HT status);
hop.RenderObjectModel3d(out HObject sceneImage, current_OM3, camParam, pose_0, renderGenParam, renderGenValue);
hop.RenderObjectModel3d(out HObject objectImage, om3, camParam, avg_pose, "color_0", "green");
hop.AddImage(sceneImage, objectImage, out HObject resultImage, 0.6, 0);
hop.CropRectangle1(resultImage, out HObject resultCropped, 250, 400, 935, 1450);
hop.WriteImage(resultCropped, "tiff", 0, #"./testImage.tiff");
Not a real solution tho.

Creating a transparency group or setting graphics state soft mask with PDFBox

I have a grayscale image that serves as a soft mask and I want to use it on a group of PDF objects (images or paths).
The mask and the objects do not necessarily use the same transformation matrix, and there might be more than one object to mask, so that excludes the possibility of using the SMask attribute of the ImageXObject dictionary.
So after reading some of the PDF specification, it looks like I should do the following: create a transparency group with the objects to mask, then draw it with the soft mask set on the graphics state.
Will that work? How can I achieve this, preferably with PDFBox?
Here's an example. I have these two images: the mask and another image.
The mask image is 200x200. It is drawn with the matrix [[4 0 100] [0 4 100]].
The image is 400x300. It is drawn with the matrix [[2 0 100] [0 2 150]].
Additionally, a 400x400 black square is drawn below the image with no transform matrix.
So a transparency group is created with the image and the square, then it's drawn with the mask image. Here's the expected result:
Rather ugly as far as the effect goes, but that's just an example.
As far as I can see establishing an extended graphics state soft mask is a very manual task in PDFBox. You can do so as follows:
try ( PDDocument document = new PDDocument() ) {
final PDImageXObject image = RETRIEVE PHOTO IMAGE;
final PDImageXObject mask = RETRIEVE MASK IMAGE;
PDTransparencyGroupAttributes transparencyGroupAttributes = new PDTransparencyGroupAttributes();
transparencyGroupAttributes.getCOSObject().setItem(COSName.CS, COSName.DEVICEGRAY);
PDTransparencyGroup transparencyGroup = new PDTransparencyGroup(document);
transparencyGroup.setBBox(PDRectangle.A4);
transparencyGroup.setResources(new PDResources());
transparencyGroup.getCOSObject().setItem(COSName.GROUP, transparencyGroupAttributes);
try ( PDFormContentStream canvas = new PDFormContentStream(transparencyGroup) ) {
canvas.drawImage(mask, new Matrix(400, 0, 0, 400, 100, 100));
}
COSDictionary softMaskDictionary = new COSDictionary();
softMaskDictionary.setItem(COSName.S, COSName.LUMINOSITY);
softMaskDictionary.setItem(COSName.G, transparencyGroup);
PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
extendedGraphicsState.getCOSObject().setItem(COSName.SMASK, softMaskDictionary);
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
try ( PDPageContentStream canvas = new PDPageContentStream(document, page) ) {
canvas.saveGraphicsState();
canvas.setGraphicsStateParameters(extendedGraphicsState);
canvas.setNonStrokingColor(Color.BLACK);
canvas.addRect(100, 100, 400, 400);
canvas.fill();
canvas.drawImage(image, new Matrix(400, 0, 0, 300, 100, 150));
canvas.restoreGraphicsState();
}
document.save(new File(RESULT_FOLDER, "SoftMaskedImageAndRectangle.pdf"));
}
The result:
If I were you, though, I would not use a bitmap image for the soft mask but instead a PDF gradient. The result most likely will be much less pixelated.

Cut out a custom shape from an image in P5.js

I am trying to create jigsaw puzzle shapes using P5.js. After creating puzzle shapes, I want to cut areas from main image into pieces. For that I have options of using GET() or COPY():
But both of them take fix height and width as parameter. How can I copy a custom area like given in following shapes:
https://editor.p5js.org/techty/sketches/h7qwatZRb
let cutout = createGraphics(w, h);
cutout.background(255, 255);
cutout.blendMode(REMOVE);
//draw shape on cutout
let newshapeimagegraphic = createGraphics(w, h);
newshapeimagegraphic.image(myImg, 0, 0);
newshapeimagegraphic.blendMode(REMOVE);
newshapeimagegraphic.image(cutout, 0, 0);
image(newshapeimagegraphic, 0, 0);

Is an AlphaMaskFilter correct in this case?

I am trying to use an alppha mask filter to apply a texture to a canvas element but cannot seem to get things to work. I have a base image which is a flat white color and to which I want to apply a color filter at runtime based on a users selection for example:
bitmap = new createjs.Bitmap(image);
bitmap.filters = [
new createjs.ColorFilter(0,0, 0.5, 1, 0, 0, 120, 0)
];
bitmap.cache(0, 0, 500, 500, 2);
I then want to use a second image which is a texture png that will add various shading texture to that first one. Looking over the docs it would seem that I need to use an AlphaMaskFilter but that does not seem to work and nothing is rendered onto the canvas. For example:
//filterImage contains the transparent image which has a shaded texture
var bitmap2 = new createjs.Bitmap(filterImage);
bitmap2.cache(0, 0, 500, 500, 2);
var bitmap = new createjs.Bitmap(image);
bitmap.filters = [
new createjs.ColorFilter(0,0, 0.5, 1, 0, 0, 120, 0),
new createjs.AlphaMaskFilter(bitmap2.cacheCanvas)
];
bitmap.cache(0, 0, 500, 500, 2);
Can someone help point me in the right direction here or if I am trying to do something which is just not possible using that filter.

How to add a overlay text on captured video/image in swift?

I want to add a overlay text like "Company watermark" on captured video or image programatically in swift. any help will be appreciated.
// create text Layer
let titleLayer = CATextLayer()
titleLayer.backgroundColor = UIColor.whiteColor().CGColor
titleLayer.string = "Company watermark"
titleLayer.font = UIFont(name: "Helvetica", size: 28)
titleLayer.shadowOpacity = 0.5
titleLayer.alignmentMode = kCAAlignmentCenter
titleLayer.frame = CGRectMake(0, 50, size.width, size.height / 6)
yourView.layer.addSublayer(titleLayer)
Hope it will help you for adding text in video.