PHP gd - watermark - how save image? - php-gd

I have on server the image. I want to make a watermark (text) on image and save this image as a new with other name. My function is:
function watermark($path, $watermark){
$imageURL = $path;
list($width,$height) = getimagesize($imageURL);
$imageProperties = imagecreatetruecolor($width, $height);
$targetLayer = imagecreatefromjpeg($imageURL);
imagecopyresampled($imageProperties, $targetLayer, 0, 0, 0, 0, $width, $height, $width, $height);
$WaterMarkText = $watermark;
$watermarkColor = imagecolorallocate($imageProperties, 191,191,191);
imagestring($imageProperties, 5, 130, 117, $WaterMarkText, $watermarkColor);
imagejpeg ($imageProperties);
imagedestroy($targetLayer);
imagedestroy($imageProperties);
}
where parameters are:
$watermark = $_POST['watermark'];
$path = "/images/$file_name";
When I start the scypt, the image with watermark is creating and displays on screen. My aim is to not display the new image but save in the same folder with name: $file_name_watermark. How can i do this?

From the docs on imagejpeg():
filename
The path to save the file to. If not set or NULL, the raw image stream
will be outputted directly.
So:
imagejpeg ($imageProperties, 'some/path.jpg');

Related

How to optimize the size of instagram images in a stream

I want to optimize the size of the images sent by the Instagram feed of a module on my prestashop shop.
How can I request images in smaller sizes, like 134x134px or 150x150px ...
My origin picture is :
https://scontent-cdt1-1.cdninstagram.com/vp/2c273bea214e232de2a2d6ded00cfd57/5D306C5B/t51.2885-15/sh0.08/e35/c135.0.809.809/s640x640/52890210_803725976651019_6529311483719815543_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com
But i will just, this picture is resized in HTML or CSS from 640x640 to 135x135.
Serving a scaled image could save 91.4KiB (95% reduction).
The imagecopyresampled() PHP function is perfect for this task, here is an example:
<?php
$filename = './test-instagram-orig.jpg';
$dest_filename = './test-instagram-resized.jpg';
$new_width = 150;
$new_height = 150;
$my_instagram_image = file_put_contents($filename, file_get_contents('https://scontent-sjc3-1.cdninstagram.com/vp/9844eb2fd4f4012141feef47d2eb97b2/5D389B06/t51.2885-15/sh0.08/e35/s640x640/10249289_1618492008409419_392402572_n.jpg?_nc_ht=scontent-sjc3-1.cdninstagram.com'));
list($width, $height) = getimagesize($filename);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $dest_filename, 100);
imagedestroy($image_p);
echo '<img src="'.$filename.'" alt="" /> <img src="'.$dest_filename.'" alt="" />';
Voila! Of course you should integrate the following code into your module and make the appropriate changes.

AWS S3 filenaming when using MediaConvert

I am currently uploading files to an Amazon S3 using MediaConvert via Lambda functions. However part of my processing is to create thumbnail images from uploaded videos. To do this I am using the AmazonMediaConvetClient and creating a job request.
However the files that are generated have a suffix applied to them of 0000000 which from what I can gather is in reference to the frame captured.
However I do not want this suffix on the filename. Is there anyway to ensure that the filename created from a video thumbnail is what I specify with no suffix?
var jpgOutput = new Output
{
NameModifier = $"-Medium",
ContainerSettings = new ContainerSettings { Container = ContainerType.RAW },
Extension = "jpg",
VideoDescription = new VideoDescription
{
CodecSettings = new VideoCodecSettings()
{
Codec = VideoCodec.FRAME_CAPTURE,
FrameCaptureSettings = new FrameCaptureSettings()
{
MaxCaptures = 1, Quality = 100
}
},
Height = thumbnail.Height,
Width = thumbnail.Width
}
};
In the code snippet above the file is created as 1-Medium.000000.jpg but I want 1-Medium.jpg

Unable to rotate image in PDFBox

