I am having the issue when converting to pdf the draw layer font colour is no longer black. I have tried adding this $image->quantizeImage(255, \Imagick::COLORSPACE_CMYK , 0, TRUE, FALSE); however, the quality is effected.
$draw = new ImagickDraw();
$canvas = new Imagick();
$draw->setFillColor("#000000");
$canvas->annotateImage($draw, $x, $y, 0,"Firstname");
$image->compositeImage($canvas,Imagick::COMPOSITE_ATOP,0, 0);
$image->setImageFormat('pdf');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="preview.pdf"');
echo $image->getImagesBlob();
update 1
I'm just trying to do some tests to see why the colour is incorrect. The Imagick colours appear to be inverted. For example, this code produces
$im->newImage($imageDimensions['width'], $imageDimensions['height'], new ImagickPixel('white'));
Update 2
I have discovered that the font colour becomes inverted. I do have a custom method that does inverse this. but causes the font to be off. Why does the font colour change to white. To test switch between the commented out $img
$image = new imagick();
$img = file_get_contents("https://i.pinimg.com/originals/7c/cb/01/7ccb010d8fddc4bcd84587ef3c34d100.jpg", false);
//$img = file_get_contents("https://www.footballcomics.co.uk/wp-content/uploads/2020/09/comic_1_page_3_bottom.jpg", false);
$image->readImageBlob($img);
$draw = new ImagickDraw();
$canvas = new Imagick();
$canvas->newPseudoImage(
600,
600,
"canvas:none"
);
$draw->setFillColor("#000000");
$draw->setFontSize( 26);
//$draw->setFont(plugin_dir_path( __FILE__ ) . './assets/fonts/aAntiCorona.ttf');
$canvas->annotateImage($draw, 300, 300, 0,"Firstname");
$image->compositeImage($canvas,Imagick::COMPOSITE_ATOP,0, 0);
$image->setImageFormat('pdf');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="preview.pdf"');
echo $image->getImagesBlob();
Update 3
I have tried adding profiles to the Imagick, believe I'm on the right track here. But still not presenting correctly
$icc_cmyk = file_get_contents(plugin_dir_path( '/JapanColor2001Coated.icc');
$canvas->profileImage('icc', $icc_cmyk);
Update 4
Running the code below will allow you to see Firstname be a different shade. Need that to match the other black text from the image.
$image = new imagick();
//$img = file_get_contents("https://i.pinimg.com/originals/7c/cb/01/7ccb010d8fddc4bcd84587ef3c34d100.jpg", false);
$img = file_get_contents("https://www.footballcomics.co.uk/wp-content/uploads/2020/09/comic_1_page_3_top.tif", false);
$image->readImageBlob($img);
$image->setImageColorspace(Imagick::COLORSPACE_CMYK);
$imageDimensions = $image->getImageGeometry();
$draw = new ImagickDraw();
//$draw->setImageColorspace(Imagick::COLORSPACE_CMYK);
$canvas = new Imagick();
$canvas->newPseudoImage(
$imageDimensions['width'],
$imageDimensions['height'],
"canvas:none"
);
$canvas->setImageColorspace(Imagick::COLORSPACE_CMYK);
$fillColor = new \ImagickPixel();
$fillColor->setColor('cmyk(0%,0%,0%,100%');
$draw->setFillColor($fillColor);
$draw->setFontSize( 26);
//$draw->setFont(plugin_dir_path( __FILE__ ) . './assets/fonts/aAntiCorona.ttf');
$canvas->annotateImage($draw, 45, 89, 0,"Firstname");
$canvas->transformImageColorspace(Imagick::COLORSPACE_CMYK);
$image->compositeImage($canvas,Imagick::COMPOSITE_ATOP,0, 0);
$image->setImageFormat('pdf');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="preview.pdf"');
echo $image->getImagesBlob();
You don't need, and shouldn't set the canvas image to CMYK before compositing it.
I actually don't know what is happening under the hood, but I think what is happening is that attempting to convert an RGB image that contains transparency to CMYK is a silly thing to do, and so instead of 'converting' it, ImageMagick assumes you just want to treat that canvas image as CMYK already, and so the colours get mapped (but not transformed) from RGBA to CMYK.
However, ImageMagick is understands that compositing images that contain transparency onto a CMYK image is a sensible thing to do, and so it supports composting RGBA images onto CMYK and doing the appropriate conversion at the same time.
Anyway, regardless of what is actually happening internally, this code appears to work:
<?php
$image = new Imagick();
$img = file_get_contents("second.tif", false);
$image->readImageBlob($img);
$imageDimensions = $image->getImageGeometry();
$draw = new ImagickDraw();
$canvas = new Imagick();
$canvas->newPseudoImage(
$imageDimensions['width'],
$imageDimensions['height'],
"canvas:none"
);
$fillColor = new \ImagickPixel();
$fillColor->setColor('black');
$draw->setFillColor($fillColor);
$draw->setFontSize( 26);
$canvas->annotateImage($draw, 45, 89, 0,"Firstname");
$canvas->transformImageColorspace(Imagick::COLORSPACE_CMYK);
$image->compositeImage($canvas,Imagick::COMPOSITE_ATOP,0, 0);
$image->setImageFormat('pdf');
$image->writeImages(__DIR__ . "working.pdf", false);
Related
I want to center text over an image, horizontal and vertical. And let it automatically fit (unless what the content is).
When I use the command line, this command works almost perfectly (needs some little finetuning):
magick image.jpg -gravity Center -size 320x140 caption:'This text is resized to best fill the space given.' -compose over -composite ouput.jpg
However, now I want to achieve the same with PHP, but the text does not resize over multiple lines, so I am doing something wrong.
The script I've created so far:
$text = 'This text is resized to best fill the space given.';
$width = 600;
$height = 600;
$textBackground = new ImagickPixel('transparent');
$textColor = new ImagickPixel('#000');
$gradient = new Imagick('image.jpg');
$image = new Imagick();
$image->newImage($width, $height, $textBackground);
$gradient->setImageColorspace($image->getImageColorspace());
$draw = new ImagickDraw();
//$draw->setFillColor($textColor);
$draw->setFontSize( 36 );
$draw->setFont("Alegreya.ttf");
$draw->setGravity (Imagick::GRAVITY_CENTER);
$image->annotateImage($draw, 0, 0, 0, $text);
$gradient->compositeImage($image, Imagick::COMPOSITE_OVER, 0, 0);
$gradient->setImageFormat('jpg');
header('Content-type: image/jpg');
echo $gradient;
The output this generates:
I am trying to create a grayscale mask in Imagemagick (v7.0.11) that behaves like a layer mask in Photoshop where white areas are completely opaque and black areas are fully masked.
function grayscaleMaskTest () {
//Create a black and white mask
$mask = new Imagick();
$mask->newImage(300, 300, "#ffffff");
$mask->setImageType(IMAGICK::IMGTYPE_GRAYSCALE);
$mask->setImageFormat('gif');
//draw black circle
$draw = new ImagickDraw();
$draw->setStrokeWidth(1);
$draw->setStrokeOpacity(1);
$draw->setStrokeColor(new ImagickPixel("#000000"));
$draw->setFillColor(new ImagickPixel("#000000"));
$draw->circle(300/2, 300/2, 300/2, 300/2 + 100);
$mask->drawImage($draw);
//$mask->writeImage('C:\Users\Mike\Documents\images\mask.png'); //export mask
//Create final image
$final = new Imagick();
$final->newImage(300, 300, "#FF0000");
//Not sure if I had this right but tried writing the mask data to the alpha channel
//$final->compositeImage($mask, IMAGICK::COMPOSITE_COPYOPACITY, 0, 0, IMAGICK::CHANNEL_ALPHA);
$final->compositeImage($mask, IMAGICK::COMPOSITE_COPYOPACITY, 0, 0);
$final->writeImage('C:\Users\Mike\Documents\images\final.png');
}
The mask looks like this :
The desired result from this code if it worked would be this (minus the fake transparency) :
With help from #fmw42 this ended up working. Thanks!
//Create a black and white mask
$mask = new Imagick();
$mask->newImage(300, 300, "#ffffff");
//draw black circle
$draw = new ImagickDraw();
$draw->setStrokeWidth(1);
$draw->setStrokeOpacity(1);
$draw->setStrokeColor(new ImagickPixel("#0"));
$draw->setFillColor(new ImagickPixel("#0"));
$draw->circle(300/2, 300/2, 300/2, 300/2 + 100);
$mask->drawImage($draw);
$mask->setImageMatte(false); //<-------This got it going!
//Create final image
$final = new Imagick();
$final->newImage(300, 300, "#FF0000");
$final->compositeImage($mask, imagick::COMPOSITE_COPYOPACITY, 0, 0);`
I try to set a text gradient from this solution here https://www.sitepoint.com/community/t/gd-text-gradient/82127/9
But the background color of the final image is black, I try $im->flattenImages and $img->setBackgroundColor but it's not working.
$im = new Imagick();
$draw = new ImagickDraw();
$draw->setFontSize(90);
$draw->setFillColor(new ImagickPixel("black"));
$draw->setTextEncoding('UTF-8');
$draw->setGravity(Imagick::GRAVITY_CENTER);
$metric = $im->queryFontMetrics($draw, $text);
$width = $metric['textWidth'];
$height = $metric['textHeight'];
/* Create and save the gradiant */
$Imagick = new Imagick();
$Imagick->newPseudoImage($height, $width, "gradient:#FB7F4C-#FF409C");
/*** rotate the image ***/
$Imagick->rotateImage(new ImagickPixel(), 270);
$Imagick->setImageFormat('png');
$Imagick->writeImage("gradiant.png");
/* Create and save the canvas */
$im->newPseudoImage($width, $height, "null:");
$im->setImageFormat('png');
$im->writeImage("canvas.png");
/* Add the text to the canvas ( Make the mask )*/
$im = new Imagick("canvas.png");
// Write the text on the image
$im->annotateImage($draw, 0, 0, 0, $text);
$im->setImageBackgroundColor("transparent"); // <= Here
/* Final image */
$canvas = new Imagick("gradiant.png");
$canvas->compositeImage($im, imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
$canvas->setImageFormat('png');
$canvas->writeImage(__DIR__ . "/../../final.png");
header('Content-Type: image/' . $im->getImageFormat());
echo $canvas;
unlink("canvas.png");
unlink("gradiant.png");
I found after the COMPOSITE_DSTIN the background color goes black, I try many way but its not working
How to remove black background?
I'm solved it by adding alphachannel
I try $canvas->setImageAlphaChannel(Imagick::ALPHACHANNEL_RESET); and after that the background stay transparent
UPDATE
After I updated my imagick library it's stop working and search for similar issue and found this Imagemagick compose image inverted
And here the code that works well
$canvas->transformImageColorspace(Imagick::COLORSPACE_SRGB);
I am using shared hosting and extensions is installed too, the following code not working and just showing blank image in output I have tried many of code/function of Imagick all of codes showing the following image.
Please help me out.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Create objects
$image = new Imagick('image.png');
$watermark = new Imagick();
$mask = new Imagick();
$draw = new ImagickDraw();
// Define dimensions
$width = $image->getImageWidth();
$height = $image->getImageHeight();
// Create some palettes
$watermark->newImage($width, $height, new ImagickPixel('grey30'));
$mask->newImage($width, $height, new ImagickPixel('black'));
// Watermark text
$text = 'Copyright';
// Set font properties
$draw->setFont('arial.ttf');
$draw->setFontSize(20);
$draw->setFillColor('grey70');
// Position text at the bottom right of the image
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the watermark palette
$watermark->annotateImage($draw, 10, 12, 0, $text);
// Draw text on the mask palette
$draw->setFillColor('white');
$mask->annotateImage($draw, 11, 13, 0, $text);
$mask->annotateImage($draw, 10, 12, 0, $text);
$draw->setFillColor('black');
$mask->annotateImage($draw, 9, 11, 0, $text);
// This is needed for the mask to work
$mask->setImageMatte(false);
// Apply mask to watermark
$watermark->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
// Overlay watermark on image
$image->compositeImage($watermark, Imagick::COMPOSITE_DISSOLVE, 0, 0);
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
i create image using Imagick function . i use wood.png as row image , 1.jpg is over right on wood.png . problem is i need to see wood image as transparent of 1.jpg.
i also upload result.png but we need same as need.png.
$width = 300;
$height = 400;
$image = new Imagick("wood.png");
$image->cropImage($width,$height, 0,0);
$current_image = new Imagick();
$current_image->readImage('1.jpg');
$current_image->cropImage($current_image->getImageWidth(),$current_image->getImageHeight(), 0, 0);
$current_image->thumbnailImage($width, $height);
$image->compositeImage($current_image, imagick::COMPOSITE_OVER, 0, 0);
header("Content-Type: image/png");
echo $image;