Avatar billede morpheus Nybegynder
16. marts 2004 - 05:24 Der er 1 løsning

Funtion til at højrestille tekst

Hej,

Jeg mangler en function der kan højrestille tekst på et billede genereret med GDlib.

Her er min foreløbige kode:

<?php
if ($_GET['hr'] >= 0 && $_GET['text']) {
    $text = $_GET['text'];
    $thick = $_GET['hr'];
   
    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);
    }
   
    $im = imagecreatetruecolor(250, 20);
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0,0,0);
    imagefill($im, 0, 0, $white);
    imagettftext($im, 11, 0, 0,15, $black, "fonts/verdana.ttf", $text);
    imagelinethick($im, 0,18,250,18,$black,$thick);
    header("Content-type: image/png");
    imagepng($im);
    imagedestroy($im);
    header ("Content-type: image/png");
}
?>
Avatar billede morpheus Nybegynder
16. marts 2004 - 10:07 #1
LØSNING:

<?php
if ($_GET['hr'] >= 0 && $_GET['text']) {
    $text = $_GET['text'];
    $thick = $_GET['hr'];
   
    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);
    }
    function fixbbox($bbox) {
        $tmp_bbox["left"] = min($bbox[0],$bbox[2],$bbox[4],$bbox[6]);
        $tmp_bbox["top"] = min($bbox[1],$bbox[3],$bbox[5],$bbox[7]);
        $tmp_bbox["width"] = max($bbox[0],$bbox[2],$bbox[4],$bbox[6]) - min($bbox[0],$bbox[2],$bbox[4],$bbox[6]) + 1;
        $tmp_bbox["height"] = max($bbox[1],$bbox[3],$bbox[5],$bbox[7]) - min($bbox[1],$bbox[3],$bbox[5],$bbox[7]);
        $tmp_bbox["left"] = 0 - $tmp_bbox["left"];
        $tmp_bbox["top"] = 0 - $tmp_bbox["top"];
        return $tmp_bbox;
    }
    $imgw = 250;
    $imgh = 20;
    $im = imagecreatetruecolor($imgw, $imgh);
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0,0,0);
    imagefill($im, 0, 0, $white);
    $bbox = fixbbox(imagettfbbox(11, 0, "fonts/verdana.ttf", $text));
    imagettftext($im, 11, 0, $imgw-$bbox[width], $bbox[height], $black, "fonts/verdana.ttf", $text);
    imagelinethick($im, 0,18,250,18,$black,$thick);
    header("Content-type: image/png");
    imagepng($im);
    imagedestroy($im);
}
?>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester