Jump to content

Sharpening Text


cheechm

Recommended Posts

Hi,

Thanks to Barand I have this script:

 

<?php

//GET TYPE (THAT USER WANTS)
$type = @$_GET['type'];
if ($type == false) {
    $type = 'top';
}

//GET DIMENTIONS (THAT USER WANTS)
$height = intval(@$_GET['height']);
if ($height == 0) {
    $height = 800;
}
$width = intval(@$_GET['width']);
if ($width == 0) {
    $width = 1600;
}

//GET HEX COLOURS (THAT USER WANTS)
$start_colour = $_GET['start_colour'];
if ($start_colour == false) {
    $start_colour = '000000';
}
$end_colour = $_GET['end_colour'];
if ($end_colour == false) {
    $end_colour = 'FF0000';
}

//CONVERT HEX COLOURS TO RGB
$hex_r  = substr($start_colour, 0, 2);
$hex_g = substr($start_colour, 2, 2);
$hex_b = substr($start_colour, 4, 2);

$start_r = hexdec($hex_r);
$start_g = hexdec($hex_g);
$start_b = hexdec($hex_b);

$hex_r  = substr($end_colour, 0, 2);
$hex_g = substr($end_colour, 2, 2);
$hex_b = substr($end_colour, 4, 2);

$end_r = hexdec($hex_r);
$end_g = hexdec($hex_g);
$end_b = hexdec($hex_b);

//CREATE BLANK IMAGE
$image = @imagecreate($width, $height) or die("Cannot Initialize new GD image stream");

if ($type == 'top') {
    
    //LOOP THROUGH ALL THE PIXELS
    for($y = 0; $y < $height; $y++) {
        
        //LOOP THROUGH ROW
        for($x=0; $x < $width; $x++) {
            
            //CALCULATE THIS ROWS RGB COLOURS
            
            if ($start_r == $end_r) {
                $new_r = $start_r;
            }
            $difference = $start_r - $end_r;
            $new_r = $start_r - intval(($difference / $height) * $y);
            
            //====
            
            if ($start_g == $end_g) {
                $new_g = $start_g;
            }
            $difference = $start_g - $end_g;
            $new_g = $start_g - intval(($difference / $height) * $y);
            
            //===
            
            if ($start_b == $end_b) {
                $new_b = $start_b;
            }
            $difference = $start_b - $end_b;
            $new_b = $start_b - intval(($difference / $height) * $y);
            
            //===
            
            //ALLOCATE THE COLOR
            $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
            
            //CREATE ROW OF THIS COLOR
            imagesetpixel($image, $x, $y, $row_color);
            
        }
        
    }

}

if ($type == 'left') {
    
    //LOOP THROUGH ALL THE PIXELS
    for($x = 0; $x < $width; $x++) {
        
        //LOOP THROUGH COLUMN
        for($y=0; $y < $height; $y++) {
            
            //CALCULATE THIS ROWS RGB COLOURS
            
            if ($start_r == $end_r) {
                $new_r = $start_r;
            }
            $difference = $start_r - $end_r;
            $new_r = $start_r - intval(($difference / $width) * $x);
            
            //====
            
            if ($start_g == $end_g) {
                $new_g = $start_g;
            }
            $difference = $start_g - $end_g;
            $new_g = $start_g - intval(($difference / $width) * $x);
            
            //===
            
            if ($start_b == $end_b) {
                $new_b = $start_b;
            }
            $difference = $start_b - $end_b;
            $new_b = $start_b - intval(($difference / $width) * $x);
            
            //===
            
            //ALLOCATE THE COLOR
            $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
            
            //CREATE ROW OF THIS COLOR
            imagesetpixel($image, $x, $y, $row_color);
            
        }
        
    }
    
}

$bgcol = imagecolorallocate($image, 255,255,255);
$mask = imagecreate ($width, $height);                                  // mask image
$maskbg = imagecolorallocate($mask, 255,255,255);
$masktextcol = imagecolorallocate($mask, 0,0,0);
imagettftext($mask, 500, 0, 10, 500, $masktextcol, 'c:/windows/fonts/calibri.ttf', 'Nick');


