Imagick Arabic Text Direction Problem
Hİ, everyone.
I have a project and I need to download images. While there is no problem in normal writings, there is an error in the Arabic word. setTextDirection command not working. Can you help me please?
Example; text that looks like this, "دور المصارف الإسلامية في تمويل التجارة الخارجية في ضوء معايير هيئة المحاسبة والمراجعة "أيوفي
looks like the below image,

Can you help me please?
Maybe...can you:
i) Do another example with a shorter word like 'المعذرة' - I can't read Arabic so a simpler example would be easier for me to grok.
ii) Say how you installed Imagick?
iii) Confirm whether the problem exists or not with the roman alphabet, or is it just the right-to-left Arabic text
Oh, and just in case, post a simple code example of what you're doing.
First of all thanks for the reply.
example arabic text is دوالمصل
this is the wrong view,

my code is,
$articleTitleDraw = new \ImagickDraw(); $articleTitleDraw->setFontSize($fontSize); $articleTitleDraw->setFont('../app/fonts/arial.ttf'); $articleTitleWrapped = $this->wordWrapAnnotation($image, $articleTitleDraw, $article->getTitleByMandatoryLanguage(), $maxWidth); foreach ($articleTitleWrapped as $index => $titlePart) { if ($index === 0) { $titleX = 750; } else { $titleX = $x + 520; } $image->annotateImage($articleTitleDraw, $titleX, $y, 0, $titlePart); $y += $yWrapMargin; }
/** * @param $image * @param $draw * @param $text * @param $maxWidth * @return array */ protected function wordWrapAnnotation($image, $draw, $text, $maxWidth): array { $text = trim($text); $words = preg_split('%\s%', $text, -1, PREG_SPLIT_NO_EMPTY); $lines = array(); $i = 0; $lineHeight = 0; while (count($words) > 0) { $metrics = $image->queryFontMetrics($draw, implode(' ', array_slice($words, 0, ++$i))); $lineHeight = max($metrics['textHeight'], $lineHeight); if ($metrics['textWidth'] > $maxWidth || count($words) < $i) { if ($i === 1) { $i++; } $lines[] = implode(' ', array_slice($words, 0, --$i)); $words = array_slice($words, $i); $i = 0; } } return $lines; }
I am waiting for your answers... :)
Yeah, I can't get it to work either.
btw, your example doesn't actually use setTextDirection. So this is what a Short, Self Contained, Correct (Compilable), Example looks like for me:
<?php
declare(strict_types=1);
$draw = new \ImagickDraw();
$draw->setFontSize($fontSize = 72);
$draw->setFont('../../fonts/Arial.ttf');
$draw->setFillColor('white');
$draw->setStrokeColor('black');
$draw->setStrokeWidth(2);
$imagick = new Imagick();
$imagick->newPseudoImage(
768,
768,
"canvas:grey"
);
$words = [
"Hello",
"دوالمصل"
];
$directions = [
Imagick::DIRECTION_LEFT_TO_RIGHT,
Imagick::DIRECTION_RIGHT_TO_LEFT,
];
$count = 1;
foreach ($words as $word) {
foreach ($directions as $direction) {
$draw->setTextDirection($direction);
$imagick->annotateImage($draw, 50, 100 * $count, 0, $word);
$count += 1;
}
}
$imagick->writeImage("./output.png");
which produces the output:

I'll have to open an issue upstream.
btw, why do you want to change the direction of the text? I'm not at all sure it's a sensible thing to do for any font/script that is joined up.
thank you so much for replying. I got complaints from users because arabic texts are written from right to left. I need to type texts starting from the right. @Danack
In your code too, the text seems to start from the left. @Danack
are you there? @Danack
Hello to everyone. This is how I solved my above problem. Maybe it will help you too. https://github.com/tawfekov/ar-php.git. I installed this library and it decoded the arabic text. I am using Symfony 2.8.
Thanks you for library @tawfekov .
Thank you for your interest @Danack