ImageMagick Draw translate coordinates - php

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.

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

How to add caption to Imagick polaroidImage?

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.

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

How to rotate an image in GD Image Library while keeping transparency?

I'm making a skin previewer for my site; I need to rotate parts of an image to create a representation of this for my users to see.
The skin is a PNG file, and all parts of it may have transparency or even none at all.
I need to be able to rotate this image while keeping any transparency inside the image transparent, while also having the extended borders (You know, the area that wasn't part of the image before it was rotated) transparent.
All of my attempts have left a black border around the image itself.
Any help?
Cut out the piece of the image you want to rotate
Rotate preserving alpha using something like this http://www.exorithm.com/algorithm/view/rotate_image_alpha
Merge back in preserving alpha using the following:
-
imagesetbrush($destimg, $srcimg);
// x, y are the center of target paste location
imageline($destimg, $x, $y, $x, $y, IMG_COLOR_BRUSHED);
I use that to rotate a PNG and preserve transparency color. Works like a charm. It's "basic GD".
$rotation = 135;
$handle_rotated = imagerotate($handle_not_rotated,$rotation,0);
imagealphablending($handle_rotated, true);
imagesavealpha($handle_rotated, true);
Don't know if it's what you're looking for?
You may want to check here for some uses for libpng (which will need zlib). If you are on linux you can write something in perl. CPAN GD module might be your ticket.

Convert an image color/hue using PHP GD library. The Catch? It has rounded corners

I'm writing code this week to dynamically change the color of images. As long as the image is square or rectangle, the code below works perfectly. However, I have an image that has rounded corners (filled with a spot color and the background outside the corners is white.)
Can someone tell me if there's a simple way to color an image that has rounded corners using the imagecolorset function (or any other php method)?
I only want the non white areas of the image to be colored (In case you're wondering, I'm disallowing the color white to be applied to the image).
Note, perhaps a better way to do this would be to use a PNG image that has transparent background (rather than my gif image). If you think that's a better approach, please advise.
Here's the function that I'm working with...
function set_theme_color_header($hex)
{
$info = hexToRGB($hex); //calls a helper function which translates the hex to RGB
$img = imagecreatefromgif('header-template.gif'); //again, this could be a PNG image, but we always start with this image, then create a color copy
$color = imagecolorallocate($img, $info["red"], $info["green"], $info["blue"]);
imagecolorset($img, 0, $info["red"], $info["green"], $info["blue"]);
imagegif($img, 'header.gif'); //only problem is that the imagecolorset function creates a messy fill at the corners
}
GD does not handle gif's very well. I recommend creating a png image with your rounded corners, then use GD's imagefilter($img, IMG_FILTER_COLORIZE, $info["red"], $info["green"], $info["blue"], $alpha) function. Then save the image as a gif.
Alternatively, use php's imagick library to literally draw the image. imagick is about a billion times better than GD. Poorly documented, but...the basic stuff isnnt too hard. See the tut link below.
Search php.net for "imagick" to see all the functions.
imagick tutorial
i really do not not why you are doing this, but maybe have a look at phpTumb (and its demos here).

Categories