How to add caption to Imagick polaroidImage? - php

I can create a polaroid effect with imagick using this code:
$userphoto->polaroidImage(new ImagickDraw(), $angle);
I'd like to add also a caption, but I found no way to to this without using annotateimage.
The closest I got was using:
$im->newPseudoImage(300, 300, "caption:Put your text");
If I add the polaroid effect to it I get what I need, except that I found no way to put the image inside!
Does anyone know a solution for this?
Using imagemagick is so simple as this:
convert -caption "Faerie Dragon" dragon.gif -gravity center
-background black +polaroid anno_polaroid.png

If you need more advanced text drawing capabilities than annotateImage provides the solution is to:
Create a new ImagickDraw object
Do all the text drawing you want in there.
Draw the ImagickDraw over the actual image.
A very basic example is here though it currently doesn't do text.

Related

Convert all pixels of a given color to transparent in iMagick (PHP) syntax?

I want to convert a given color to transparency with iMagick. I have found one way to do this, but it behaves like a paint bucket rather than examining the entire image.
For the following example, I'm using this:
$transparentColor = new ImagickPixel('transparent');
$image->floodFillPaintImage($transparentColor, 20000, "#0009c5", 0, 0, false, Imagick::CHANNEL_ALPHA);
This is the input image
This is the output image
The result I'd like to see is all the blue areas turned to transparency. Unfortunately, it seems that "fill" is the key point in this function and hence stops when confronted with non-"target" colors.
Does anyone know how to accomplish turning all the blue areas to transparent using iMagick (not command line imageMagick)?
Thanks in advance!
Try:
$image->transparentPaintImage($targetColor, $alphaLevel, $fuzz, false);
If the transparent areas are "messy", it may help to despeckle:
$image->despeckleimage();
Doc: http://php.net/manual/en/imagick.transparentpaintimage.php

php image filter "colorize" destroys image details

i am trying to to colorize a black and white picture on my server.
i have tried doing this with multiple methods and the only one that kind-of-works is gd imagefilter via filter IMG_FILTER_COLORIZE.
it does kind of colorize the image in exactly the same color that i want, but it loses all the details on the image, as if it just trimmed black dots that were not black enough and thinned all the black lines, making them almost invisible. here is what i'm talking about:
this result was achieved with this code
$im=imagecreatefromjpeg($orig_file);
imagefilter($im, IMG_FILTER_COLORIZE, 71, 92, 10);
imagejpeg($im, $output_file, 95);
why is this happening? are there any other methods how i could colorize the image? my original image is quite large and i can't iterate over it as it is too slow; that's why i'm trying to use a library that would do this
i have managed to achieve the desired result with help of Imagick and compositeImage. here is the result
how i achieved it is kind of a trick that works only in a very specific condition- which is the need to have the background white and only black/gray objects in front (or the exact opposite). this technique wouldn't work on a background with transparency
the idea is to have 2 layers- underneath is the layer with original grayscale image and the top layer- fully filled with the desired color. then you composite these images using "COMPOSITE_LIGHTEN" (you might use other methods, like darken for example- if you have white objects on a black background)
this is the code
$base_color="#475c0a";
$im=new Imagick($orig_file);
$im2 = new Imagick();
$im2->newImage($im->getImageWidth(), $im->getImageHeight(), new ImagickPixel($base_color) );
$im->compositeImage($im2, Imagick::COMPOSITE_LIGHTEN, 0, 0);
$im->writeImage($new_image_path);
hope this will help someone out

ImageMagick Draw translate coordinates

I have generated a polygon using coordinates, which is stored in an ImagickDraw object. I would like to rotate a few copies of the object and then draw them at various positions onto my image.
I am using imagick 3.1.0rc1 and ImageMagick 6.7.6-5.
Here's what I am using:
$sprite = new ImagickDraw();
$sprite->polygon($coords) //array of coordinates
$sprite->rotate(-90); //Doesn't seem to rotate
$sprite->translate($x, $y); //Doesn't seem to translate
$im->drawImage($sprite);
The problem is that for some reason, rotate and translate does nothing. Am I doing something wrong? Or does rotate and translate not do what I think it's suppose to do?
Seems like translate was not the way to do it.
I ended up generating the sprite in a new ImagickDraw object and then drew it onto my main image using compositeImage() into the appropriate position.
Just for the record, you have to apply the rotation/translation before you do the drawing.

PHP Imagick API: How to change the Hue of an Image to known hexadecimal value

I create template websites. If a client choose a blue or green or purple heading, I don't want to have to store all those different color variations of an image. I want to programmatically change the hue. I do not want to 'flood fill' it because that would remove any textures or bevels.
For example page you see I have accomplished exactly what I want.
http://sta.oursitesbetter.com/test/index.php
I have done this using Imagick modulateImage function.
HOWEVER, I am just throwing random 'Hue' values and not RGB values. I want to accomplish this same thing feeding RGB values. I need a function similar to modulateImage however it must take RGB as value and set the image to that hue.
I have studied for the past 5 hours and cannot figure out how to do it. I need help.
Has any of the gurus of StackOverflow got a PHP Imagick solution to this color quandary?
From Wikipedia: Hue #Computing hue from RGB:
$hex=(sqrt(3)*($green-$blue)) /
(2*($red-$green-$blue));//$red, $green and $blue are each a value in the range 0->255
$img->modulateImage(100, 100, intval($hex*100/256));
//probably 256->100 value above will work, if didn't , try with the following insead.
//$img->modulateImage(100, 100, $hex);
Another option might be to use ImageMagick to create a single pixel image in the RGB colour you want and then see what ImageMagick makes that in HSL colorspace. Say your RGB values are 25,126,200, you could do this:
convert -size 1x1! xc:rgb\(25,126,200\) -colorspace HSL txt:-
# ImageMagick pixel enumeration: 1,1,65535,hsl
0,0: (57.0474%,77.7783%,44.1184%) #920AC71C70F1 hsl(57.0474%,77.7783%,44.1184%)
The following equivalent methods are maybe a little more succinct:
convert xc:rgb\(25,126,200\) -format "%[fx:100*p{0,0}.hue]" info:
57.0476
convert xc:rgb\(25,126,200\) -format "%[fx:100*hue]" info:
57.0476
So, IM makes that a Hue of 57.0474%. Of course you don't need to run that and parse the text output, I am only doing that to demonstrate the concept - you can access the pixel directly in PHP.

PHP/ImageMagic Get the "shadow" of an image

I would like to script PHP/ImageMagic in order to produce the "shadow" of a given image. Like in this example (done manually in GFXeditor) :
original shadow
alt text http://uppix.net/d/a/f/dc82fce795fc4af20170080b09c9a.png ==> alt text http://uppix.net/8/9/9/b1e9df4b2858c40081771961e028d.png
Note: All the originals images will be on a white background like in the example.
I've check the ImageMagic documentation but I haven't found anything useful yet. Does anyone know if it can be done in PHP/ImageMagic ? If so how ?
Use convert with -threshold option?
EDIT: oops... from PHP? Imagick::thresholdImage?
I wonder if it isn't more like a mask than a shadow? In the context of IM, shadow looks like a blurry copy of the image.
Try the edge detection chapter in the ImageMagick examples.

Categories