for ($x=0; $x<$width; $x++) {
    for ($y=0; $y<$height; $y++) {
        if (imagecolorat($mask,$x,$y) == $maskbg) 
        {
            imagesetpixel($image,$x,$y,$bgcol);
        }
    }
}
$small = imagecreate(800,400);
imagecopyresampled($small,$image,0,0,0,0,800,400,$width, $height);

$tc = imagecolorat($small, 0, 0);
imagecolortransparent($small, $tc);
header("content-type: image/png");
imagepng($small);
imagedestroy($mask);
imagedestroy($image);
imagedestroy($small);
?>

 

which creates an image like this:

 

a2ve2t.png

 

 

How can I make the letters smoother than that. I guess there are ideal variables?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/102970-sharpening-text/
Share on other sites

You should PM Barand next time for something like this because he's really good with GD.  And it could just be the font that's causing the letters to look like that.

That could be one thing, another thing he could try is to try adding

<?php
imagealphablending($image, true);
imagesavealpha($image, true);
?>

After the $image = imagecreatetruecolor($width, $height);

 

Line... that's one big suggestion from me.

 

That's what I use for a dynamic script I use in my site, it produces like this:

 

fontres.php?size=72&font=i_did_this&text=Hello%20Nick&shadow=10&fg=FFFFFF&sd=000000

Link to comment
https://forums.phpfreaks.com/topic/102970-sharpening-text/#findComment-528334
Share on other sites

Could you send me the source code of that please?

 

Adding those 2 settings didn't change anything.

 

Thanks for trying though.

<?php
/*
Font Image Creation
Coded for use with the
Henesys.com Drag-&-Drop
Interface
***********************
Coded By: DarkAngel
DarkerAngel ( at ) gmail ( dot ) com
***********************
(c) Henesys.com
*/
$font_base = "font/";
$size = better_imagettfbbox($_GET['size'], 0, $font_base.$_GET['font'].".ttf", $_GET['text']);
//print_r($size);
$x = abs($size[6]) + abs($size[4]) + $_GET['shadow'];
$y = abs($size[3]) + abs($size[5]) + $_GET['shadow'];
$im = imagecreatetruecolor($x,$y);
imagealphablending($im, true);
imagesavealpha($im, true);
$fg = hexcolor2rgb($_GET['fg']);
$sd = hexcolor2rgb($_GET['sd']);
$bg = imagecolorallocatealpha($im, 128, 128, 128, 127);
$fg = imagecolorallocate($im, $fg['r'], $fg['g'], $fg['b']);
$sd = imagecolorallocate($im, $sd['r'], $sd['g'], $sd['b']);
imagefill($im, 1, 1, $bg);
imagettftext($im, $_GET['size'], 0,abs($size[6])+$_GET['shadow'], abs($size[7])+$_GET['shadow'], $sd, $font_base.$_GET['font'].".ttf", $_GET['text']);
imagettftext($im, $_GET['size'], 0,abs($size[6]), abs($size[7]), $fg, $font_base.$_GET['font'].".ttf", $_GET['text']);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

function better_imagettfbbox($size, $angle, $font, $text) {
  $dummy = imagecreate(1, 1);
  $black = imagecolorallocate($dummy, 0, 0, 0);
  $bbox = imagettftext($dummy, $size, $angle, 0, 0, $black, $font, $text);
  imagedestroy($dummy);
  return $bbox;
}
//HTML Color > Tri INT R,B,G
function hexcolor2rgb($color) {
$color = str_replace('#', '', $color);
$r = hexdec(substr($color, 0, 2));
$g = hexdec(substr($color, 2, 2));
$b = hexdec(substr($color, 4, 2));
return array( 'r' => $r, 'g' => $g, 'b' => $b);
}
?>

 

That's the entire code for the above image

Link to comment
https://forums.phpfreaks.com/topic/102970-sharpening-text/#findComment-528351
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.