【问题标题】:get lowermost left corner of an iamge and write text there获取图像的最左下角并在那里写文字
【发布时间】:2011-06-16 15:29:21
【问题描述】:

我正在尝试获取图像的最左下角 (x,y) 坐标。 我这样做是为了能够在左下角用不同大小的图片写文字。下面是代码。你能帮忙吗?

<?php
$white = imagecolorallocate($image2, 255, 255, 255);
$grey = imagecolorallocate($image2, 128, 128, 128);
$black = imagecolorallocate($image2, 0, 0, 0);
$textsize = 30;
$size = imagettfbbox($textsize, 0, $font, $text);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image2size = getimagesize("image2.jpg");
$textleftpos = round(($image2size[0] - $xsize) / 2);
$texttoppos = round(($image2size[1] + $ysize) / 2);
imagettftext($image2, $textsize, 0, $textleftpos, $texttoppos, $white, $font, $text);
imagejpeg($image2, "image3.jpg");
?>

【问题讨论】:

  • 你的代码有什么问题?
  • 我可以居中,但不能选择最下角。正如您从我的多次编辑中看到的那样,我遇到了一些可变错误。修复了它们,但现在图像是文本居中而不是左角。
  • 到底发生了什么?无意冒犯,但您需要在错误描述中变得更加详细,它们往往有点不清楚。请告诉我们您得到的确切坐标,而不是例如最低角
  • @pekka-none 采取:) 很荣幸你们花时间回答这些新手问题。这些是我得到的坐标:(50,152)

标签: php gd imagick


【解决方案1】:

在左边缘表示 x 坐标为 0 在底部边缘表示 y 坐标等于图像的高度减去文本的高度

所以,假设你的文字大小是 30px:

$size = imagesize($img);
$x = 0;
$y = $size[1] - 30;
// assuming you're using GD1
imagettftext($image, 30, 0, $x, $y, $color, $font, "sample text");

【讨论】:

  • @reanimation-thanks,但我选择了 Brad 的解决方案。无论如何都要投票!
【解决方案2】:
$indentfromedge = 5; // or whatever you want for an indent
$textleftpos = $indentfromedge;
$texttoppos = $image2size[1] - $ysize - $indentfromedge;

我想这就是你想要的。用上面的代码将其中的两行替换为$text*pos

【讨论】:

  • 谢谢布拉德!像魅力一样工作。
猜你喜欢
  • 2023-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多