Webdesign in Siegen

Fragen zur GD-lib

Fragen zum Thema PHP können hier gestellt werden

Moderator: Basti

Fragen zur GD-lib

Beitragvon Avedo am 29.04.2008, 18:24

Hallo!
Wie kann ich es anstellen, dass die in einer PHP-Datei erstellten Bilder auch alle ausgegeben werden und nicht nur das erste? Hier ein kleines Beispiel, dass nur das erste Bild anzeigt und nicht, wie ich es möchte, beide.
Code: Alles auswählen
<?php
header('Content-type: image/png');

$test = @imagecreate(150, 100);
imagecolorallocate($test, 150, 150, 150);
$text_test = imagecolorallocate($test, 0, 51, 102);
imagestring($test, 2, 5, 5, 'Mein erstes Bild', $text_test);
imagepng($test);

$image = @imagecreate(150, 100);
imagecolorallocate($image, 50, 50, 50);
$text_image = imagecolorallocate($image, 0, 51, 102);
imagestring($image, 2, 5, 5, 'Eigentlich mein zweites Bild', $text_image);
imagepng($image);

?>

Wäre euch für Hilfe sehr dankbar.
MfG, Andy
Ich bin zu Mimis Religion konvertiert!
I'm so tired of slitting the throats of people calling me a violent psychopath.
Benutzeravatar
Avedo
Mitglied
 
Beiträge: 464
Registriert: 09.12.2007, 20:12
Wohnort: Göttingen

Re: Fragen zur GD-lib

Beitragvon .wired am 29.04.2008, 19:16

Du hast doch im Header definiert, dass die Datei als Bild behandelt werden soll, oder? Dann kannst du meines Wissens nach nicht erwarten, dass beide ausgegeben werden, das müsstest du dann schon anders lösen...

MfG .wired
Bild Bild
Benutzeravatar
.wired
Mitglied
 
Beiträge: 315
Registriert: 24.06.2007, 20:36
Wohnort: Diekholzen

Re: Fragen zur GD-lib

Beitragvon Basti am 29.04.2008, 19:49

Ja, es ist so wie .wired sagt. Du erstellst ja einen Header für ein Bild, und dann wird die ganze Ausgabe auch nur als ein Bild behandelt.

Wenn du mehrere erzeugte Bilder anzeigen möchtest, ist eine zuvorige Abspeicherung und anschließende Ausgabe in einem einfachen HTML Dokument die wohl einfachste Lösung.
Benutzeravatar
Basti
Moderator
 
Beiträge: 1774
Registriert: 15.06.2006, 17:33
Wohnort: Rheinbreitbach

Re: Fragen zur GD-lib

Beitragvon Avedo am 29.04.2008, 20:57

Hallo!
Danke erstmal für eure Hilfe. Habe gerade (zur Entspannung) mal etwas mit den GDlib-Funktionen gespielt. Hatte irgendwie keine Lust mehr mich mit den RFCs für heute rumzuschlagen. Dabei ist bisher eine kleine Klasse, ImageEdit, entstanden, die folgende Funktionen enthält:
  • newImage($width, $height)
  • background($image, $color)
  • border($image, $width, $color)
  • pixelate($image, $pixel)
  • filter($image, $color, $opacity)
  • negative($image)
  • returnImg($image)
  • imgColor($img, $hex)
Ein Aufruf der Klasse sieht so aus:
Code: Alles auswählen
<?php
   require_once("ImageEdit.php");

   $img = new ImageEdit();
   
   $image = imagecreatefromjpeg('andy.JPG');
   header('content-type: image/jpeg');
   $image = $img->negative($image);
   $image = $img->border($image, 5, '#FFFFFF');
   $image = $img->border($image, 1, '#666666');
   imagejpeg($image);
   imagedestroy($image);
?>

Ist eigentlich mehr eine Spielerei, da ich festgestellt habe, dass bei mir die Filter-Funktionen der GD noch nicht funktionieren.
Wie das ganze bisher in Aktion aussieht, kann man hier sehen.
MfG, Andy
Ich bin zu Mimis Religion konvertiert!
I'm so tired of slitting the throats of people calling me a violent psychopath.
Benutzeravatar
Avedo
Mitglied
 
Beiträge: 464
Registriert: 09.12.2007, 20:12
Wohnort: Göttingen

Re: Fragen zur GD-lib

Beitragvon .wired am 29.04.2008, 21:18

Öhm, jetzt mal nicht persönlich nehmen... aber was genau willst du uns damit sagen? Keine Frage, kein Code, nur ne kleine Geschichte zu deiner Klasse... "Und weiter?", frag ich mich da...

