Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 23
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Na początku, kiedy zobaczyłem, że ktoś chce jakiejś opłaty za pomoc w tworzeniu stron ryknąłem śmiechem - potem przyszły problemy... i zaryzykowałem. Druga rzecz to: nie chciałem "kopiować". Ale prawda jest taka: są lepsi, bardziej doświadczeni i... czasem trzeba poprosić o pomoc, a jak poświęca się na to trzecią cześć życia, to nic dziwnego, że nie chce się swoich "sekretów" zdradzać za darmo. Skorzystałem z "algorytmy.pl" i naprawdę jestem z tego w 100% zadowolony, polecam - dla zawodowców (co się uczą) i amatorów (można skorzystać z gotowego rozwiązania).

Tomasz Czypicki
Cybernoxa

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

imageline

(PHP 3, PHP 4, PHP 5)

imageline -- Draw a line

Description

int imageline ( resource image, int x1, int y1, int x2, int y2, int color )

imageline() draws a line from x1, y1 to x2, y2 (top left is 0, 0) in image image of color color.

Przykład 1. Drawing a thick line

<?php

function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
  
/* this way it works well only for orthogonal lines
   imagesetthickness($image, $thick);
   return imageline($image, $x1, $y1, $x2, $y2, $color);
   */
  
if ($thick == 1) {
       return
imageline($image, $x1, $y1, $x2, $y2, $color);
   }
  
$t = $thick / 2 - 0.5;
   if (
$x1 == $x2 || $y1 == $y2) {
       return
imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
   }
  
$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
  
$a = $t / sqrt(1 + pow($k, 2));
  
$points = array(
      
round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
      
round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
      
round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
      
round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
   );   
  
imagefilledpolygon($image, $points, 4, $color);
   return
imagepolygon($image, $points, 4, $color);
}

?>

See also imagecreate() and imagecolorallocate().




User Contributed Notes

fatpratmatt at gmail dot com
30-Jan-2006 08:43

Here is a function which draws lines that cross at a specific point [It may need some tweaking]:

<?php
// Image Cross by Matt Evans
// $im - image resource
// $x - x coordinate where the lines should cross
// $y - y coordinate where the lines should cross
// $size - the length of each line
// $colour - the colour of the cross

function imagecross($im, $x, $y, $size = 5, $colour) {
imageline($im, $x+$size/2, $y+$size/2, $x-$size/2, $y-$size/2, $colour);
imageline($im, $x-$size/2, $y+$size, $x+$size/2, $y-$size, $colour);
}

// Example
imagecross($im, 50, 50, 5, $crosscolour);
?>


21-Oct-2005 04:30

imageline coordinate variables are documented as int ,  a value in decimal format will be truncated.

This may be useful when, for example, applying a non-integer scaling factor in generating an image.

Care should be taken to ensure this does not create significant errors that affect the quality of the image. For example :

<?php
$x
=0.00000000001;$y=100;
imageline($img,0,0,0,$y+$x);
imageline($img,0,0,0,$y-$x);
?>

the first line will be straight , the second will have a step. Use round() where appropriate.


meid at gmx dot at
07-Sep-2005 07:39

Some simple code to draw lines with specific thickness using "imagefilledpolygon". Useful if your gdlib does not support "imagesetthickness".

<?
function dickelinie($img,$start_x,$start_y,$end_x,$end_y,$color,$thickness)
{
  
$angle=(atan2(($start_y - $end_y),($end_x - $start_x)));

  
$dist_x=$thickness*(sin($angle));
  
$dist_y=$thickness*(cos($angle));
  
  
$p1x=ceil(($start_x + $dist_x));
  
$p1y=ceil(($start_y + $dist_y));
  
$p2x=ceil(($end_x + $dist_x));
  
$p2y=ceil(($end_y + $dist_y));
  
$p3x=ceil(($end_x - $dist_x));
  
$p3y=ceil(($end_y - $dist_y));
  
$p4x=ceil(($start_x - $dist_x));
  
$p4y=ceil(($start_y - $dist_y));
  
  
$array=array(0=>$p1x,$p1y,$p2x,$p2y,$p3x,$p3y,$p4x,$p4y);
  
imagefilledpolygon ( $img, $array, (count($array)/2), $color );
}
// Example:
header ("Content-type: image/jpeg");
$img = ImageCreate (210, 210) or die("Cannot Initialize new GD image stream ");
$backgroundcolor = ImageColorAllocate ($img, 255, 255, 255);
$orange = ImageColorAllocate($img, 252, 102, 4);
dickelinie($img, 10, 10, 10, 200,$orange,2);
dickelinie($img, 10, 200, 200, 10,$orange,2);   
dickelinie($img, 200, 10, 200, 200,$orange,2);
imagejpeg($img); 
ImageDestroy($img);

?>


d [AT] sprid [DOT] de
06-Sep-2005 12:12

