ImageMagick and adding a visual watermark - php

I am currently trying to work with ImageMagick and teaching myself as I go.
I have managed to get images to upload and then be converted size wise, and append some text to them, but I ideally need to append a logo to them.
I have been reading through the documentation via the ImageMagick website but feel I am making stupid mistakes now.
This is the code I have for the ImageMagick command, can anyone shed some light on what I actually need to do to fix this?
$cmd = 'mogrify -composite -disolve 25% -gravity southwest watermark_new.png '.$thefile.' '.$thefile.'';
Thanks for all the help guys.
THIS FIXED THE PROBLEM:
$cmd = 'mogrify -gravity southwest -geometry +30+30 -draw "image Over 0,0 '.$width.','.$height.' \''.$watermark.'\'" '.$thefile;

I dont think you need the mogrify part - the command you're actually looking to use is composite - have you tried:
$cmd = 'composite -disolve 25% -gravity southwest watermark_new.png '.$thefile.' '.$thefile.'';
Theres a number of examples here

Related

Error Level Analysis with Imagick PHP - result without color

yesterday I posted about a problem with recreating an Error Level Analysis in PHP with Imagmagick. In this question I found a solution with the command-line interaction and tried to translate it into Imagick and PHP.
The following code was proposed:
convert barn.jpg \( +clone -quality 95 \) -compose difference -composite -auto-level -gamma 1.5 barn_ela.png
In this example, the result should highlight the manipulated parts of the image. So I implemented the following code:
$ELAImageMagick = new Imagick($targetDir . $OriginalImage);
$OriginalImageMagick = new Imagick($targetDir . $OriginalImage);
$ELAImageMagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$ELAImageMagick->setImageCompressionQuality($this->getRequest()->postVar('elaQuality'));
$ELAImageMagick->compositeImage($OriginalImageMagick, Imagick::COMPOSITE_DIFFERENCE, 1,1);
$ELAImageMagick->autoLevelImage();
//Set gamma with a slider in the frontend
$ELAImageMagick->gammaImage($this->getRequest()->postVar('elaSize'));
//save ELA-image into Folder
$ELAImageMagick->writeImage($targetDir . $ELAImage);
Unfortunatly the result does not come close to the desired optic of the result:
The original image (yellow bird has been added in photoshop)
The ELA-Result
Does anyone have an idea, what step I didn't catch quite right and how to solve it? I looked into the documentation and didn't really find any alternatives to this.
Thanks in advance!
You need to first prove that your code works in Imagemagick. But -quality only works for writing a JPG output. So you need to actually save the result of the -quality operation as a JPG and then use it in a second command. Here I just pipe from one convert to another. The first convert actually creates the compressed JPG but just passes it to the second command without writing to disk.
Input:
convert birds.jpg -quality 95 JPG:- | convert birds.jpg - -compose difference -composite -auto-level -gamma 1.5 birds_ela.png
The result I get is:
This is a JPG version of the PNG output since the PNG was too large to post.
However the results from the command line do not match the resulting image you show. So the command is not going to work in Imagick either.
ADDITION:
From what I can see there is no tampering other than possibly the orange bird in the middle. Most of the image is flat or has random noise. The small bird in the middle has a bit brighter texture and so may be an insert.
ADDITION 2
For enhancing edge artifacts, add -geometry +1+1 to my command above just before -compose difference.
convert birds.jpg -quality 95 JPG:- | convert birds.jpg - -geometry +1+1 -compose difference -composite -auto-level -gamma 2 -define jpeg:extent=2000kb birds_ela2.png
Again posting a JPG version for file size issues.
This looks similar to your posted result.

Generate centered thumbnail from first frame of GIF with ImageMagick

I have the following method to generate a thumbnail. It takes any dimensions and converts it to a 600 x 450 image, centered.
private function generateThumbnail ($ext, $name) {
if ($ext === ".png"){
exec('convert -define png:size=1200x900 '.$this->file.' -thumbnail 600x450^ -gravity center -extent 600x450 '.$name);
exec("optipng -o2 -q " . $name);
} else {
exec('convert -define jpeg:size=1200x900 '.$this->file.' -thumbnail 600x450^ -gravity center -extent 600x450 '.$name);
$img = new Imagick($name);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(97);
$img->stripImage();
$img->writeImage($name);
$img->destroy();
}
}
The problem arises when generating a thumbnail from a GIF. It doesn't generate an image from the first frame. One GIF just had a blank image (transparent), another had strange colors/quality.
How do I generate a thumbnail the first frame of a GIF, centered?
You could probably do them with the API but I do not use it as it is limited. But it looks like you still need to use the command line for the optipng program anyway so I would have carried on with the command line.
Anyway back to your problem; how do you know the first frames of the gif are not a transparent image or a strange image?
As #Mark Setchell said you should be able to specify a specific frame with input.gif[0] and I would try that with a different number and see what you get.
I am not an animation use but you should be able to see the gif frames with -coalesce. Anthony has some examples on his site: http://www.imagemagick.org/Usage/anim_basics/#coalesce
This should give you an idea of the frames in the gif:
montage script_k.gif -coalesce \
-tile x1 -frame 4 -geometry '+2+2' \
-background none -bordercolor none coalesce_k_montage.gif
You can see how you gif file is made and perhaps go for the second or third frame rather than the first.

