Page 1 of 1

distorting php string

Posted: 2011-02-04T06:44:05-07:00
by rivler
Hi,

I'm trying to distort some text using imagemagick in php. But I can't figure out how to get imagemagick to read the string to be distorted, I can't turn it into an image at this point because I add the string to an image using "imagettftext" later on.

I receive the error: "Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `C:\wamp\www\h': No such file or directory' in C:\wamp\www\distort_test.php:20 Stack trace: #0 C:\wamp\www\distort_test.php(20): Imagick->__construct('h') #1 {main} thrown in C:\wamp\www\distort_test.php on line 20"

The code I'm trying to get to work is below. Any help would be hugely appreciated.

Thank you!

Code: Select all

<?php
{
$image = imagecreate(400, 200);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);

$letter1 = "abcdefghijklmnopqrstuvwxyz";
$letter2 = "abcdefghijklmnopqrstuvwxyz";

$text1 = "";
$text2 = "";
		
$text1 .= $letter1[rand(0,25)];
$text2 .= $letter2[rand(0,25)];

$font = 'Arial.ttf';
	
$im = new imagick($text1);
			
	$im->distortImage( Imagick::DISTORTION_PERSPECTIVE,

	array( 8, 40, 6, 30, 8, 126, 8, 126, 90, 126, 105, 126, 90, 5, 126, 30), true );
	
$im2 = new imagick(text2 );
			
	$im2->distortImage( Imagick::DISTORTION_PERSPECTIVE,

	array( 8, 40, 6, 30, 8, 126, 8, 126, 90, 126, 105, 126, 90, 5, 126, 30), true );
	
imagettftext($image, 14, 0, 50, 60, $black, $font, $im);
imagettftext($image, 14, 0, 90, 60, $black, $font, $im2);
	
imagepng($image);
imagedestroy($image);

header('Content-Type: image/png');
echo $image;
}
?>

Re: distorting php string

Posted: 2011-03-03T23:05:14-07:00
by DJ Mike
The error message is because you have a mix of GD and Imagick. You first create a GD resource then you try and use that resource with Imagick. There were some other problems but that is the big one. The following code uses only Imagick and assumes that the font is in the same directory.

Code: Select all

<?php
    # variables of parameters
    $text = "Some Text";
    $font = "LOKICOLA.TTF";
    $size = 60;
    $color = "black";
    $bgcolor = "white";

    # create image resource
    $im = new imagick();
    $im->newimage(400,200, "blue");
    $im->setimageformat("png");
    $im->setimagebackgroundcolor("$bgcolor");

    # make a draw object with settings
    $draw = new imagickdraw();
    $draw->setgravity(imagick::GRAVITY_CENTER);
    $draw->setfont("$font");
    $draw->setfontsize($size);
    $draw->setfillcolor("$color");

    # annotate
    $im->annotateImage($draw, 0,0,0, "$text");
    $im->setImageVirtualPixelMethod( imagick::[SPAM] );
    # distort
    $im->distortImage( Imagick::DISTORTION_PERSPECTIVE,
         array( 8,40,  6,30, # top left
                8,126,  8,126, # top right
                90,126, 105,126, # bottom right
                90,5,  126,30), # bottom left
                true );
                
    # output
    header("Content-Type: image/png");
    echo $im;
    ?>