I just want to get the basic idea of how to draw a circle with php5 imagemagick.
I have looked at the page, http://php.net/manual/en/imagickdraw.circle.php
but the example there is too confusing. I want to just draw a circle then mess around with that. Can someone give me a simple php5 imagemagick circle example?
I have tried:
<?php
header("Content-type: image/jpeg");
$circle = new ImagickDraw();
$draw->circle (10, 10, 60, 10);
//echo $circle;
?>
any numerous variants, but I cant get a circle drawn.
I hope this is what you're looking for:
<?php
$draw = new ImagickDraw ();
//given that $x and $y are the coordinates of the centre, and $r the radius:
$draw->circle ($x, $y, $x + $r, $y);
?>
Danak on the php chat page provided me with this link, Which provides an active example.
http://www.phpimagick.com/ImagickDraw/circle
The variable names are concise and descriptive,
and the example makes it easy for me to understand what is going on.
function circle($strokeColor, $fillColor, $backgroundColor, $originX, $originY, $endX, $endY) {
//Create a ImagickDraw object to draw into.
$draw = new \ImagickDraw();
$strokeColor = new \ImagickPixel($strokeColor);
$fillColor = new \ImagickPixel($fillColor);
$draw->setStrokeOpacity(1);
$draw->setStrokeColor($strokeColor);
$draw->setFillColor($fillColor);
$draw->setStrokeWidth(2);
$draw->setFontSize(72);
$draw->circle($originX, $originY, $endX, $endY);
$imagick = new \Imagick();
$imagick->newImage(500, 500, $backgroundColor);
$imagick->setImageFormat("png");
$imagick->drawImage($draw);
header("Content-Type: image/png");
echo $imagick->getImageBlob();
}
Related
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);
After the follow code:
$im = new \Imagick('fu.png');
$im->thresholdimage(0.9, 127);
I'm getting this image:
so I need to close the open areas.
When I copy the code from http://phpimagick.com/Imagick/morphology?morphologyType=9 which is:
$im = new \Imagick('fu.png');
$im->thresholdimage(0.9, 127);
$canvas = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);
header("Content-Type: image/png");
echo $im->getImageBlob();
I'm getting error: PHP Fatal error: Using $this when not in object context in..
I don't have ANY idea how to use this, I've never seen anything like it and I'm sure it's not a matter of logical thinking but knowing exactly how the hell to do that or you're f***ed.
Please help!
UPDATE:
As Roljhon explained I need to use the getchar.. function so I did:
private function getCharacterOutline()
{
$im = new \Imagick('fu.png');
$im->thresholdimage(0.9, 127);
$character = new \Imagick();
$character->newPseudoImage(
$im->getImageWidth(),
$im->getImageHeight(),
"canvas:white"
);
$canvas = new \Imagick();
$canvas->newPseudoImage(
$im->getImageWidth(),
$im->getImageHeight(),
"canvas:black"
);
$character->compositeimage(
$im,
\Imagick::COMPOSITE_COPYOPACITY,
0, 0
);
$canvas->compositeimage(
$character,
\Imagick::COMPOSITE_ATOP,
0, 0
);
$canvas->setFormat('png');
return $canvas;
}
$canvas = $this->getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);
header("Content-Type: image/png");
echo $im->getImageBlob();
I don't know what I'm doing and what this piece of crap supposed to do anyway... I'm getting error ofc...
Do it like this instead since getCharacterOutline is a standalone function and not within a class
UPDATED
$canvas = getCharacterOutline();
$kernel = \ImagickKernel::fromBuiltIn(\Imagick::KERNEL_DISK, "6");
$canvas->morphology(\Imagick::MORPHOLOGY_CLOSE, 1, $kernel);
header("Content-Type: image/png");
echo $canvas->getImageBlob();
I am using PHP ImageMagic and I am trying to put a border around a Rectangle. I am following this, and this to create the border. It seems to be working fine for them, but for me, if I make the stroke width more than 2, it starts to break.
Here is the code, that I am using
$white = new ImagickPixel("rgb(255, 255, 255)");
$borderWidth = 10;
$draw = new ImagickDraw();
$strokeColor = new ImagickPixel("rgb(255, 255, 255)");
$fillColor = new ImagickPixel("none");
$draw->setStrokeColor($strokeColor);
$draw->setFillColor($fillColor);
//$draw->setStrokeOpacity(1);
$draw->setStrokeWidth(2);
$draw->rectangle(5, 5, 295, 295);
$imagick = new Imagick();
$imagick->newImage(300, 300, "rgb(225, 225, 225)");
$imagick->setImageFormat("png");
$imagick->drawImage($draw);
header("Content-type:image/png");
echo $imagick;die;
This is the result, with $draw->setStrokeWidth(2)
And this is the result, with $draw->setStrokeWidth(5) which obviously seems broken.
What might be the issue?
I am trying to draw multiple circles on a background image, but im having a hard time figuring out how to do it.
Ive tried passing multiple imageMagick instances to the draw function but did not work.
So then I tried creating a new image object, and setting it to transparent.
Then i tried the drawing an image on top of that, but i cant get it to adjust the opacity at all.
Along the lines of what i am trying to do is this:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
function drawImage(Imagick $im) {
// $im->setCompressionQuality(100);
$im->setImageFormat("jpg");
header("Content-Type: image/" . $im->getImageFormat());
echo $im;
exit;
}
// define circle mask
$layer = new Imagick('spc.jpg');
//now we need the height and width.
$width = $layer->getImageWidth();
$height = $layer->getImageHeight();
$x = $width/2;
$y = $height/2;
$endX = $x + 150;
$endY = $y + 150;
$circle = new ImagickDraw();
$circle->setFillColor("#FFFF00");
$circle->circle($x, $y, $endX, $endY);
//$layer->drawImage($circle, $circle2);
$circle2 = new ImagickDraw();
$circle2->setFillColor("#0000FF");
$circle2->circle(0, 0, $x, $y);
$layer->drawImage($circle, $circle2);
drawImage($layer);
?>
drawImage shoud get only 1 parameter. try this at the final lines:
$layer->drawImage($circle);
$layer->drawImage($circle2);
drawImage($layer);
I think/hope this is nearer to what you are aiming for... I have hacked around a bit because my environment is different from yours - but you should be able to see what I have done.
The main difference is that you need to set the fill colour to transparent and the stroke colour to whatever colour you want the circle outlines to be drawn in. Also, I have reused your Draw object rather than creating additional ones for each circle.
#!/usr/local/bin/php
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
function drawImage(Imagick $im) {
$im->setImageFormat("jpg");
$im->writeImage('out.jpg');
exit;
}
// define circle mask
$layer = new Imagick();
$layer->newImage(500,250,new ImagickPixel('white'));
$layer->borderImage('#6000ff',3,3);
//now we need the height and width.
$width = $layer->getImageWidth();
$height = $layer->getImageHeight();
$x = $width/2;
$y = $height/2;
$circle = new ImagickDraw();
$circle->setFillColor('transparent'); // transparent
$circle->setStrokeColor("#0000FF"); // blue
$circle->circle($x, $y, $x + 25, $y);
$layer->drawImage($circle);
$circle->setStrokeColor("#00FF00"); // green
$circle->circle($x, $y, $x + 50, $y);
$layer->drawImage($circle);
$circle->setStrokeColor("#FF00FF"); // purple
$circle->circle($x, $y, $x +75, $y);
$layer->drawImage($circle);
$circle->setStrokeColor("#FF0000"); // red
$circle->circle($x, $y, $x +100, $y);
$layer->drawImage($circle);
drawImage($layer);
?>
I have a php file for captcha. But image is not there in web page. I cant find out what is the prob. Please help me. Here is my code.
<?php
ob_start();
session_start();
// Set the content-type
header('Content-Type: image/jpeg');
mimetypes.add_type('application/font-ttf', '.ttf', True)
// Create the image
$im = imagecreatefromjpeg('bg.jpg');
// Create some colors
$R = rand(0,100);
$G = rand(0,100);
$B = rand(0,100);
$cc = imagecolorallocate($im, $R, $G, $B);
// The text to draw
$text = rand(100,10000);
$_SESSION['text'] = $text;
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, rand(40,45), rand(0,1), rand(10,70), rand(38,50), $cc, $font, $text);
$NumberOfLines=15;
imagecolorallocate($im, 15, 142, 210);
$cc=0;
while($cc < $NumberOfLines){
// set random color:::
//assign random rgb values
$c1 = mt_rand(50,200); //r(ed)
$c2 = mt_rand(50,200); //g(reen)
$c3 = mt_rand(50,200); //b(lue)
//test if we have used up palette
if(imagecolorstotal($im)>=255) {
//palette used up; pick closest assigned color
$color = imagecolorclosest($im, $c1, $c2, $c3);
} else {
//palette NOT used up; assign new color
$color = imagecolorallocate($im, $c1, $c2, $c3);
}
// done...
$startH =rand(3,200);
$startTOP = rand(0,8);
$stopH=rand(3,200);
$stopTOP =50;
imageline($im, $startH, $startTOP, $stopH, $stopTOP, $color);
$cc++;
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);
?>
Name of this script file is img.php And it is set as src of img
tag like
img src='img.php'
Here arial.ttf file is in the same folder where this php file resides. Please help me for this. This captcha image is not being loaded.
First remove the ob_start() command. It does not make much sense (at least to me).
Then uncomment the header(..) line so that you see error messages in the browser.
I had to do the following things to get the code up and running:
I removed the mimetypes line (is this PHP at all? If yes, a semicolon is missing)
the script did not find the font - I had to use absolute path and set the permissions right
When you are done with debugging and see fancy chars in your browser window, add the header(..) line again.