A client of mine is trying to place text on a background image using imagettftext(). The code was working previously and suddenly went from displaying black text to displaying gray text. The code is properly set to black and changing it to any other color also has no effect.
$im = imagecreatefrompng("/path/to/background.png");
$text = imagecolorallocate($im, 0, 0, 0);
imagettftext($im, 10, 0,96, 201,$text, "vera", $thiscardnumber);
imagepng($im, "/path/to/new/".$imagefilename.".png");
imagedestroy($im);
Any help would be appreciated as nothing was changed in the code or on the server that I am aware of to suddenly stop this from generating proper black text.
You can see the text generated here:
The black text is part of the background image, the gray text is generated by the code above.
EDIT: From comments below
Ah, OK. If you just recreate a the few lines of code separately, do you still get the grey text? Because I get black text with your code. What about if you try a different font, or a different base image just to eliminate them as the culprit? Have you verified what the value of $text is?
ORIGINAL Answer:
There's something amiss here. Your "x" and "y" coordinates are larger than the background image, so the text you are generating is off of the image canvas.
Is there any chance that your background image just has the grey text in it already, and the text you are generating is happening outside of the image?
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 can successfully add a transparent rectangle to an area of a saved PNG file, but it has a faint border around it, which can be seen on the final image. I want to remove this.
The code I use to add the rectangle is:
$dest=imagecreatefrompng("originalFilename.png");
$red = imagecolorallocate($dest, 255, 0, 0);
imagecolortransparent($dest, $red);
imagefilledrectangle($dest, 0, 0, 50, 25, $red);
imagepng($dest, "newFilename.png");
and the result looks like this:
Image showing rectangle outline
The transparency works fine, showing in this example a white background behind... but you can see that in the top left there's the faint outline of the transparent rectangle. This is what I want to remove.
I've since tried putting a box over the edge (it would be white to match the background of the image, but using green for display purposes here), but that box gets a slightly red huhe around it too!?!? Really strange.
Like this
red hue
Grateful for any clues. This is not the same as the suggested duplicate as it deals specifically with the hue that's left around the border of the transparent area and also any object made to go in that area.
I am trying to create a text overlay on an image using php and iMagick. The crucial part is that I want the text size to scale to fill a predefined box, so I do not want to set a fixed font size. I have found the code at ImageMagick - Text into rectangle but need to adapt this code for the iMagick. I seem to be able to do everything except the crucial part -size ${width}x100
Here's the code I have, with a few things that I have tried commented out
$draw = new ImagickDraw();
$draw->setGravity (Imagick::GRAVITY_SOUTHEAST);
$draw->setFont('Bookman-DemiItalic');
//$draw->setFontSize( 30 ); // don't want to set a fixed font size
$draw->setTextUnderColor('#00000075');
//$draw->setSize(0, 0, 200, 200); // error: Call to undefined method ImagickDraw::setSize()
//$draw->setViewBox(200, 200, 800, 800); // appears to do nothing
//$draw->rectangle(200,200,300,300); // this just draws a separate white box
$draw->annotation(100, 100, 'The quick brown fox jumps over the lazy dog'); // works, but it's a default (small) text size and the box simply fits the exact text with no border
$img->drawImage($draw);
Is there a size option that somehow I've missed in the docs?
Note that How can I wrap text using Imagick in PHP so that it is drawn as multiline text? partially addresses this issue, but it doesn't appear to offer scaling of the text to fit a fixed size box, rather a wrapping of text if the selected font size would cause the text to overrun. Though maybe I have misunderstood - I will give it a try!
I am running into a weird font rendering issue when writing text over images using imagefttext().
$img = imagecreatefromjpeg( $canvas_image_path );
imagettftext( $img, $font_size, 0, $x, $y, $font_color, $font_path, $text );
I have a tool which selects a background image (different sizes - JPEGs (.jpg)) to work on and then write the text over it which comes from an WYSIWYG editor.
Using Bodoni-italic font, for certain image sizes the rendering of i is weird.
Spacing looks weird after T
It works absolutely fine for images that are small like 320 x 240 or big like 1536 x 2048 but for other sizes in between that range, i is written as shown in screenshot.
Now the fact that it does show up correctly for certain image sizes rule out the possibility of something being wrong with the font file itself. The only suspect I see here is the image size but I am not sure how to debug the situation from here & find a fix for it.
Any idea on where I should be looking or what can possibly cause this?
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