I am developing image editor, i successfully write text on images on position x,y, now i need to draw a white image on image that is called white out. white image will be drawn at position cord X, cord Y and dynamic size of image width, height. i write code but i am not getting the image on right coordinates and height width of image.
here is my image rendering function.
$image = new Imagick('image.jpg');
$diagnose=$this->input->post('diagnoses');
$fontsize=$this->input->post('fontsize');
$fontstyle=$this->input->post('fontstyle');
$cordX = $this->input->post('posX');
$cordY = $this->input->post('posY');
$font_size = 20;
// Watermark text
//$diagnose = "Hello world.{newline}It's a beautiful day.";
$textlinesArray = explode("{newline}",$diagnose);
// Create a new drawing palette
$draw = new ImagickDraw();
// Set font properties
$draw->setFont('Arial');
$draw->setFontSize($font_size);
$draw->setFillColor('black');
// Position text at the bottom-right of the image
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
$i = 0;
foreach ($textlinesArray as $index => $text) {
$newY = ($i * $font_size) + $cordY;
//echo $newY . "</br>";
//echo $text;
// Draw text on the image
$image->annotateImage($draw, $cordX, $newY,0, $text);
$i++;
}
$draw->setFillColor('magenta'); // Set up some colors to use for fill and outline
$draw->setStrokeColor( new ImagickPixel( 'cyan' ) );
$draw->setStrokeWidth(2);
$draw->rectangle($cordX, $cordY, 200, 300); // Draw the rectangle
$image->drawImage( $draw );
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
here is the output, i need that color box dynamic width height and position
Related
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'm am trying to add a 50px white margin down the right side of an image by creating an empty image that's 50px wider than the source image and then merging the source image onto it. The problem is that the source image just gets stretched sideways so it's 50px wider!
Maybe I'm using the wrong function to merge the images ...
here's my code
$destImage = $filepath;
#echo "dest image = ".$destImage;
$sourceImage = imagecreatefrompng($filepath);
// dimensions
$src_wide = imagesx($sourceImage);
echo "src_wide=".$src_wide;
$src_high = imagesy($sourceImage);
// new image dimensions with right padding
$dst_wide = $src_wide+50;
echo "dst_wide=".$dst_wide;
$dst_high = $src_high;
// New resource image at new size
$dst = imagecreatetruecolor($dst_wide, $dst_high);
// set white padding color
$clear = array('red'=>255,'green'=>255,'blue'=>255);
// fill the image with the white padding color
$clear = imagecolorallocate( $dst, $clear["red"], $clear["green"], $clear["blue"]);
imagefill($dst, 0, 0, $clear);
// copy the original image on top of the new one
imagecopymerge($dst,$sourceImage,0,0,0,0,$src_wide,$src_high, 100);
imagepng($dst,$destImage,6);
imagedestroy($dst);
chmod($destImage,0775);
what am I doing wrong here ??
thanks
It's stretching because you are copying it to the full width of the destination image. Instead use
imagecopyresampled($dst,$sourceImage,50,0,0,0,$src_wide,$src_high,$src_wide,$src_high);
I'm trying to piece together a png from various components of an svg file using Imagick. I am able to get the text in and behaving as expected. Now how can I start adding the various svg components. In the example below, I have a sample variable with a string that represents my svg element. I have to use SVG element formatting and attributes, like r, may occasionally be set by variables.
Edit: Big picture, I'm just converting an svg with some dynamic settings to a png.
<?php
//SVG ELEMENT TO INCORPORATE
$element = '<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />';
//MESSAGE
$message = "Get this SVG to work.";
//FILENAME
$fileName = "testSVG" . microtime(true) . ".png";
//SET THE SIZE, BACKGROUND, AND FORMAT
$image = new Imagick();
$image->newImage(1200, 630, new ImagickPixel('pink'));
$image->setImageFormat('png');
//NEW DRAW ELEMENT
$draw = new ImagickDraw();
//FONT PROPERTIES
$draw->setFont('Impact.ttf');
$draw->setFontSize(50);
$draw->setFillColor("red");
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
//FONT METRICS
$metrics = $image->queryFontMetrics($draw, $message);
//ADD TEXT TO CANVAS
$draw->annotation(5, $metrics['ascender']+5, $message);
//DRAW THE IMAGE
$image->drawImage($draw);
//SAVE THE IMAGE
file_put_contents($fileName, $image);
//DISPLAY THE IMAGE
header('Content-type: image/png');
echo $image;
?>
i just do image watermarking in php it's working but not setting image like i want,here's my code of php file.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];
$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname=$_FILES["fileToUpload"]["name"];
// Set the thumbnail name
$thumbnail = $folder.$newname."_thumbnail.jpg";
$actual = $folder.$newname.".jpg";
$imgname=$newname."_thumbnail.jpg";
// Load the mian image
$source = imagecreatefromjpeg($uploadimage);
// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');
// get the width and height of the watermark image
$water_width = imagesx($watermark);
$water_height = imagesy($watermark);
// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);
// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis
$dime_x = 0;
$dime_y = 0;
// copy both the images
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);
// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
}
?>
<img src='uploads/<?php echo $imgname;?>'>
</body>
</html>
and my html code also working fine.but problem with generated image it's like that
text with 'JACLIN ADMIN' is my png image and i want to apply it in middle from up and left. i just put 0 for both but problem is how can i put it in middle dynamicaly when size of images with diffrent height and width?please help me.
First, you need to find the middle point of your image:
$im_middle_w = $main_width/2;
$im_middle_h = $main_height/2;
Then you just need to add the watermark there but you need to move the watermark to the left by half (so it's actually centered):
$dime_x = $im_middle_w - $water_width/2;
$dime_y = $im_middle_h - $water_height/2;
Haven't tested it but it should work. If it doesn't work, feel free to link the images and I'll see to the code myself.
First set Value:
$watermark_pos_x = (imagesx($image)/2) - (imagesx($watermark)/2) - 15; $watermark_pos_y = (imagesy($image)/2) - (imagesy($watermark)/2) - 10;
Then after apply value on Function:
// merge the source image and the watermark
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, imagesx($watermark), imagesy($watermark));
I'm sorry for my English!
I've this problem with imagick php and overlay text on an image.
Size of font is small when resize the width of image.
This is an example for two image width, first is 350px and second is 1728px:
http://i.stack.imgur.com/8kRo0.jpg
http://i.stack.imgur.com/TedDO.jpg
$f->userFontSize is the size of font that logged user choose to show on his image.
if(trim($_GET['full']) != 'si'){
$width = 350;
$height = 350;
} else {
$width = getWidth($root . $f->imagePathFull);
$height = getHeight($root . $f->imagePathFull);
}
$image = new imagick();
$draw = new imagickDraw();
$pixel = new imagickPixel('white');
$image->newImage($width, $height, $pixel);
$image->readImage($root . $f->imagePathNormal);
$image->resizeImage ( $width, $height, imagick::FILTER_LANCZOS, 1, TRUE);
$fontPath = $root . '/assets/fonts/Mermaid.ttf';
$draw->setFillColor('black');
$draw->setFont($fontPath);
$draw->setFontSize(12 + $f->userFontSize);
$draw->setGravity(1);
$draw->setTextAntialias(true);
$draw->setFillOpacity(".55");
$image->annotateImage($draw, 5,0, 0, "text text text");
$image->setImageFormat("jpg");
header( "Content-Type: image/jpeg" );
echo $image->getImageBlob();
$image->clear();
When I print the image with resolution as 1728px the text is very small. How I do to do to increase font size depending image size?
Thank you! :)
I've solved with this way, I calculated the percentage of width that text occupy on image at 350px and according to this I calculated the percentage at 1728px(or another width). I do cicle a some font size until when I find a size that is egual at percentage. The final result of my solution is non perfect but acceptable. This is the code:
$myCP = '40'; // increase o decrease font size.
for($i = 1; $i <= 500;$i++){
$box = imagettfbbox($i, 0, $fontPath, "lorem ipsum");
$lT = $box[2] - $box[0];
$cP = round(($lT / $width) * 100,1);
// $cP percentage of font width
if($cP >= $myCP) { $newFontSize = $i; break; } else { continue; }
}