PHP und ImageRotate()

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • PHP und ImageRotate()

    Hallo,

    habe PHP 4.4.3 im Einsatz und mir von php.net/manual/de die Funktion für imageRotate() eingebunden, weil die gd-Bibliothek nicht gebundled mit PHP kam. Nun habe ich das Problem, dass zwar das Bild gedreht wird, aber alles bleibt schwarz?! Es handelt sich hierbei um einen Counter, bei dem die Zahlen als einzelne GIFs abgelegt sind und dann zusammengesetzt werden, dann soll das Bild gedreht werden. Auf meinem alten Server hat der Code super funktioniert, ich habe jetzt nur die Funktion eingefügt...
    Beispiel:
    www.its-chemnitz.de/a/test.php?id=1 funktioniert ja super aber
    www.its-chemnitz.de/a/test.php?id=23 bringt den oben beschriebenen Effekt
    Ich hoffe der Quellcode ist einiger Maßen lesbar, ist jetzt schon paar Jahre alt das Skript. Wer findet einen Fehler?

    PHP-Code:
    <?php

    // $src_img - a GD image resource
    // $angle - degrees to rotate clockwise, in degrees
    // returns a GD image resource
    // USAGE:
    // $im = imagecreatefrompng('test.png');
    // $im = imagerotate($im, 15);
    // header('Content-type: image/png');
    // imagepng($im);
    function imageRotate($src_img$angle$bicubic=false) {

    // convert degrees to radians
    $angle $angle 180;
    $angle deg2rad($angle);

    $src_x imagesx($src_img);
    $src_y imagesy($src_img);

    $center_x floor($src_x/2);
    $center_y floor($src_y/2);

    $cosangle cos($angle);
    $sinangle sin($angle);

    $corners=array(array(0,0), array($src_x,0), array($src_x,$src_y), array(0,$src_y));

    foreach(
    $corners as $key=>$value) {
    $value[0]-=$center_x//Translate coords to center for rotation
    $value[1]-=$center_y;
    $temp=array();
    $temp[0]=$value[0]*$cosangle+$value[1]*$sinangle;
    $temp[1]=$value[1]*$cosangle-$value[0]*$sinangle;
    $corners[$key]=$temp;
    }

    $min_x=1000000000000000;
    $max_x=-1000000000000000;
    $min_y=1000000000000000;
    $max_y=-1000000000000000;

    foreach(
    $corners as $key => $value) {
    if(
    $value[0]<$min_x)
    $min_x=$value[0];
    if(
    $value[0]>$max_x)
    $max_x=$value[0];

    if(
    $value[1]<$min_y)
    $min_y=$value[1];
    if(
    $value[1]>$max_y)
    $max_y=$value[1];
    }

    $rotate_width=round($max_x-$min_x);
    $rotate_height=round($max_y-$min_y);

    $rotate=imagecreatetruecolor($rotate_width,$rotate_height);
    imagealphablending($rotatefalse);
    imagesavealpha($rotatetrue);

    //Reset center to center of our image
    $newcenter_x = ($rotate_width)/2;
    $newcenter_y = ($rotate_height)/2;

    for (
    $y 0$y < ($rotate_height); $y++) {
    for (
    $x 0$x < ($rotate_width); $x++) {
    // rotate...
    $old_x round((($newcenter_x-$x) * $cosangle + ($newcenter_y-$y) * $sinangle))
    $center_x;
    $old_y round((($newcenter_y-$y) * $cosangle - ($newcenter_x-$x) * $sinangle))
    $center_y;

    if ( 
    $old_x >= && $old_x $src_x && $old_y >= && $old_y $src_y ) {
        
    $color imagecolorat($src_img$old_x$old_y);
    } else {
    // this line sets the background colour
        
    $color imagecolorallocatealpha($src_img255255255127);
    }
    #echo $old_x." ".$old_y." ".$color."<br>";
    imagesetpixel($rotate$x$y$color);
    }
    }

    return(
    $rotate);
    }

    function 
    errorpic($error_msg) {
        
    $img=ImageCreate(strlen($error_msg)*713);
        
    $red=ImageColorAllocate($img25500);
        
    $bg=ImageColorAllocate($img255255255);
        
    ImageFilledRectangle($img00strlen($error_msg)*713$bg);
        
    ImageString($img300$error_msg$red);
        
    ImagePng($img);
        
    ImageDestroy($img);
        die();
    }

    function 
    errortxt($error_msg) {
        echo 
    $error_msg;
        die();
    }

    require 
    "counter.inc";
    $id=trim($_GET["id"]);
    $txt=trim($_GET["txt"]);
    # Ausgabe als Text oder Grafik?
    if ($txt!="")
        
    $txt=true;
    else
        
    $txt=false;
    if (!
    $id)
        if (
    $txt)
            
    errortxt("ID fehlt!");
        else
        
    errorpic("ID fehlt!");
    if (!
    checkid($id))
        if (
    $txt)
        
    errortxt("Ungültige ID!");
        else
        
    errorpic("Ungültige ID!");
    $prefs=getid($id);
    $count=$prefs["cnt_wert"];
    $ip=$_SERVER["REMOTE_ADDR"];
    if (
    $prefs["cnt_art"]==2) {
    # jeder Besucher wird gezählt
        
    $count+=1
        
    setcount($id$count);
    } else {
    # echte Besucher werden gezählt
        
    if (checklog($id$ip)) {
        
    updatelog($id$ip);
        } else {
        
    $count+=1;
            
    setcount($id$count);
        
    setlog($id$ip);
        }
    }
    $len=strlen(strval($count));
    if (
    $len>$prefs["cnt_len"])
        
    $width=$len;
    else
        
    $width=$prefs["cnt_len"];
    if (
    $txt) {
        if (
    $width>$len)
        for (
    $i=0;$i<($width-$len);$i++)
            echo 
    "0";
        echo 
    $count;
    } else {
        
    $digitdir="./digits/typ_".$prefs["cnt_typ"];
        if (!
    file_exists("$digitdir/0.gif"))
        
    errorpic("Keine Bilder im Verzeichnis!");
        
    Header("Content-type: image/png");
        
    clearstatcache();
        
    $d0=ImageCreateFromGif("$digitdir/0.gif");
        
    $dx=ImageSX($d0);
        
    $dy=ImageSY($d0);
        
    $img=ImageCreate($width*$dx$dy);
        
    ImageDestroy($d0);
        
    $xoff=$width*$dx;
        while(
    $xoff 0) {
        
    $digit=$count%10;
        
    $count=$count/10;
        
    $temp=ImageCreateFromGif("$digitdir/$digit.gif");
        
    $xoff=$xoff-$dx;
        
    ImageCopyResized($img$temp$xoff000$dx$dy$dx$dy);
        
    ImageDestroy($temp);
        }
        
    $transcol=split(",",$prefs["cnt_trans"]);
        if (
    count($transcol)>2) {
        
    $col_trans=ImageColorResolve($img,intval($transcol[0]),intval($transcol[1]),intval($transcol[2]));
        
    $col_trans=ImageColorTransparent($img,$col_trans); 
        }
        if (
    $prefs["cnt_rot"])
        
    $img=imageRotate($img$prefs["cnt_rot"]);
        
    ImagePng($img);
        
    ImageDestroy($img);
    }
    ?>

  • #2
    Sind die zu drehenden Bilder TrueColor ?
    aus dem Manual
    It's worth noting this function will only rotate true colour images - If you have a 256 colour pallet based image it will not rotate with this. Use the following code to convert the 256 colour image to truecolour first:
    Gruss

    tobi
    Gutes Tutorial | PHP Manual | MySql Manual | PHP FAQ | Apache | Suchfunktion für eigene Seiten

    [color=red]"An error does not become truth by reason of multiplied propagation, nor does truth become error because nobody sees it."[/color]
    Mohandas Karamchand Gandhi (Mahatma Gandhi) (Source)

    Kommentar


    • #3
      Danke für den Hinweis, hab folg. Ausschnitt gleich zu Beginn der Funktion imageRotate() eingefügt

      PHP-Code:
      if (!imageistruecolor($src_img)) {
          
      $w imagesx($src_img);
          
      $h imagesy($src_img);
          
      $t_im imagecreatetruecolor($w,$h);
          
      imagecopy($t_im,$src_img,0,0,0,0,$w,$h);
          
      $src_img $t_im;

      ...aber keine Wirkung...

      Kommentar


      • #4
        was passiert, wenn du die function "imageRotate(...)" mal in "drehDichDuLuder(...)" oder etwas anderes unverfängliches umbenennst? alternativ kannst du mit einem die() in der funktion erstmal testen, ob php wirklich da rein springst.
        Kissolino.com

        Kommentar


        • #5
          das Luder dreht sich schon
          macht es einen unterschied, wenn ich das erzeugte counter-bild erst als datei speichere und wieder mit imagecreatefrompng (oder gif) wieder einlese oder kann ich wie im code oben einfach die variable übergeben?

          anmerkung:
          es scheint wirklich irgendwo in der farbübersetzung zwischen palette und truecolor zu hängen. wenn ich händisch zu color einen hohen wert dazu addieren (bsp. 10000000) dann wird das bild komplett grün (bzw. welche farbe auch immer das ist). gibt es einen befehl, der aus einer palettenfarbe die zugehörige truecolor-farbe findet - sollte das nicht der befehl imagecolorat machen???

          weitere anmerkung:
          wenn ich mir die korrdinaten und dazugehörige farbe ausgeben lasse, erhalte ich folg.

          -1 14 2
          -1 13 3
          -1 12 4
          -1 11 5
          -1 10 6
          -1 9 7
          -1 8 8
          -1 7 9
          -1 6 10
          -1 5 11
          -1 4 12
          -1 3 13
          -1 2 14
          -1 1 15
          0 14 16
          0 13 0
          0 12 0
          0 11 0
          0 10 0
          0 9 0
          0 8 0
          0 7 0
          0 6 0
          0 5 0
          0 4 0
          0 3 0
          1 2 0
          1 1 0
          1 14 17
          1 13 0
          1 12 0
          1 11 0
          1 10 0
          1 9 0
          1 8 0
          1 7 0
          1 6 0
          1 5 0
          1 4 0
          1 3 0
          2 2 0
          2 1 0
          2 14 18
          2 13 0
          2 12 0
          2 11 0
          2 10 0
          2 9 0
          2 8 0
          2 7 0
          2 6 0
          2 5 0
          2 4 0
          2 3 0
          3 2 0
          3 1 0
          3 14 19
          3 13 0
          3 12 0
          3 11 0
          3 10 0
          3 9 0
          3 8 0
          3 7 0
          3 6 0
          3 5 0
          3 4 0
          3 3 1
          4 2 0
          4 1 0
          4 14 20
          4 13 0
          4 12 0
          4 11 0
          4 10 0
          4 9 0
          4 8 0
          4 7 0
          4 6 0
          4 5 0
          4 4 0
          4 3 1
          5 2 1
          5 1 0
          5 14 21
          5 13 0
          5 12 0
          5 11 1
          5 10 1
          5 9 1
          5 8 1
          5 7 1
          5 6 1
          5 5 1
          5 4 1
          5 3 1
          6 2 0
          6 1 0
          6 14 22
          6 13 0
          6 12 0
          6 11 0
          6 10 0
          6 9 0
          6 8 0
          6 7 0
          6 6 0
          6 5 0
          6 4 0
          6 3 0
          7 2 0
          7 1 0
          7 14 23
          7 13 0
          7 12 0
          7 11 0
          7 10 0
          7 9 0
          7 8 0
          7 7 0
          7 6 0
          7 5 0
          7 4 0
          7 3 0
          8 2 0
          8 1 0
          8 14 24
          8 13 0
          8 12 0
          8 11 0
          8 10 0
          8 9 0
          8 8 0
          8 7 0
          8 6 0
          8 5 0
          9 4 0
          9 3 0
          9 2 0
          9 1 0
          9 14 25
          9 13 0
          9 12 0
          9 11 0
          9 10 0
          9 9 0
          9 8 0
          9 7 0
          9 6 0
          9 5 0
          10 4 0
          10 3 0
          10 2 0
          10 1 0
          10 14 26
          10 13 0
          10 12 0
          10 11 0
          10 10 0
          10 9 0
          10 8 0
          10 7 0
          10 6 0
          10 5 0
          11 4 0
          11 3 0
          11 2 0
          11 1 0
          11 14 27
          11 13 0
          11 12 0
          11 11 0
          11 10 0
          11 9 0
          11 8 0
          11 7 0
          11 6 0
          11 5 0
          12 4 0
          12 3 0
          12 2 0
          12 1 0
          12 14 28
          12 13 0
          12 12 0
          12 11 0
          12 10 0
          12 9 0
          12 8 0
          12 7 0
          12 6 0
          13 5 0
          13 4 0
          13 3 0
          13 2 0
          13 1 0
          13 14 29
          13 13 0
          13 12 0
          13 11 0
          13 10 0
          13 9 0
          13 8 0
          13 7 0
          13 6 0
          14 5 0
          14 4 0
          14 3 0
          14 2 1
          14 1 0
          14 14 30
          14 13 0
          14 12 0
          14 11 0
          14 10 0
          14 9 0
          14 8 0
          14 7 0
          15 6 0
          15 5 0
          15 4 0
          15 3 0
          15 2 1
          15 1 0
          15 14 31
          15 13 0
          15 12 0
          15 11 1
          15 10 1
          15 9 1
          15 8 0
          15 7 0
          16 6 0
          16 5 0
          16 4 0
          16 3 0
          16 2 1
          16 1 0
          16 14 32
          16 13 0
          16 12 0
          16 11 0
          16 10 0
          16 9 0
          16 8 1
          17 7 0
          17 6 1
          17 5 1
          17 4 0
          17 3 0
          17 2 1
          17 1 0
          17 14 33
          17 13 0
          17 12 0
          17 11 0
          17 10 0
          17 9 0
          17 8 0
          18 7 0
          18 6 0
          18 5 0
          18 4 1
          18 3 1
          18 2 1
          18 1 0
          18 14 34
          18 13 0
          18 12 0
          18 11 0
          18 10 0
          18 9 0
          19 8 0
          19 7 0
          19 6 0
          19 5 0
          19 4 0
          19 3 0
          19 2 0
          19 1 0
          19 14 35
          19 13 0
          19 12 0
          19 11 0
          19 10 0
          19 9 0
          20 8 0
          20 7 0
          20 6 0
          20 5 0
          20 4 0
          20 3 0
          20 2 0
          20 1 0
          20 14 36
          20 13 0
          20 12 0
          20 11 0
          20 10 0
          21 9 0
          21 8 0
          21 7 0
          21 6 0
          21 5 0
          21 4 0
          21 3 0
          21 2 0
          21 1 0
          21 14 37
          21 13 0
          21 12 0
          21 11 0
          21 10 0
          22 9 0
          22 8 0
          22 7 0
          22 6 0
          22 5 0
          22 4 0
          22 3 0
          22 2 0
          22 1 0
          22 14 38
          22 13 0
          22 12 0
          22 11 0
          22 10 0
          23 9 0
          23 8 0
          23 7 0
          23 6 0
          23 5 0
          23 4 0
          23 3 0
          23 2 0
          23 1 0
          23 14 39
          23 13 0
          23 12 0
          23 11 0
          23 10 0
          24 9 0
          24 8 0
          24 7 0
          24 6 0
          24 5 0
          24 4 0
          24 3 0
          24 2 0
          24 1 0
          24 14 40
          24 13 0
          24 12 0
          25 11 1
          25 10 1
          25 9 1
          25 8 0
          25 7 0
          25 6 0
          25 5 0
          25 4 1
          25 3 1
          25 2 0
          25 1 0
          25 14 41
          25 13 0
          25 12 0
          26 11 1
          26 10 0
          26 9 0
          26 8 1
          26 7 0
          26 6 0
          26 5 0
          26 4 0
          26 3 0
          26 2 1
          26 1 0
          26 14 42
          26 13 0
          26 12 0
          27 11 1
          27 10 0
          27 9 0
          27 8 0
          27 7 1
          27 6 0
          27 5 0
          27 4 0
          27 3 0
          27 2 1
          27 1 0
          27 14 43
          27 13 0
          27 12 0
          28 11 1
          28 10 0
          28 9 0
          28 8 0
          28 7 0
          28 6 1
          28 5 0
          28 4 0
          28 3 0
          28 2 1
          28 1 0
          28 14 44
          28 13 0
          28 12 0
          29 11 1
          29 10 0
          29 9 0
          29 8 0
          29 7 0
          29 6 0
          29 5 1
          29 4 1
          29 3 1
          29 2 0
          29 1 0
          29 14 45
          29 13 0
          29 12 0
          30 11 0
          30 10 0
          30 9 0
          30 8 0
          30 7 0
          30 6 0
          30 5 0
          30 4 0
          30 3 0
          30 2 0
          30 1 0
          30 14 46
          30 13 0
          30 12 0
          31 11 0
          31 10 0
          31 9 0
          31 8 0
          31 7 0
          31 6 0
          31 5 0
          31 4 0
          31 3 0
          31 2 0
          31 1 0
          31 14 47
          31 13 0
          31 12 0
          32 11 0
          32 10 0
          32 9 0
          32 8 0
          32 7 0
          32 6 0
          32 5 0
          32 4 0
          32 3 0
          32 2 0
          32 1 0
          wenn ich jetzt die o.g. funktion zur umwandlung von 256-farben image auf truecolor einsetze, dann werden aus den wenig vorhandenen 1 alles 0...
          wer jetzt mal www.its-chemnitz.de/a/test.php?id=23 anschaut, der wird sehen, dass das bild wirklich gedreht wird (ich habe mal eingefügt
          PHP-Code:
          if ($color==0$color=150000000
          was nun?
          Zuletzt geändert von itschemnitz; 17.07.2006, 12:36.

          Kommentar


          • #6
            Original geschrieben von itschemnitz
            was nun?
            schmeiss weg

            wenn du "nur" in 90° schritten drehen willst, kann man das einfacher angehen:
            PHP-Code:
            // farben aus dem src-bild an dest-bild übergeben
            for($i=0$i<imagecolorstotal($this->imgsrc); $i++)
            {
                  
            $c imageColorsForIndex($this->imgsrc$i);
                  
            imagecolorallocate($this->imgdest$c['red'], $c['green'], $c['blue']);
            }

            // pixel einzeln wie in einem koordinatensystem übertragen
            for($x=0$x<$this->thisimage['x']; $x++)
            {
                for(
            $y=0$y<$this->thisimage['y']; $y++)
                {
                     
            $c imagecolorsforindex($this->imgdest,imagecolorat($this->imgsrc$x$y));
                     
            $i imagecolorexact($this->imgdest,  $c['red'], $c['green'], $c['blue']);
                     switch(
            $_degree)
                     {
                        case 
            90:
                            
            imagesetpixel($this->imgdest,  $y$this->thisimage['x']-$x-1,  $i);
                            break;
                        case 
            270:
                            
            imagesetpixel($this->imgdest$this->thisimage['y']-$y-1$x,  $i);
                            break;
                        case 
            180:
                            
            imagesetpixel($this->imgdest$this->thisimage['x']-$x-1$this->thisimage['y']-$y-1$i);
                            break;
                     }
                }

            das snippet stammt aus einer älteren klasse von mir und tut klaglos seinen dienst. musst du nur anpassen, vorher noch das src-bild auslesen und das dest-bild anlegen, hinterher ausgeben. dann sollte das passen.

            src = source
            dest = destination
            Kissolino.com

            Kommentar


            • #7
              Daaanke, reicht mir vollkommen aus, ich hoffe es stört nicht sehr wenn ich in dem Thread gleich noch ne Frage stelle (das Forum ist ja maßlos überfüllt mit Threads):

              Ich habe noch die Option mit der Transparenz drin, die funktioniert nicht. Folgender Maßen sieht die jetzt aus:

              PHP-Code:
                  if (count($transcol)==3) {
                  if (
              ImageColorExact($img,intval($transcol[0]),intval($transcol[1]),intval($transcol[2]))!=-1) {
                          
              $col_trans=ImageColorAllocate($img,intval($transcol[0]),intval($transcol[1]),intval($transcol[2])); 
                          
              $trans=ImageColorTransparent($img,$col_trans); 
                      }
                  } 
              $col_trans enthält als Farbe 2, aber der Befehl danach ImageColorTransparent bewirkt nichts, auf farbigem Untergrund bleibt der Counter (am Bsp. der 23) trotzdem mit weißem Hintergrund stehen...

              Nico

              Kommentar


              • #8
                wie und wann nutzt du die transparenz?

                ich hab damit keine probleme:
                - bild-hintergrund eine wilde farbe zuweisen
                - text draufschreiben
                - in ein neues bild drehen, dabei farben übertragen
                - hintergrundfarbe transparent machen
                - ausgabe

                ... fettich
                Kissolino.com

                Kommentar


                • #9
                  Die Transparenz wird zu allerletzt gesetzt, nach dem Drehen des Bildes
                  Es wird im laufen Script einfach oben stehender Code abgearbeitet. Kann evtl. das Problem darin liegen, dass das Bild als PNG ausgegeben wird? (werde das dann mal verifizieren)

                  Kommentar

                  Lädt...
                  X