John Davidson

php - Test Imagick Image

0 comments
Message:


Hello i'm doing a test with Imagick library with following code


<?php

error_reporting(E_ALL);
ini_set( 'display_errors','1');

/* Create a new imagick object */
$im = new Imagick();

/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");

/* Create imagickdraw object */
$draw = new ImagickDraw();

/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);

/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);

/* Close the pattern */
$draw->popPattern();

/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');

/* Set font size to 52 */
$draw->setFontSize(52);

/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");

/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");

/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);

/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);

/* Set the format to PNG */
$canvas->setImageFormat('png');

/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>

The output of this script should be this: https://i.stack.imgur.com/N5f30.jpg


However i got the following error


Fatal error: Uncaught ImagickException: non-conforming drawing primitive definition `text' @ error/draw.c/RenderMVGContent/4466 in /home/andrea/domains/domain.it/public_html/test/magick.php:38 Stack trace: #0 /home/andrea/domains/domain.it/public_html/test/magick.php(38): Imagick->drawImage(Object(ImagickDraw)) #1 {main} thrown in /home/andrea/domains/domain.it/public_html/test/magick.php on line 38

PHP Version is PHP Version 7.4.27
Imagick version is https://i.stack.imgur.com/aBjF7.jpg


Thanks for any kind of help



My Answer:
php

// Create a new Imagick object
$image = new Imagick();

// Set the image format
$image->setFormat("JPEG");

// Read the image file
$image->readImage("input.jpg");

// Resize the image
$image->resizeImage(200, 200, Imagick::FILTER_LANCZOS, 1);

// Add some text to the image
$text = new ImagickDraw();
$text->setFillColor('white');
$text->setFontSize(20);
$text->setGravity(Imagick::GRAVITY_CENTER);
$text->annotation(0, 0, "Hello, World!");

$image->drawImage($text);

// Save the modified image
$image->writeImage("output.jpg");

// Destroy the image object
$image->destroy();

echo "Image processed successfully!";
?>


This code creates a new Imagick object, reads an image file, resizes the image, adds some text to the image, and saves the modified image to a new file. You can customize the code to suit your needs and test different functionalities of the Imagick library.

Rate this post

4 of 5 based on 2430 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us