So I'm trying to draw a line with Imagick today. Sounds simple enough right?
Here's the code I have:
$image = new Imagick();
$image->newImage(100, 100, 'white');
$image->setImageFormat('png');
$line_color = new ImagickPixel('#555555');
$line_weight = 1;
$line = new ImagickDraw();
$line->setResolution(100, 100);
$line->setStrokeWidth($line_weight);
$line->setStrokeColor($line_color);
$line->line(20, 20, 20, 60);
$image->drawImage($line);
$image->writeImage('test.png');
Nothing surprising, I create a 100x100 white image, I set a line color at 555555, a line weight at 1, I create my ImagickDraw object and tell him to draw a line.
The problem is that the output looks like this: http://i.stack.imgur.com/hM8QS.png
Which looks fine at first, until you zoom in and see this: http://i.stack.imgur.com/O6Yie.png
I tried several settings for the color, the weight and the resolution, but I can't get rid of this weird shadow/blur effect. It does this with pretty much anything I draw (lines, rectangle etc...) except with points. So if I want I can create my single-pixel line with a series of points and a simple for loop, but it will really get tedious to do. Especially since I'm not gonna draw just this single line.
So, anyone has any idea of what is happening? How can I get rid of this behavior?
The effect you are seeing is due to stroke anti-aliasing being enabled. You can disable it with setStrokeAntialias.
$line->setResolution(100, 100);
$line->setStrokeWidth($line_weight);
$line->setStrokeColor($line_color);
$line->setStrokeAntialias(false);
Reenable stroke anti-aliasing whilst drawing curved lines to avoid aliasing artifacts.
Bizarely, in my tests, this causes the line to disappear completely when the stroke color is set to #555555 (or an equivalent representation) as it is in your code sample. However, setting the color to #545454 or #565656 causes the line to be drawn correctly, without the shadow/blur effect you described.
Related
I am using PHP-Imagick to convert a PDF into images, which works fine.
A simplified example:
$im = new imagick();
$im->setResolution(250,250);
$page = $pdfPath."[0]";
$im->readImage($page);
if($im->getImageColorspace() == Imagick::COLORSPACE_CMYK)
{
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->autoLevelImage();
}
$im->setImageFormat('jpg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(90);
$im->writeImage("page1.jpg");
However the coloring seems to differ from the original PDF, for example (see image)
left side is original PDF.
right side the rendered image, which has different colors than the original.
It seems that the original color of dark-pink turns into purle. Also red colors seem to saturate.
I have tried the following, but none worked:
reduce saturation (e.g. $im->modulateImage(100, 80, 100); ) But this causes red to become orange and yellow colors to turn greenish.(see example below)
playing around with image filter (e.g. imagefilter($im, IMG_FILTER_COLORIZE,100, 0, 0); ), but this also changes all colors.
Thinking it had to do with differences between CMYK and RGB, I have added $im->transformImageColorspace() doesn't do anything to fix this.
playing with various hue filters, but these change all colors of the image also.
Question: it seems I need to reduce the redness of the image? (I think it's the excessive red color causing the difference) without affecting the other colors, so that for example yellow remains yellow. Is there a way to do this in PHP?
The solution is to add the following line before reading the image:
The code should be:
$im->setColorspace(Imagick::COLORSPACE_SRGB);
$im->readImage($page);
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
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
I have a webcam and want to add some annotations to image, but antialiasing works strange. I'll try to explain:
I have an image like this:
Then I add semi-transparent rectangle and put text over it:
// add background black rectangle
$draw->setFillColor('black');
$draw->setFillOpacity(0.5);
$draw->rectangle( 0, 10, $cam['width'], 70 ); // Draw the rectangle
// add lead text
$draw->setFont('ARIALBD.TTF');
$draw->setFontSize(14);
$draw->setFillColor('white');
$draw->annotation(20, 35, $cam['text']);
// add timestamp
$draw->setFont('ARIAL.TTF');
$draw->setFontSize(12);
$draw->setFillColor(new ImagickPixel('#f3ac01'));
$draw->annotation(20, 50, date("Y-m-d H:i:s"));
$img->drawImage($draw);
And get something like this:
Huh, but timestamp looks ulgy(look at minutes/seconds). Trying to disable antialiasing:
$draw->setTextAntialias(false);
and now it looks still ugly(look at hours), but now it has sharp edges:
How can I get result with smart antialiasing, like this(I know PS uses own antialiasing, but IMagickDraw deal real bad result):
For me it looks more like the compression level of the whole image when saved than the antialiasing, have a look at setCompressionQuality, and tweak the compression quality until you avoid the "blurriness" you have in the picture.
Try the different font first. Here is example of open sans (another link) font.
I've run into another problem with GD and PHP.
I'm successfully writing text to an image.
However, I've encountered a case where it would be beneficial to place the text - instead of directly on the image - on a rectangle (or any shape) to create a solid background for the text where the image it's being placed on might not allow the text to be read very easily.
My two ideas are, in order of preference:
Fill the background with the color as it writes the text
Write the text to an appropriately sized image with a solid background, and then overlay the image onto the target
I can't figure out how to do #1 and #2 seems overly complex and I don't know how to determine the dimensions of the text so that I can create a new image for it.
For clarity, here is the output that isn't very good:
And here's how I'd like it to look, with a tight box behind the text of any color:
I'm open to any suggestions, but drawing the color on the fly without any other images or hackiness would obviously be my first choice.
Update:
After #Dan suggested using `imagettftext', I decided that it was high time I added support for that function to my library. Everything is working as would be expected except for one major issue.
The text that's written to the image is still transparent, even when written to a solid background (0 transparency).
Here's a script I wrote to test:
<?php
set_include_path('backbone:global:jquery');
require_once('Image.php');
$scout = new Image();
$scout->source = "scout.jpg";
$result = $scout->Write->Font(25, 25, "A Fairly Long String", 12, "#FF0000", 0, "LiberationSans-Regular.ttf", 1, "#FFFF00", .4, 4);
if( !isset($_GET['dev']) )
{
header("Content-Type: " . $scout->contentType());
}
if( $result )
{
$scout->output();
}
?>
The files I used/required:
1. scout
2. liberation font
3. Image Manipulation Library
- Image
- ImageBase
- ImageCombine
- ImageDraw
- ImageManipulate
- ImageWrite
I apologize about all the files, it really only uses Image, ImageBase, ImageCombine, and ImageWrite, but the others are require_onceed by the loader.
Here's a sample of the output from the script above:
And here's output with zero transparency (fully opaque):
$result = $scout->Write->Font(25, 25, "A Fairly Long String", 12, "#FF0000", 0, "LiberationSans-Regular.ttf", 1, "#FFFF00", 1, 4);
Any ideas what could be causing this? It's EXTREMELY possible that it's my code somewhere, but it seems strange that it would work exactly as I thought it should except for this one bug.
In searching desperately for the answer to this problem, I stumbled upon this SO question that's tangentially related to my problem, and contains the answer.
I've never fully understood why you tell imagealphablending false when you want transparency, so I guess my failure to properly understand the code I'm using has led to this issue.
In any case, the following modified code works like a charm in my one single test case.
To write text to a background without forced 100% transparency for the character boxes, you must turn alpha blending ON while writing the text:
imagealphablending($text->handle, true);
$bool = imagettftext($text->handle, $textSize, $angle, $padding, abs($size[5]) + $padding, $this->AllocateColor($rgba[0]['r'], $rgba[0]['g'], $rgba[0]['b'], $rgba[0]['alpha']), $font, $string);
imagealphablending($text->handle, false);
Maybe this will do the job for you if you switch to ttf. http://php.net/manual/en/function.imagettfbbox.php