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;
Related
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);
Using bellow PHP code I can add text to an image but Now I want to add a background color to image text. Can you tell me how to do this?
Note: The text background needs to bellow the image.
What is now:
What I want:
PHP code:
//Set the Content Type
header('Content-type: image/jpg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Set Path to Font File
$font_path = 'arial.ttf';
// Set Text to Be Printed On Image
$text = "My Content Goes to here";
// Print Text On Image
//$image = imagecreatefromjpg($jpg_image);
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
The problem becomes easy, if you don't think of it as a background color, but as drawing a rectangle before writing the text:
$bcolor=imagecolorallocate($jpg_image, 0x00, 0xC0, 0xFF);
imagerectangle($jpg_image, 0, $orig_height*0.8, 0, $orig_height, $bcolor);
before the imagettftext.
EDIT
It was of course imagefilledrectangle, not imagerectangle. Here is the complete script
<?php
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('test-02.jpg');
$orig_width = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);
// Allocate A Color For The background
$bcolor=imagecolorallocate($jpg_image, 0x00, 0xC0, 0xFF);
//Create background
imagefilledrectangle($jpg_image, 0, $orig_height*0.8, $orig_width, $orig_height, $bcolor);
// Set Path to Font File
$font_path = realpath(dirname(__FILE__)).'/arial.ttf';
// Set Text to Be Printed On Image
$text = "New Content Goes to here";
// Allocate A Color For The Text
$color = imagecolorallocate($jpg_image, 0, 0, 0);
// Print Text On Image
imagettftext($jpg_image, 20, 0, 10, $orig_height-10, $color, $font_path, $text);
//Set the Content Type
header('Content-type: image/jpg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
I've got some basic code working to generate an image with a solid background. However I was wondering how I can make a gradient. This is my code:
<?php
function process($inputdata)
{
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );
/* New image */
$image->newImage(400, 300, $pixel);
/* Black text */
$draw->setFillColor('black');
/* Font properties */
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );
/* Create text */
$image->annotateImage($draw, 10, 45, 0, $inputdata);
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $image;
return;
}
The closest code I could find is something like this:
$gradient = new Imagick();
$gradient->newPseudoImage(400, 300, 'gradient:blue-red');
But I don't know how I then combine the gradient with the text.
Please use compositeImage function. It combine one image into another. Create one Imagick instance with gradient, second one with text and alpha channel background, and combine them into the one.
Reference: http://php.net/manual/en/imagick.compositeimage.php
$text = 'The quick brown fox jumps over the lazy dog';
$width = 1000;
$height = 1000;
$textBackground = new ImagickPixel('transparent');
$textColor = new ImagickPixel('#000');
$gradient = new Imagick();
$gradient->newPseudoImage($width, $height, 'gradient:blue-red');
$image = new Imagick();
$image->newImage($width, $height, $textBackground);
$gradient->setImageColorspace($image->getImageColorspace());
$draw = new ImagickDraw();
$draw->setFillColor($textColor);
$draw->setFontSize( 10 );
$image->annotateImage($draw, 10, 45, 0, $text);
$gradient->compositeImage($image, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
$gradient->setImageFormat('png');
header('Content-type: image/png');
echo $image;
Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's my code -
$patchImageS = 'image.png'; // the image to be patched over the final bg
$patchImage = imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage = imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');
I tried to change the parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've found:
// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);
// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);
// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);
// Set your appropriate header
header('Content-Type: image/png');
// Render canvas to the browser
imagepng($png);
// Clean up
imagedestroy($png);
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
look for imagesavealpha() in the php-documentation - i think this is what you are looking for.
EDIT: here's an example:
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
$png = imagerotate($png, 23, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);