How to create custom script to convert ttf, otf or font file to png image via image magic script?


Please check that image magick must be install on your server

please note /usr/bin/convert this path could be change which is define below of script to find out the
path of image magick
make a new php page and paste this code on this page
<?php
echo "<pre>";
system("type convert");
echo "</pre>";
?>

and run the page and can check that path is right or not if path is changed so please change the path from
 /usr/bin/convert this path


Create custom script to convert ttf, otf or font file to png image via image magic script?
this script will generate the image of ttf ,otf, .pfm , pfb or font file.

simply copy and paste this code on your browser.


<?php
set_time_limit(0);
ini_set('max_execution_time', 0);
if($_SERVER['HTTP_HOST']=="localhost")
{
$DB["dbName"] = "fontxs";
$DB["host"] = "localhost";
$DB["user"] = "root";
$DB["pass"] = "";
    $SITE_PATH = 'http://localhost/fontxs/';
$SSL_PATH = '';
$SITE_FS_PATH = '';
$local_mode = false;
}
$link = mysql_connect($DB["host"], $DB["user"], $DB["pass"]) or die("<span style='FONT-SIZE:11px; FONT-COLOR: #000000; font-family=tahoma;'><center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>");

$rs_link = mysql_select_db($DB["dbName"]) or die( mysql_error());

$result = mysql_query("select * from tbl_font where image_status = '0' and font_approved = '1' order by font_id limit 30");



$ii = 0;