Here my function do clear all problems. With this, you can draw firstly smooth lines (basic code adapted from code_couturier at graffiti dot net, with some performance changes). The special is, you can define the alpha-value of the line (0 = normal smooth line, 127 = fully transparent). Change whatever you want to make it better, but post your results ;)

<?php
/**
 * function imageSmoothAlphaLine() - version 1.0
 * Draws a smooth line with alpha-functionality
 *
 * @param  ident    the image to draw on
 * @param  integer  x1
 * @param  integer  y1
 * @param  integer  x2
 * @param  integer  y2
 * @param  integer  red (0 to 255)
 * @param  integer  green (0 to 255)
 * @param  integer  blue (0 to 255)
 * @param  integer  alpha (0 to 127)
 *
 * @access  public
 *
 * @author  DASPRiD <d@sprid.de>
 */
function imageSmoothAlphaLine ($image, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha=0) {
 
$icr = $r;
 
$icg = $g;
 
$icb = $b;
 
$dcol = imagecolorallocatealpha($image, $icr, $icg, $icb, $alpha);
 
  if (
$y1 == $y2 || $x1 == $x2)
  
imageline($image, $x1, $y2, $x1, $y2, $dcol);
  else {
  
$m = ($y2 - $y1) / ($x2 - $x1);
  
$b = $y1 - $m * $x1;

   if (
abs ($m) <2) {
    
$x = min($x1, $x2);
    
$endx = max($x1, $x2) + 1;

     while (
$x < $endx) {
      
$y = $m * $x + $b;
      
$ya = ($y == floor($y) ? 1: $y - floor($y));
      
$yb = ceil($y) - $y;
  
      
$trgb = ImageColorAt($image, $x, floor($y));
      
$tcr = ($trgb >> 16) & 0xFF;
      
$tcg = ($trgb >> 8) & 0xFF;
      
$tcb = $trgb & 0xFF;
      
imagesetpixel($image, $x, floor($y), imagecolorallocatealpha($image, ($tcr * $ya + $icr * $yb), ($tcg * $ya + $icg * $yb), ($tcb * $ya + $icb * $yb), $alpha));
 
      
$trgb = ImageColorAt($image, $x, ceil($y));
      
$tcr = ($trgb >> 16) & 0xFF;
      
$tcg = ($trgb >> 8) & 0xFF;
      
$tcb = $trgb & 0xFF;
      
imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb + $icr * $ya), ($tcg * $yb + $icg * $ya), ($tcb * $yb + $icb * $ya), $alpha));
 
      
$x++;
     }
   } else {
    
$y = min($y1, $y2);
    
$endy = max($y1, $y2) + 1;

     while (
$y < $endy) {
      
$x = ($y - $b) / $m;
      
$xa = ($x == floor($x) ? 1: $x - floor($x));
      
$xb = ceil($x) - $x;
 
      
$trgb = ImageColorAt($image, floor($x), $y);
      
$tcr = ($trgb >> 16) & 0xFF;
      
$tcg = ($trgb >> 8) & 0xFF;
      
$tcb = $trgb & 0xFF;
      
imagesetpixel($image, floor($x), $y, imagecolorallocatealpha($image, ($tcr * $xa + $icr * $xb), ($tcg * $xa + $icg * $xb), ($tcb * $xa + $icb * $xb), $alpha));
 
      
$trgb = ImageColorAt($image, ceil($x), $y);
      
$tcr = ($trgb >> 16) & 0xFF;
      
$tcg = ($trgb >> 8) & 0xFF;
      
$tcb = $trgb & 0xFF;
      
imagesetpixel ($image, ceil($x), $y, imagecolorallocatealpha($image, ($tcr * $xb + $icr * $xa), ($tcg * $xb + $icg * $xa), ($tcb * $xb + $icb * $xa), $alpha));
 
      
$y ++;
     }
   }
  }
}
// end of 'imageSmoothAlphaLine()' function
?>


Tyron
02-May-2005 08:50

// Here's a function for drawing a rotated gradient Rectangle (based on a previous note)

// Create An Image 255x255
$img = ImageCreateTrueColor(255, 255);

GradientRect($img,50,50,80,80,30);

ImagePng($img,"test.png");
ImageDestroy($img);
echo "<br><img src=\"test.png\">";

function GradientRect($img, $x1, $y1, $x2, $y2, $wdt) {
  $alpha = atan2($y2-$y1,$x2-$x1);
  $real_wdt = $wdt*sin($alpha);
  $real_hgt = $wdt*cos($alpha);
  echo "real wdt:".$real_wdt;
  echo "<br>real hgt:".$real_hgt;
  echo "<br>angle: ".($angle*180/pi());
  $plotD = 0;
  $i=0;

  $dy = $real_hgt/$wdt;
  $dx = $real_wdt/$wdt;
  $drgb= 256/$wdt;
  while($i++ < $wdt) {
   // Draw a line and move it down and make it lighter to get the gradient effect
   ImageLine($img, $x1-$i*$dx, $y1+$i*$dy, $x2-$i*$dx, $y2+$i*$dy, ImageColorAllocate($img, $i*$drgb, 0, 0));
   ImageLine($img, $x1-$i*$dx+1, $y1+$i*$dy, $x2-$i*$dx+1, $y2+$i*$dy, ImageColorAllocate($img, $i*$drgb, 0, 0));

  }
}