I am trying to change the angle of the image by using matrix.Its a needle image which points the value in a chart.
If i am giving "at.rotate(Math.toRadians(45));" , then its looking like it went inside the page, but i need it in the page.
Here is the code i m trying
//Retrieving the pages of the document
PDPage page = document.getPage(0);
//Creating PDImageXObject object
PDImageXObject needle = PDImageXObject.createFromFile("SpeedometerNeedle-300dpi-ActualSize.png"
,document);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true);
//Drawing the image in the PDF document
// draw 90° rotated, placed on the right of the first image
Matrix at = new Matrix(needle.getHeight() / 4, 0, 0, needle.getWidth() / 4,120, 565);
at.rotate(Math.toRadians(45));
// contentStream.drawImage(needle, 100, 565, needle.getWidth() / 4, needle.getHeight() / 4);
contentStream.drawImage(needle,at);
contentStream.close();
I am using org.apache.pdfbox:pdfbox:2.0.1 version ,So if i am trying to use
AffineTransform at = new AffineTransform(ximage.getHeight() / 2, 0, 0, ximage.getWidth() / 2, x + ximage1.getWidth(), y);
at.rotate(Math.toRadians(90));
contentStream.drawXObject(ximage, at);
Then drawXObject method is shown as deprecated.
So suggest me how to use it.

How to add Code128 Barcode image to existing pdf using pdfbox(1.8.12) with barcode4j library?

I am trying to generate the barcode from barcode4j library(code128bean, other barcode beans) and try to add to the existing pdf. The barcode image is getting created locally using the below code.
//Create the barcode bean
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
//width exactly one pixel
//bean.setCodeset(2);
code128Bean.doQuietZone(false);
//Open output file
File outputFile = new File("D:/barcode4jcod128.png"); //I dont want to create it
OutputStream code128Stream = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
code128Stream, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
code128Bean.generateBarcode(canvas1, "123456");
//Signal end of generation
canvas1.finish();
} finally {
code128Stream.close();
}
My problem is I don't want to create an image and save it locally in filesystem and then add it as image to pdf. I just want to create dynamically i mean just create the barcode image dynamically and add it to the pdf.
How do I set the pagesize (like PDPage.PAGE_SIZE_A4) to the existing PDPages which I retrieved from catalog.getAllPages() method, like (List<PDPage> pages = catalog.getAllPages();)
Can somebody help on this?
Thank you so much for your help Tilman. Here is what i did
public static BufferedImage geBufferedImageForCode128Bean(String barcodeString) {
Code128Bean code128Bean = new Code128Bean();
final int dpi = 150;
code128Bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
code128Bean.doQuietZone(false);
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
//Generate the barcode
code128Bean.generateBarcode(canvas1, barcodeString);
return canvas1.getBufferedImage();
}
// main code
PDDocument finalDoc = new PDDocument();
BufferedImage bufferedImage = geBufferedImageForCode128Bean("12345");
PDXObjectImage pdImage = new PDPixelMap(doc, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(
finalDoc, pdPage, true, true, true
);
contentStream.drawXObject(pdImage, 100, 600, 50, 20);
contentStream.close();
finalDoc.addPage(pdPage);
finalDoc.save(new File("D:/Test75.pdf"));
The barcode is getting created the but it is created in vertical manner. i would like to see in horizontal manner. Thanks again for your help.
1) add an image to an existing page while keeping the content:
BitmapCanvasProvider canvas1 = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0
);
code128Bean.generateBarcode(canvas1, "123456");
canvas1.finish();
BufferedImage bim = canvas1.getBufferedImage();
PDXObjectImage img = new PDPixelMap(doc, bim);
PDPageContentStream contents = new PDPageContentStream(doc, page, true, true, true);
contents.drawXObject(img, 100, 600, bim.getWidth(), bim.getHeight());
contents.close();
2) set the media box to A4 on an existing page:
page.setMediaBox(PDPage.PAGE_SIZE_A4);

fit a image on a background image with wideimage

is it possible to fit image onto a white background image using wideimage and in that way to show the image without deformations?.
Now I have this, this source merges a background image with an uploaded image, but a part of image loses
public function createThumb($pathimg, $img, $id) {
$paththumb = 'img/upload/thumbs/' . $img;
$imagen = WideImage::load($pathimg);
$width = $imagen->getWidth();
$height = $imagen->getHeight();
$newWidth = 427;
$newHeight = ($newWidth / $width) * $height;
$imagen->resize($newWidth, $newHeight, 'fill')->saveToFile($paththumb);
$img1 = WideImage::load(realpath($paththumb));
$watermark = WideImage::load(realpath('img/bg.jpg'));
$new = $img1->merge($watermark, 0, 0, 20);
$new->saveToFile($paththumb);
}