while($row=mysql_fetch_array($result))
{
$ii++;
echo $ii."<br/>";

   $file_upload_path1 = "path of font file (ttf,otf)";

generateimage($file_upload_path1, $row['font_id']);

generateimageabc($file_upload_path1, $row['font_id']);

generateimagesymbol($file_upload_path1, $row['font_id']);

$update= mysql_query("update  tbl_font set image_status = '1'  where font_id = '".$row['font_id']."'");

}





     function generateimage($file_upload_path1, $id) {

        $total = "";
        $str = "";
        $i = "";
        $hor_file_list = "";
        $font = $file_upload_path1;

        $arr1 = array('0065' => 'A', '0066' => 'B', '0067' => 'C', '0068' => 'D', '0069' => 'E', '0070' => 'F', '0071' => 'G', '0072' => 'H', '0073' => 'I', '0074' => 'J', '0075' => 'K', '0076' => 'L', '0077' => 'M', '0078' => 'N', '0079' => 'O', '0080' => 'P', '0081' => 'Q', '0082' => 'R', '0083' => 'S', '0084' => 'T', '0085' => 'U', '0086' => 'V', '0087' => 'W', '0088' => 'X', '0089' => 'Y', '0090' => 'Z');

        foreach ($arr1 as $key => $val) {
            $string = $val;
            $size = 36;
            $angle = 0;
            $fontfile = $font;
            $fontfile1 = "font.ttf";
            $dimensions = imagettfbbox($size, $angle, $fontfile, $string);
            $width = $dimensions[2] + 40;

            $total+=$width;

            if ($total <= 700) {
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 18 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $val . '" font__' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 12 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $key . '" font_' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x60 xc:white -font ' . $fontfile . ' -pointsize 42 \
-fill black -undercolor white  -gravity center -annotate 0+10 "' . $val . '" font' . $key . '.png');


                $file = "font__$key.png " . "font_$key.png " . " font$key.png";
                exec('/usr/bin/convert ' . $file . ' -append  ' . 'font___' . $key . '.png');

                unlink("font__$key.png");
                unlink("font$key.png");
                unlink("font_$key.png");
                $str.="font___$key.png ";
            } else if ($total > 700) {
                $i++;
                $arr_img[$i] = $str;
                $total = $width;
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 18 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $val . '" font__' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 12 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $key . '" font_' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x60 xc:white -font ' . $fontfile . ' -pointsize 42 \
-fill black -undercolor white  -gravity center -annotate 0+10 "' . $val . '" font' . $key . '.png');

                $file = "font__$key.png " . "font_$key.png " . " font$key.png";
                exec('/usr/bin/convert ' . $file . ' -append  ' . 'font___' . $key . '.png');
                unlink("font__$key.png");
                unlink("font$key.png");
                unlink("font_$key.png");
                $str = "font___$key.png ";
            }
        }

        if ($total > 0) {
            $i++;
            $arr_img[$i] = $str;
            $total = 0;
            $str = "";
        }

        foreach ($arr_img as $key => $val) {
            $horizontal_file_name = "hor_file_" . $key . ".png";
            exec('/usr/bin/convert ' . $val . ' +append  ' . $horizontal_file_name);

            $wordChunksLimited = explode(" ", $val);

            for ($i = 0; $i < count($wordChunksLimited) - 1; $i++) {

                unlink("$wordChunksLimited[$i]");
            }



            $hor_file_list.=$horizontal_file_name . " ";
        }



        exec('/usr/bin/convert ' . $hor_file_list . ' -append  ' . 'temp/final_' . $id . '.png');

        $horizontal = explode(" ", $hor_file_list);

        for ($i = 0; $i < count($horizontal) - 1; $i++) {

            unlink("$horizontal[$i]");
        }
    }


     function generateimageabc($file_upload_path1, $id) {

        $total = "";
        $str = "";
        $i = "";
        $hor_file_list = "";
        $font = $file_upload_path1;

        //  $arr1 = array('0065' => 'A', '0066' => 'B', '0067' => 'C', '0068' => 'D', '0069' => 'E', '0070' => 'F', '0071' => 'G', '0072' => 'H', '0073' => 'I', '0074' => 'J', '0075' => 'K', '0076' => 'L', '0077' => 'M', '0078' => 'N', '0079' => 'O', '0080' => 'P', '0081' => 'Q', '0082' => 'R', '0083' => 'S', '0084' => 'T', '0085' => 'U', '0086' => 'V', '0087' => 'W', '0088' => 'X', '0089' => 'Y', '0090' => 'Z');
        $arr1 = array('0097' => 'a', '0098' => 'b', '0099' => 'c', '0100' => 'd', '0101' => 'e', '0102' => 'f', '0103' => 'g', '0104' => 'h', '0105' => 'i', '0106' => 'j', '0107' => 'k', '0108' => 'l', '0109' => 'm', '0110' => 'n', '0111' => 'o', '0112' => 'p', '0113' => 'q', '0114' => 'r', '0115' => 's', '0116' => 't', '0117' => 'u', '0118' => 'v', '0119' => 'w', '0120' => 'x', '0121' => 'y', '0122' => 'z');


        foreach ($arr1 as $key => $val) {
            $string = $val;
            $size = 36;
            $angle = 0;
            $fontfile = $font;
            $fontfile1 = "font.ttf";
            $dimensions = imagettfbbox($size, $angle, $fontfile, $string);
            $width = $dimensions[2] + 40;

            $total+=$width;

            if ($total <= 700) {
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 18 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $val . '" font__' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 12 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $key . '" font_' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x60 xc:white -font ' . $fontfile . ' -pointsize 42 \
-fill black -undercolor white  -gravity center -annotate 0+10 "' . $val . '" font' . $key . '.png');


                $file = "font__$key.png " . "font_$key.png " . " font$key.png";
                exec('/usr/bin/convert ' . $file . ' -append  ' . 'font___' . $key . '.png');

                unlink("font__$key.png");
                unlink("font$key.png");
                unlink("font_$key.png");
                $str.="font___$key.png ";
            } else if ($total > 700) {
                $i++;
                $arr_img[$i] = $str;
                $total = $width;
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 18 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $val . '" font__' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 12 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $key . '" font_' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x60 xc:white -font ' . $fontfile . ' -pointsize 42 \
-fill black -undercolor white  -gravity center -annotate 0+10 "' . $val . '" font' . $key . '.png');

                $file = "font__$key.png " . "font_$key.png " . " font$key.png";
                exec('/usr/bin/convert ' . $file . ' -append  ' . 'font___' . $key . '.png');
                unlink("font__$key.png");
                unlink("font$key.png");
                unlink("font_$key.png");
                $str = "font___$key.png ";
            }
        }

        if ($total > 0) {
            $i++;
            $arr_img[$i] = $str;
            $total = 0;
            $str = "";
        }

        foreach ($arr_img as $key => $val) {
            $horizontal_file_name = "hor_file_" . $key . ".png";
            exec('/usr/bin/convert ' . $val . ' +append  ' . $horizontal_file_name);

            $wordChunksLimited = explode(" ", $val);

            for ($i = 0; $i < count($wordChunksLimited) - 1; $i++) {

                unlink("$wordChunksLimited[$i]");
            }



            $hor_file_list.=$horizontal_file_name . " ";
        }



        exec('/usr/bin/convert ' . $hor_file_list . ' -append  ' . 'temp/final_a' . $id . '.png');

        $horizontal = explode(" ", $hor_file_list);

        for ($i = 0; $i < count($horizontal) - 1; $i++) {

            unlink("$horizontal[$i]");
        }
    }

    function generateimagesymbol($file_upload_path1, $id) {

        $total = "";
        $str = "";
        $i = "";
        $hor_file_list = "";
        $font = $file_upload_path1;

        //$arr1 = array('0065' => 'A', '0066' => 'B', '0067' => 'C', '0068' => 'D', '0069' => 'E', '0070' => 'F', '0071' => 'G', '0072' => 'H', '0073' => 'I', '0074' => 'J', '0075' => 'K', '0076' => 'L', '0077' => 'M', '0078' => 'N', '0079' => 'O', '0080' => 'P', '0081' => 'Q', '0082' => 'R', '0083' => 'S', '0084' => 'T', '0085' => 'U', '0086' => 'V', '0087' => 'W', '0088' => 'X', '0089' => 'Y', '0090' => 'Z');
        $arr1 = array('0122' => '~', '0122' => '`',
            '0033' => '!', '0122' => '@', '0035' => '#', '0036' => '$', '0037' => '%', '0094' => '^', '0038' => '&', '0042' => '*', '0040' => '(', '0041' => ')', '0045' => '-', '0095' => '_', '0061' => '=', '0043' => '+', '0124' => '|', '0047' => '/', '0062' => '>', '0060' => '<', '0096' => '.', '0125' => '{', '0123' => '}', '0091' => '[', '0093' => ']');

        foreach ($arr1 as $key => $val) {
            $string = $val;
            $size = 36;
            $angle = 0;
            $fontfile = $font;
            $fontfile1 = "font.ttf";
            $dimensions = imagettfbbox($size, $angle, $fontfile, $string);
            $width = $dimensions[2] + 40;

            $total+=$width;

            if ($total <= 700) {
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 18 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $val . '" font__' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 12 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $key . '" font_' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x60 xc:white -font ' . $fontfile . ' -pointsize 42 \
-fill black -undercolor white  -gravity center -annotate 0+10 "' . $val . '" font' . $key . '.png');


                $file = "font__$key.png " . "font_$key.png " . " font$key.png";
                exec('/usr/bin/convert ' . $file . ' -append  ' . 'font___' . $key . '.png');

                unlink("font__$key.png");
                unlink("font$key.png");
                unlink("font_$key.png");
                $str.="font___$key.png ";
            } else if ($total > 700) {
                $i++;
                $arr_img[$i] = $str;
                $total = $width;
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 18 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $val . '" font__' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x20 xc:white -font ' . $fontfile1 . ' -pointsize 12 \
-fill grey -undercolor white -gravity center -annotate 0+10 "' . $key . '" font_' . $key . '.png');
                exec('/usr/bin/convert -border 0 -size ' . $width . 'x60 xc:white -font ' . $fontfile . ' -pointsize 42 \
-fill black -undercolor white  -gravity center -annotate 0+10 "' . $val . '" font' . $key . '.png');

                $file = "font__$key.png " . "font_$key.png " . " font$key.png";
                exec('/usr/bin/convert ' . $file . ' -append  ' . 'font___' . $key . '.png');
                unlink("font__$key.png");
                unlink("font$key.png");
                unlink("font_$key.png");
                $str = "font___$key.png ";
            }
        }

        if ($total > 0) {
            $i++;
            $arr_img[$i] = $str;
            $total = 0;
            $str = "";
        }

        foreach ($arr_img as $key => $val) {
            $horizontal_file_name = "hor_file_" . $key . ".png";
            exec('/usr/bin/convert ' . $val . ' +append  ' . $horizontal_file_name);

            $wordChunksLimited = explode(" ", $val);

            for ($i = 0; $i < count($wordChunksLimited) - 1; $i++) {

                unlink("$wordChunksLimited[$i]");
            }



            $hor_file_list.=$horizontal_file_name . " ";
        }



        exec('/usr/bin/convert ' . $hor_file_list . ' -append  ' . 'temp/final_s' . $id . '.png');

        $horizontal = explode(" ", $hor_file_list);

        for ($i = 0; $i < count($horizontal) - 1; $i++) {

            unlink("$horizontal[$i]");
        }
    }
?>

this script will generate the image like this

image1.png
image2.png
image3.png


Please note: place a save a ttf font file on root folder of or script file which file name should be font.ttf 
and create a folder on same location which name will be temp.
thanks for come on my blog :)

Comments

  1. Script is too large but work for me thanks for this

    ReplyDelete

Post a Comment