ImageMagick caption in PHP not filling size vertical dimension

I have cobbled together the following code, which very nearly works:
<?php
$img = new Imagick("quote_blank.jpg");
$txt = new Imagick();
$txt->setBackgroundColor("transparent");
$txt->newPseudoImage(380,250, "Caption:".htmlspecialchars($_GET['quote']) );
$txt->colorizeImage('#468847',1);
$img->compositeImage($txt, imagick::COMPOSITE_OVER, 10, 80);
$draw = new ImagickDraw();
$draw->setFillColor('#468847');
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
$draw->setFontSize(25);
$draw->setFontStyle(3);
$img->annotateImage($draw, 5,5,0, htmlspecialchars($_GET['attrib']) );
$img->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $img;
?>
(please note that quote_blank.jpg is a 400x400 image background over which the text is rendered and resides in the same directory as the php file).
The issue is that the caption only fills the 380x250 PseudoImage with a very small number of short words. Anything of any length results in just the top half (or less) of the box having any text in it (aside from the attribution annotation).
It seems like the PseudoImage is working correctly but that ImageMagick's algorithm for calculating the font size is only designed to fill the width, not the height. I have no idea how it decides what line length to go for (which would presumably in turn dictate the font size and therefore number of lines and vertical coverage of the caption box).
So I guess my question is this: Is there any way of changing how it does it's calculations in order to fill as much of the caption box as possible, horizontal AND vertical?
Sample of just a few words, showing the caption can go full-height:
Sample of a more typical length of text, showing it doesn't fill the box vertically
I tested your code with ImageMagick 6.8.9-8 and got the following output, which is better than what you're getting. If you're using an older version, try updating ImageMagick.
Vinicius Pinto had the right answer right off the bat. But updating wasn't so easy on a shared server- I have not figured out how to get Imagick to use the updated version. So I had to rewrite my code to access ImageMagick via the commandline, which I wanted to share. Code doesn't show up well as far as I can tell on a comment, so sorry for cheating the answer feature a little.
$location='/home/user/local/bin/convert';
$command='convert -background none -size 380x250 -fill "#468847" caption:"'.htmlspecialchars($_GET['quote']).'" quote_blank.jpg +swap -gravity southeast -geometry +10+80 -composite convert -fill "#468847" -gravity southeast -pointsize 25 -annotate 0x20+5+5 "'.htmlspecialchars($_GET['attrib']).'" anno_label.jpg';
exec ($location . ' ' .$command);
header('Content-Type: image/jpeg');
readfile('anno_label.jpg');
unlink('anno_label.jpg');

Generate a image thumbnail without stretching it with ImageMagick 6.2.8

Fill Area Flag ('^' flag) is support IM v6.3.8-3But my client's production server has version ImageMagick 6.2.8
Right now in my local server i use this command to generate thumbnail and it works fine:
convert image.jpg -resize "280x210^" -gravity Center -crop "280x210+0+0" thumbnail.jpg
Since my client's production server doesn't support '^' flag how can i generate a thumbnail without using it? (or maybe calculating it manually in PHP or BASH)
Should i use -extent, does it stretch the image?
I also read this and im not sure if ^ flag is for not letting the image stretch because thats what i want, generate a thumbnail without stretching it.
Note: i dont have root access on the server. Im using PHP and BASH to run the commands.
EDIT:
I also don't want any other background colors while resizing and croping.
try
convert image.jpg -background black -resize 280
-gravity center -crop 280x210+0+0 -extent 280x210 image.c.jpg
I found a solution
This is the PHP function i used:
function imgconvert($in,$out,$size){
$size_arr=explode('x',$size);
$resize=( ($size_arr[0]/$size_arr[1]) > 1.775 ? $size_arr[0].'x':'x'.$size_arr[1]);
system("convert \"$in\" -resize $resize -gravity Center -crop \"$size+0+0\" \"$out\"");
}
It seems that if width/height is larger than 1.775 i should use widthX as resize value and if else than i should use Xheight .

ImageMagick: What is this convert-command doing?

I'm trying to port a PHP script to Ruby and until now I only used ImageMagick to convert from one file-format to another. Meaning: Yes, I'm an ImageMagick newbie. ;-)
Somewhere inside the PHP script the following code is executed:
$output = array();
$returnValue = 0;
$cmd = 'convert '.$pngFile->path.' -resize 1x1 -alpha on -channel o -format "%[fx:u.a]" info:'
exec($cmd, $output, $returnValue);
Using the ImageMagick documentation for convert I identified the following options:
-resize 1x1 Resize to 1x1 pixels (right?)
-alpha on Activate alpha-channel
-channel o Apply options to the opacity image-channel
My questions:
What does -format "%[fx:u.a]" exactly do? I know that u is a symbol for first image in sequence and a one for alpha. But I don't get what the whole expression really does.
What does info: stand for?
What does this convert-command exactly do?
Thank you very much for your kind help.
Please note: The accepted answer on the following question has a very good answer to this question:
Understanding ImageMagick's convert and translating to Ruby RMagick
Seems like it is computing the average opacity. The info format is a dummy image format that will instruct convert to output image information to stdout (: means stdout) in the format %[fx:u.a]. Resizing to 1x1 is probably a way of averaging.

Categories