12

My previous related question:

php work with images : write complete word in arabic , ttf font

My problem was:

  • If I want to write احمد in image it appears as د م ح ا
  • Well, I fixed it and now the output: ا ح م د

Using this function:

function arab($word){

       $w = explode(' ',$word) ;

       $f = array(array('ا','أ'),'ب','ت','ث','ج','ح','د','ذ','ر','ز','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ك','ل','م','ن','ه','و','ى');

       $t = array(array('ا_','أ_'),'ب_','ت_','ث_','ج_','ح_','د_','ذ_','ر_','ز_','س_','ش_','ص_','ض_','ط_','ظ_','ع_','غ_','ف_','ق_','ك_','ل_','م_','ن_','ه_','و_','ى_');

       $my_arab = '' ;

       foreach($w as $wo)
        {
             $r  = array() ;

             $wo = str_replace($f , $t ,$wo);

             $ne = explode('_', $wo) ;

             foreach($ne as $new) {
                $new = str_replace('_','',$new) ;
                array_unshift($r , $new);
             }

            $my_arab .=  ' '.implode('',$r) ;

        }

     return trim($my_arab) ;

}

But the new problem is:

ا ح م د

(separated letters) where it should be:

احمد

How can I fix this?

user.dz
  • 797
  • 2
  • 14
  • 34
Passer By
  • 181
  • 1
  • 11
  • Sorry, I don't understand the question. If you want `احمد` to appear as `احمد`, why do you route it through this function? – JJJ Oct 15 '11 at 20:12
  • Because it apper like `ا ح م د` its right but with spaces between letters – Passer By Oct 15 '11 at 20:15
  • SO seems to mutilate the Arabic string, the `$f` and `$t` lines are not valid syntax as printed, if you C&P into a normal editor they come back to normal. – Victory Sep 02 '14 at 09:53

3 Answers3

9

Your way of reversing Arabic characters does not take into account the nature of connected glyphs. However, it is a valid trick to solve the issue of PHP/GD not automatically supporting RTL languages like Arabic.

What you need to do is to use ar-php library that does exactly what you intended.

Make sure your PHP file encoding is in unicode/UTF.
e.g. > open Notepad > Save As > encoding as UTF-8:

enter image description here

Sample usage for Arabic typography in PHP using imagettftext:

<?php 
    // The text to draw
    require('./I18N/Arabic.php'); 
    $Arabic = new I18N_Arabic('Glyphs'); 
    $font = './DroidNaskh-Bold.ttf';
    $text = $Arabic->utf8Glyphs('لغةٌ عربيّة');

    // Create the image
    $im = imagecreatetruecolor(600, 300);

    // 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, 599, 299, $white);

    // Add the text
    imagettftext($im, 50, 0, 90, 90, $black, $font, $text);

    // Using imagepng() results in clearer text compared with imagejpeg()
    imagepng($im, "./output_arabic_image.png");
    echo 'open: ./output_arabic_image.png';
    imagedestroy($im); 
?>

Outputs:

enter image description here

Nasser Al-Wohaibi
  • 4,102
  • 1
  • 33
  • 26
  • In case someone is willing to integrate this with Laravel , add the file path to your **composer.json** , under **"classmap" section** . [more info](https://laracasts.com/discuss/channels/general-discussion/include-a-non-composer-lib?page=1) – emonik Feb 24 '16 at 18:14
3

you must use bidi converter i use it to write persian in images

<?php
#----------------------------------------------------------------------
# Persian Image 1
#----------------------------------------------------------------------
# Copyright (c) 2011 Saeed Arab Sheybani
#----------------------------------------------------------------------
# This program is under the terms of the GENERAL PUBLIC LICENSE (GPL)
# as published by the FREE SOFTWARE FOUNDATION. The GPL is available
# through the world-wide-web at http://www.gnu.org/copyleft/gpl.html
#----------------------------------------------------------------------
# Authors: Saeed Arab Sheybani <webrefer@Gmail.com>
# Thanks to TCPDF project @ http://www.tecnick.com/ i use unicode_data.php and bidi.php from this site
#----------------------------------------------------------------------

/**
 * A function to change persian or arabic text from its logical condition to visual
 *
 * @author      Saeed Arab Sheybani <webrefer@Gmail.com>
 * @param       string  Main text you want to change it
 * @param       boolean Apply e'raab characters or not? default is true
 * @param       boolean Which encoding? default it "utf8"
 * @param       boolean Do you want to change special characters like "allah" or "lam+alef" or "lam+hamza", default is true
 */
function Persian_image(&$str)
{
    include_once('bidi.php');

    $text = explode("\n", $str);

    $str = array();

    foreach($text as $line){
        $chars = bidi::utf8Bidi(bidi::UTF8StringToArray($line), 'R');
        $line = '';
        foreach($chars as $char){
            $line .= bidi::unichr($char);
        }

        $str[] = $line;
    }

    $str = implode("\n", $str);
}
  • This worked much better for me than the solution above. Solution above with ar-php trashed some characters, notably the non-western numerals that were created by a date formatted by Android system. – blackjack75 Oct 06 '16 at 00:50
2

I Used this whitout any problem: https://github.com/omid/Persian-Log2Vis

UPDATE: i forked Persian-Log2Vis and change some codes to work fine. https://github.com/tahmasebi/Persian-Log2Vis

Foad Tahmasebi
  • 1,192
  • 1
  • 14
  • 27