كيف اتحكم في النص وحجم الصورة عند تحويل النص الى صورة ؟

  • مجهول

السلام عليكم

لدي استفسار عن كود لتحويل النص الى صورة ، انشأته بهذا الشكل :

  require('./I18N/Arabic.php'); 
  $Arabic = new I18N_Arabic('Glyphs'); 
  $font = './Alhurra Regular.ttf';
  $text = $Arabic->utf8Glyphs(htmlspecialchars($_GET["text"]) );
  $im = imagecreatefrompng('bg3.png');
  // Create some colors
  $white = imagecolorallocate($im, 255, 255, 255);
  $grey = imagecolorallocate($im, 128, 128, 128);
  $black = imagecolorallocate($im, 0, 0 , 0);
  imagefilledrectangle($im, 0, 0, 0, 0, $white);
  // Add the text
  imagettftext($im, 30, 0, 0,300, $black, $font, $text);
  // Using imagepng() results in clearer text compared with imagejpeg()
  imagepng($im, "./text-img/output_arabic_image.png");
  $remoteImage = "./text-img/output_arabic_image.png";
  $imginfo = getimagesize($remoteImage);
  header("Content-type: {$imginfo['mime']}");
  readfile($remoteImage);

النتيجة تظهر هكذا

اريد ان يأتي النص داخل المربع الاحمر

شكرا على المساعدة


$img = imagecreatefrompng('bg3.png');

$x = 10; // موضع المستطيل أفقيا
$y = 10; // موضع المستطيل عموديا
$w = 200; // عرض المستطيل
$h = 100; // إرتفاع المستطيل

$font = realpath('Alhurra Regular.ttf'); // رابط الخط
$font_size = 30; // حجم الخط
$color = imagecolorallocatealpha($img, 255, 0, 0, 50); // اللون
$text = 'مرحبا بالعالم';

$bbox = imagettfbbox($font_size, $angle, $font, $text);
$center_x = $x + $w / 2 - ($bbox[2] - $bbox[0]) / 2;
$center_y = $y + $h / 2 + $bbox[1];

imagerectangle($img, $x, $y, $x + $w, $y + $h, $color);
imagettftext($img, $font_size, 0, $center_x, $center_y, $color, $font, $text);

لم يعمل معي بشكل جيد ، مزال الخط خارج الصورة عندما يكون النص طويل

هذا الكود كاملا لا يسمح بخروج النص عن الإطار

يدعم أيضا إمكانية إختيار تموضع النص أفقيا

احسنت ، هذا هو المطلوب