GD Rotate not working

Im trying to simply rotate some text by 15 degrees, but I cant get it working at all despite multiple debug lines

My simplified code I dont think I could get more simpler, what Ive done is:-

header('Content-Type: image/png');

$im = imagecreatetruecolor(300, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 300, 150, $white);

$text = 'Testing...';
$font = 'arial.ttf';

imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

imagerotate($im, 15, 0);


Open in new window

Any idea what Im doing wrong?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Your first problem is most likely your font file. The path to the font file needs to be the absolute path if the .ttf is not in the same folder as your script.

This will get you an image but it won't be rotated. This is because imagerotate returns a resource handle to the rotated image. You have to use that return value in your imagepng call - and then destroy that handle as well.

Sample code here (with arial.ttf in same folder as script).
header('Content-Type: image/png');

$im = imagecreatetruecolor(400, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 399, 149, $white);

$text = 'Testing...';
$font = 'arial.ttf';

imagettftext($im, 20, 0, 11, 21, $black, $font, $text);

$rotate = imagerotate($im, 15, 0);


Open in new window

Working sample here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
No points for this, please, but when in doubt it's always wise to check the online man pages.  I could not work without a window open to at all times.  In the instant case you can see that the imagerotate() function returns a resource.

I can see how this can be confusing, since many of the other image functions mutate a resource and return something like a boolean, but that is the unfortunate nature of PHP - it's hard to guess what will be changed or returned without reference to the manual.  Example of a related function:
Vimal DMSenior Software EngineerCommented:

Try installing "imagick" and restarted apache.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.