MfG .wired
Bild Bild
Benutzeravatar
.wired
Mitglied
 
Beiträge: 315
Registriert: 24.06.2007, 20:36
Wohnort: Diekholzen

Re: Fragen zur GD-lib

Beitragvon Avedo am 29.04.2008, 22:14

Nichts weiter. Siehe es als kleine Spielerei, die ich euch, nach meiner Frage, die sich mir zuerst stellte, einfach mal zeigen wollte. So wie die Designer Ihre Grafiken gerne zeigen, zeige ich gerne Code. Ja ich weiß, dass noch kein Code zu sehen ist, aber vielleicht hat jemand ja noch weitere Anregungen, was man noch so mit der GDlib basteln könnte. Über solche würde ich mich zumindest freuen und wenn die Klasse fertig ist, soweit man überhaupt davon sprechen, kann ich sie dann ja auch hier reinstellen. Das ganze soll übrigens auf jeden Fall noch um eine resize()-Methode erweitert werden.
MfG, Andy
Ich bin zu Mimis Religion konvertiert!
I'm so tired of slitting the throats of people calling me a violent psychopath.
Benutzeravatar
Avedo
Mitglied
 
Beiträge: 464
Registriert: 09.12.2007, 20:12
Wohnort: Göttingen

Re: Fragen zur GD-lib

Beitragvon Avedo am 29.04.2008, 23:57

Abend!
Habe noch ein bisschen an der Klasse gearbeitet und bin eigentlich nun soweit fertig. Leider ist bei der letzten Methode ein kleiner Fehler aufgetreten, der sich leider nicht finden lässt. Es handelt sich hierbei um die Methode mosaik(), die Thumbnails von einem Bild erzeugen soll und aus diesen, die mit verschiedenen Filtern gefärbt werden, soll wieder das Bild in groß konstruiert werden.
Hier mal der Quellcode.
Code: Alles auswählen
<?php

error_reporting(E_ALL);

/***
* ImageEdit class allows editing an image-file

* @package ImageEdit
* @version 0.1
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/ 
class ImageEdit
{       
   /**
   * newImage() - Creates a new image-handle
   *
   * @access public
   * @param Int $width
   * @param Int $height
   * @return Obj
   */
   public function newImage($width=100, $height=100)
   {   
      // create image and safe temporary
      $image = imagecreatetruecolor($width, $height);
      
      return $image;
   }
   
   /**
   * resize() - Rezises an image
   *
   * @access public
   * @param Obj $image
   * @param Int $size
   * @return Obj
   */
   public function resize($image, $size)
   {      
      // get width and height of the image
      $x = imagesx($image);
      $y = imagesy($image);
      
      // define width and height of the thumbnail
      if ($x>$y)
      {
         $width = $size;
         $height = round($y / $x * $size);
      }

      else
      {
         $height = $size;
         $width = round($x / $y * $size);
      }
      
      // create empty thumbnail
      $thumb = imagecreatetruecolor($width, $height);
      
      // insert resized image into the empty thumbnail
      imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $x, $y);      
      
      return $thumb;
   }
   
   /**
   * mosaik() - Creates a mosaik out of the given image
   *
   * @access public
   * @param Obj $image
   * @param Int $size
   * @return Obj
   */
   public function mosaik($image, $size)
   {         
      // create thumbnail for the mosaik
      $thumb = $this->resize($image, $size);
      
      // get width and height of thumbnail
      $thumb_width = imagesx($thumb);
      $thumb_height = imagesy($thumb);
         
      // get width and height of the image
      $width = imagesx($image);
      $height = imagesy($image);
      
      // get thumb arrangement
      $num_x = round($width / $thumb_width);
      $num_y = round($height / $thumb_height);
         
      // get width and height of the layer needed for the mosaik
      $width = $num_x * $thumb_width;
      $height = $num_y * $thumb_height;
      
      // create empty image-layer
      $mosaik = imagecreatetruecolor($width, $height);
      
      for ($x=0; $x<$width; $x+=$thumb_width)
      {
         for ($y=0; $y<$height; $y+=$thumb_height)
         {
            // get color of spezific pixel
            $rgb = imagecolorat($image, $x, $y);
            $red = ($rgb >> 16) & 255;
            $green = ($rgb >> 8) & 255;
            $blue = ($rgb & 255);
            
            // create an empty filter-layer
            $filter = imagecreatetruecolor($thumb_width, $thumb_height);
            
            // create a spezific filter-color
            $color = imagecolorallocate($filter, $red, $green, $blue);
            
            // fill filter-layer with color
            imagefill($filter, 0, 0, $color);

            // merge image and filter-layer
            $merge = imagecopymerge ($thumb, $filter, 0, 0, 0, 0, $thumb_width, $thumb_height, 50);
            
            // insert filtered thumb into mosaik image
            imagecopyresampled($mosaik, $merge, 0, 0, $x, $y, $width, $height, $thumb_width, $thumb_height);   
         }
      }   
      
      return $mosaik;
   }
   
   /**
   * background() - Fills the background of an image
   *
   * @access public
   * @param Obj $image
   * @param Str $color
   * @return Obj
   */
   public function background($image, $color)
   {
      // define color
      $color = $this->imgColor($image, $color);
      
      // fill image with color
      imagefill($image, 0, 0, $color);
      
      return $image;
   }
   
   /**
   * border() - Creates a border around the image
   *
   * @access public
   * @param Obj $image
   * @param Int $width
   * @param Hex $color
   * @return Obj
   */
   public function border($image, $width, $color)
   {
      // define color
      $color = $this->imgColor($image, $color);
      
      // get width and height of the image
      $x = imagesx($image);
      $y = imagesy($image);
      
      // create border with $width and $color
      for($i=0; $i<$width; $i++)
      {
         // create border bottom
         imageline($image, 0, ($y-1)-$i, $x, ($y-1)-$i, $color);
         
         // create border top
         imageline($image, 0, $i, $x, $i, $color);
         
         // create border right
         imageline($image, ($x-1)-$i, 0, ($x-1)-$i, $y, $color);
         
         // create border left
         imageline($image, $i, 0, $i, $y, $color);
      }
      
      return $image;
   }
   
   /**
   * pixelate() - Makes a image look like a very bad one
   *
   * @access public
   * @param Obj $image
   * @param Int $pixel
   * @return Obj
   */
   public function pixelate($image, $pixel)
   {      
      // get width and height of the image
      $width = imagesx($image);
      $height = imagesy($image);

      for ($x=0; $x<$width; $x+=$pixel)
      {
         for ($y=0; $y<$height; $y+=$pixel)
         {
            // get color of spezific pixel
            $rgb = imagecolorat($image, $x, $y);
            
            // colorate every pixel
            imagefilledrectangle($image, $x, $y, $x+$pixel-1, $y+$pixel-1, $rgb);
         }
      }
      
      return $image;
   }
   
   /**
   * filter() - Colorates an image
   *
   * @access public
   * @param Obj $image
   * @param Hex $color
   * @param Int $opacity
   * @return Obj
   */
   public function filter($image, $color, $opacity)
   {   
      // get width and height of the image
      $width = imagesx($image);
      $height = imagesy($image);   
      
      // create the filter-layer
      $filter = imagecreate($width, $height);
               
      // colorate the filter-layer
      $filter = $this->background($filter, $color);

      // merge image and filter-layer
      $merge = imagecopymerge ($image, $filter, 0, 0, 0, 0, $width, $height, $opacity);
      
      return $image;
   }
   
   /**
   * negative() - Creates a negative of an image
   *
   * @access public
   * @param Obj $image
   * @return Obj
   */
   public function negative($image)
   {
      // get width and height of the image
      $width = imagesx($image);
      $height = imagesy($image);   

      for ($x=0; $x<$width; ++$x)
      {
         for ($y=0; $y<$height; ++$y)
         {
            $rgb = imagecolorat($image, $x, $y);
            $red = 255 - (($rgb >> 16) & 255);
            $green = 255 - (($rgb >> 8) & 255);
            $blue = 255 - ($rgb & 255);
            
            $color = imagecolorallocate($image, $red, $green, $blue);
            imagesetpixel($image, $x, $y, $color);
         }
      }
      
      return $image;
   }

   /**
   * returnImg() - Saves Image and return image-file
   *
   * @access: public
   * @param Obj $image
   * @return NONE
   */
   public function returnImg($image)
   {      
      imagejpeg($image);
   }   
   
   /**
   * imgColor() - Converts hexacode to rgbcode and sets the color
   *
   * @access private
   * @param Obj $img
   * @param Hex $hex
   * @return Obj
   */
   public function imgColor($img, $hex)
   {
      $hex = str_replace('#','', $hex);
      $hex = trim($hex);
      $int = hexdec($hex);

      return imageColorAllocate ($img,
         0xFF & ($int >> 0x10),
         0xFF & ($int >> 0x8),
         0xFF & $int);
   }
}
?>

MfG, Andy
Ich bin zu Mimis Religion konvertiert!
I'm so tired of slitting the throats of people calling me a violent psychopath.
Benutzeravatar
Avedo
Mitglied
 
Beiträge: 464
Registriert: 09.12.2007, 20:12
Wohnort: Göttingen


Zurück zu PHP

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 2 Gäste