开发者

imagettfbbox & imagettftext - resize,position,rotate

开发者 https://www.devze.com 2022-12-27 11:09 出处:网络
Ok, now it\'s officialy 10 hours that I\'m trying to make this work, but by every minute I\'m farther and farther from the final goal...

Ok, now it's officialy 10 hours that I'm trying to make this work, but by every minute I'm farther and farther from the final goal...

The thing is, I need to create a function, class or whatever that by the selected TTF or truetype font create, properly resize, and fill the image with a text given by the url. The image shouldn't be longer than that text. I've given up on all my codes, and the best thing I've got so far is taken from the net ...

<?php
  if(!isset($_GET['size'])) $_GET['size'] = 20;
  if(!isset($_GET['text'])) $_GET['text'] = "Moj tekst";
  $get_font = ( isset( $_GET['font'] ) ) ? $_GET['font'] : 'arial';

  $size = imagettfbbox($_GET['size'], 0, "font/".$get_font.".ttf", $_GET['text']);
  $xsize = abs($size[0]) + abs($size[2]);
  $ysize = abs($size[5]) + abs($size[1]);

  $image = imagecreate($xsize, $ysize);

  imageSaveAlpha($image, true);
  ImageAlphaBlending($image, false);

  $transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
  Imagefill($image, 0, 0, $transparentColor);

  $blue = imagecolorallocate($image, 0, 0, 255);
  $white = ImageColorAllocate($image, 255,255,255);
  $black = ImageColorAllocate($image, 0,0,0); //-($_GET['size']/20)
  imagettftext($image, $_GET['size'], 0, abs($size[0]), abs($size[5]), $black, "font/".$get_font.".ttf", $_GET['text']);

  header("content-type: image/png");
  imagepng($image);
  imagedestroy($image);
?>

This code is ok, although the edge of the last or first latter is sometimes partly hidden or just it doesn't fit into the created image (dependin开发者_开发问答g on font size), which isn't the problem with smaller font size. And the function i really need is by changing image's rotation to change the entire images proportions so the text fits in.

Example : http://img199.imageshack.us/img199/9368/32739521.jpg

Also, if there is finished function or class that does this, it would be great.. :'( :)


If you are always missing one letter of the end of the beginning, why not just add the width of a single letter to your final size?

In my experience, imagettfbbox doesn't do a very good job of deciding what does and doesn't fit within a bounding box - you need to fudge it a little yourself :)

Hope this helps.

Luke Peterson

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号