PHP/Graphics Image/Draw String

Материал из Web эксперт
Перейти к: навигация, поиск

Draw char with color

<html>
<body>
                              
<?php
    $img=ImageCreate(200,200);
    $bgcolor=ImageColorAllocate($img,200,200,200);
    $red=ImagecolorAllocate($img,255,0,0); 
    $green=ImagecolorAllocate($img,0,255,0); 
    $blue=ImagecolorAllocate($img,0,0,255); 
    $grey=ImagecolorAllocate($img,50,50,50); 
    $black=ImagecolorAllocate($img,0,0,0); 
                                                            
    ImageChar($img,0,20,20,"S",$red);
    ImageChar($img,1,20,40,"S",$green);
    ImageChar($img,2,20,60,"S",$blue);
    ImageChar($img,3,20,80,"S",$grey);
    ImageChar($img,4,20,100,"S",$black);
    ImageChar($img,5,20,120,"S",$red);
                                                            
    ImageCharUp($img,0, 20,160,"S",$red);
    ImageCharUp($img,1, 40,160,"S",$green);
    ImageCharUp($img,2, 60,160,"S",$blue);
    ImageCharUp($img,3, 80,160,"S",$grey);
    ImageCharUp($img,4,100,160,"S",$black);
    ImageCharUp($img,5,120,160,"S",$red);
                                                            
    /*
    RGB Red = 0..255 , Green = 0..255 , Blue = 0..255.
     */
    ImageSetPixel($img,50,50,$pixelcolor);
    ImagePNG($img,"pic.png");
    ImageDestroy($img);
?>
<img src="pic.png" border=0>
</body>
</html>



Drawing a Square and a string

<?php
     header ("Content-type: image/png");
   
     $im = ImageCreate (150, 150);
     $grey = ImageColorAllocate ($im, 230, 230, 230); // background color
     $black = ImageColorAllocate ($im, 0, 0, 0);
   
     ImageRectangle($im, 40, 40, 140, 140, $black);
     ImageString($im, 3, 5, 5, "Figure 18.3: Square", $black);
     ImagePng ($im);
     ImageDestroy ($im);
?>



Draw string and fill rectangle

<?php
  header("Content-type: image/gif");
  $imgcord = imagecreate(500,500);
  
  $blue = imagecolorallocate($imgcord, 0,0,255);
  $red = imagecolorallocate($imgcord, 255,0,0);
  ImageRectangle($imgcord, 175,165, 200,225, $blue);
  ImageFill($imgcord, 0,0, $red);
  ImageString($imgcord, 3,170, 220,"www.wbex.ru", $blue);
  ImagePNG($imgcord);
  ImageDestroy($imgcord);
?>



Paint string value with different colors and position

<html>
<body>
<?php
   $img=ImageCreate(300,300);
   $bgcolor=ImageColorAllocate($img,200,200,200);
   $red=ImagecolorAllocate($img,255,0,0); 
   $green=ImagecolorAllocate($img,0,255,0); 
   $blue=ImagecolorAllocate($img,0,0,255); 
   $grey=ImagecolorAllocate($img,50,50,50); 
   $black=ImagecolorAllocate($img,0,0,0); 
                                                            
                                                            
   ImageString($img,0,20,20,"www.wbex.ru",$red);
   ImageString($img,1,20,40,"www.wbex.ru",$green);
   ImageString($img,2,20,60,"www.wbex.ru",$blue);
   ImageString($img,3,20,80,"www.wbex.ru",$grey);
   ImageString($img,4,20,100,"www.wbex.ru",$black);
   ImageString($img,5,20,120,"www.wbex.ru",$red);
                                                            
   ImageStringUp($img,0,50,200,"www.wbex.ru",$red);
   ImageStringUp($img,1,100,200,"www.wbex.ru",$green);
   ImageStringUp($img,2,150,200,"www.wbex.ru",$blue);
   ImageStringUp($img,3,200,200,"www.wbex.ru",$grey);
   ImageStringUp($img,4,250,200,"www.wbex.ru",$black);
   ImageStringUp($img,5,300,200,"www.wbex.ru",$red);
                                                            
   ImageSetPixel($img,50,50,$pixelcolor);
   ImagePNG($img,"pic.png");
   ImageDestroy($img);
?>
<img src="pic.png" border=0>
</body>
</html>



Using a Bounding Box to Center Text

<?php
     header ("Content-type: image/png");
   
     $text = "Bounding Box!";
     $font_size = 15;
     $height = 500;
     $width = 300;
   
     $im = ImageCreate ($width, $height);
     $grey = ImageColorAllocate ($im, 230, 230, 230);
     $black = ImageColorAllocate ($im, 0, 0, 0);
   
     $text_bbox = ImageTTFBBox($font_size, 0, "ARIALBD.TTF", $text);
     $image_center = $width / 2;
     $text_x = $image_center - round(($text_bbox[4]/2));
   
     ImageTTFText($im, $font_size, 0, $text_x, 10, $black, "ARIALBD.TTF", $text);
     ImagePng ($im);
     ImageDestroy ($im);
?>