Practical PHP

  • Increase font size
  • Default font size
  • Decrease font size
Home PHP Scripts and examples How to make a mirrored text in php

How to make a mirrored text in php

E-mail Print

 Sometimes we want to handle text, but we want to present that text in a much cooler way than what is possible with CSS and HTML. We want a really cool designer that gives us fresly photoshopped headers for all our stories. But somehow we do not get it, as it takes a lot of time to make all text into graphics, it's not easy, and in case of a redesign everything will have to be done again.. So we never actually do this. Until now!

This scripts generates a picture  that contains your text as a PNG-image, with a transparent reflection underneath. It's like a photoshop-light for those of us that only care about headlines on our web..

 

Font:

The font used in this example is "Bauer", picked from 1001freefonts.com

As the domain name implies they have more than one to pick from. I chose this at random.

 Example:

The graphics were generated from http://www.practical-php.com/practical-php-scripts/shadowtext/generate_shadowtext.php?size=30&text=Practical-php.com

If you look at the url - change the text to make your own, and change the size=30 to something else to get another font-size.

There is a small amount of logic in the script:

- it caches all the images it generates. You may not want this, and it can be disabled. 

- The font size is limited to a minimum of 5 and a maximum of 100

 

 

 

The script for this is the following:

You only have to modify the two first lines to make it work on your system.

 


 

    DEFINE ("CACHED_PICTURE_FILE_PATH"     , "path/to/cache/directory");
    DEFINE("GRAPHICS_FONT"              , "/full path to your .ttf font");

   // practical-php.com refleted graphics script

    $text =(html_entity_decode(rawurldecode($_GET["text"])));
    $text = $_GET["text"];
    $text or $text="practical-php.com";

    // we know what we want now - let us see if we have it cached on disk
    $cached_file = CACHED_PICTURE_FILE_PATH .str_replace("/","_", $text . $_REQUEST["size"] .".png");

    // if the file is cached - dump it out and die.
     if(file_exists($cached_file)){
           header("Content-type: image/png");
          readfile($cached_file);
         die;
       }

   // What font face and size to use
    $ttfont = GRAPHICS_FONT;
   if($_GET["size"] and $_GET["size"] > 5 and $_GET["size"]< 100){
            $ttsize = $_GET["size"];
    }else{
            $ttsize = 14;
    }

    $shadowheight_factor = 1.6;
    // Calculate the bounding box of the given string with
    // the above font parameters
    $ttbbox = imagettfbbox($ttsize, 0, $ttfont, $text);

    $ttwidth = $ttbbox[2] - $ttbbox[0];
    $ttheight = $ttbbox[3] - $ttbbox[5];

     // calculate image size:
    $img_x = $ttwidth + 20;
    $img_y = $ttheight * $shadowheight_factor;

    // make a blank image
     header("Content-type: image/png");
    $new_width=$img_x;
    $new_height=$img_y;
    $im = imagecreatetruecolor($new_width,$new_height);
    $bgColor = imagecolorallocate($im, 255, 255, 255);
    imagefilledrectangle($im, 0, 0, $new_width-1, $new_height-1, $bgColor);
    $background_color = imagecolorallocate($im, 255, 255, 255);
  

   // make white transparent
    imagecolortransparent($im, $background_color);

    // insert text
    $topstart = $ttsize +7;
    $text_color = ImageColorAllocate($im, 0, 0, 0);
    ImageTTFText($im, $ttsize, 0, 1, $topstart, $text_color, $ttfont, $text);

    // mirror text
    $img_width = imagesx($im);
    $img_height = imagesy($im);
    $start_y = $topstart ;
    $end_y = $start_y - $ttsize;

    //set height of shadow
    $mirror_height= $ttsize/$shadowheight_factor;

    // avoid overflows
    if($mirror_height > $img_height){
       $mirror_height = $img_height;
    }
    // visibility from/ to
    $visible_from =127;
    $visibility = 50;
    $visibility_step = intval($visible_from / $mirror_height );

    // mirror one and one line
    for($y=0 ; $y<$mirror_height ; $y++ ){

        // modify tranparency for each line
        $visibility = $visibility + $visibility_step;
        if($visibility > 125){
            $visibility = 127;
        }

        for($x=0;$x<$img_width;$x++){
            $rgb = ImageColorAt($im, $x, ($start_y-$y) );
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $new_y = ($start_y +  $y) ;
            $set_color = imagecolorallocatealpha($im,$r,$g,$b,$visibility);
            imagesetpixel($im, $x, $new_y, $set_color);
        }
    }

     // show image
    imagepng($im);
    $scratchname =  $cached_file;
    imagepng($im,$scratchname);
    imagedestroy($im);

 

Main Menu


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /usr/www/users/infostil/www.practical-php.com/libraries/joomla/cache/handler/callback.php on line 99