ajreading at classixshop dot com
23-Apr-2005 04:28

<?php
// An easy bit of code showing how you can use the ImageLine() function to create gradients

// Create An Image 255x255
$img = ImageCreateTrueColor(255, 255);

$plotD = 0;
while(
$plotD < 256)
{
  
// Draw a line and move it down and make it lighter to get the gradient effect
  
ImageLine($img, 0, $plotD , 255, $plotD, ImageColorAllocate($img, $plotD, $plotD, $plotD));
  
$plotD++;
}
Header("Content-type: image/png");
ImagePng($img);
ImageDestroy($img);
?>


eviloverlord at gmail dot com
27-Jan-2005 04:50

This code is used to draw a board of hexagons (for games, classes, etc.)

<?php
//Draws a hexagonal board

// User-defined values
$maxTiles = 7;                    //The number of tiles at the center (widest part) of the board
$minTiles = 4;                    //The number of tiles at the edges of the board
$side = 30;                        //The length of the sides of the tiles in pixels
$bgColor = array(0, 0, 0);        //The background color in RGB format
$fgColor = array(255, 255, 255);//The foreground color in RGB format

//Calculated values
$widthInTiles = range($maxTiles, $minTiles);            //In our example: 7, 6, 5, 4
$rowsInTiles = count($widthInTiles)*2-1;                //the total number of rows on our board
$xSide = $side*sin(deg2rad(60));                        //the length of the x-part of the angled sides
$ySide = $side*sin(deg2rad(30));                        //the length of the y-part of the angled sides
$boardWidth = $xSide*$widthInTiles[0]*2;                //The entire width of the board
$boardHeight = $rowsInTiles*($side + $ySide) + $ySide;    //The entire height of the board

// create a blank image and allocate the foreground, background colors
$image = imagecreate($boardWidth, $boardHeight);
$bg = imagecolorallocate($image, $bgColor[0], $bgColor[1], $bgColor[2]);
$fg = imagecolorallocate($image, $fgColor[0], $fgColor[1], $fgColor[2]);

// draw the board
$row = 0;
foreach(
$widthInTiles as $tiles)
{
   for (
$i = 0; $i < $tiles+1; $i++)
   {
      
$x1 = $row*$xSide + $i*$xSide*2;
      
$y1 = $boardHeight/2;
      
$y1Dif = ($side/2) + $row*($side+$ySide);

      
$x2 = $x1 + $xSide;
      
$y2 = $y1;
      
$y2Dif = $ySide;

      
$x3 = $x2 + $xSide;

       if (
$i < $tiles)
       {
          
imageline($image, $x1, $y1 - $y1Dif, $x2, $y2 - $y1Dif - $y2Dif, $fg);
          
imageline($image$x1, $y1 + $y1Dif, $x2, $y2 + $y1Dif + $y2Dif, $fg);
          
imageline($image, $x2, $y2 - $y1Dif - $y2Dif, $x3, $y1 - $y1Dif, $fg);
          
imageline($image, $x2, $y2 + $y1Dif + $y2Dif, $x3, $y1 + $y1Dif, $fg);
       }
              
      
imageline($image, $x1, $y1 - $y1Dif, $x1, $y1 - $y1Dif + $side, $fg);           
      
imageline($image, $x1, $y1 + $y1Dif, $x1, $y1 + $y1Dif - $side, $fg);           
   }
  
$row++;
}

// output the picture
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>


likavcan at NOSPAN sturak nospan dot sk
06-Jun-2004 12:56

This function draws arrow.

function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color) {

   $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));

   $dx = $x2 + ($x1 - $x2) * $alength / $distance;
   $dy = $y2 + ($y1 - $y2) * $alength / $distance;

   $k = $awidth / $alength;

   $x2o = $x2 - $dx;
   $y2o = $dy - $y2;

   $x3 = $y2o * $k + $dx;
   $y3 = $x2o * $k + $dy;

   $x4 = $dx - $y2o * $k;
   $y4 = $dy - $x2o * $k;

   imageline($im, $x1, $y1, $dx, $dy, $color);
   imageline($im, $x3, $y3, $x4, $y4, $color);
   imageline($im, $x3, $y3, $x2, $y2, $color);
   imageline($im, $x2, $y2, $x4, $y4, $color);

}


Lionel Van Bemten
23-May-2004 12:41

here is a code to draw a "degraded" ... :
<?
header
("Content-type : image/jpeg");

$image = @ImageCreate(200, 100)
  or die (
"Erreur de cr

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt