php.de

Zurück   php.de > Webentwicklung > PHP-Fortgeschrittene

PHP-Fortgeschrittene Arbeiten mit PHP ohne Einschränkungen

Antwort
 
LinkBack Themen-Optionen Thema bewerten
Alt 05.11.2005, 16:49  
Benutzer
 
Registriert seit: 23.10.2005
Beiträge: 54
greg
Standard Zuf�lligen String auf Bild schreiben - Klasse geht nicht

Hallo,

Ich experimentier gerade ein wenig mit den Grafik Funktionen von PHP und habe da aus einem Buch ne Klasse.

Allerdings geht die nicht so wie ich das gerne h�tte und ich finde den Fehler nicht. Ich hoffe mir kann hier geholfen werden.

Es wird immer nur das Bild angezeigt, aber ohne den Zufallsstring. Kann mir das nicht erkl�ren.

PHP-Code:
<?php

    
/**
    * Random Image Text
    * Generates a text which is shown in picture format
    * Difficult to read for ORC programms but easy for
    * human beings. Used for a secure user registration
    *
    * @package    Auth
    * @access    public
    */
    
class RandomImageText
    
{
        
/**
        * The background image
        *
        * @access private
        * @var resurce
        */
        
var $image;
        
        
/**
        * Height of the image in px
        *
        * @access private
        * @var int
        */
        
var $iHeight;
        
        
/**
        * Witdh of the image in px
        *
        * @access private
        * @var int
        */
        
var $iWidth;
        
        
/**
        * Height of the font in px
        *
        * @access private
        * @var int
        */
        
var $fHeight;
        
        
/**
        * Witdh of the font in px
        *
        * @access private
        * @var int
        */
        
var $fWidth;
        
        
/**
        * The x-positon in px
        *
        * @access private
        * @var int
        */
        
var $xPos;
        
        
/**
        * An array with font names
        *
        * @access private
        * @var array
        */
        
var $fonts;
        
        
/**
        * Constructor
        * 
        * @param string        path to the background image
        * @param int        height of the font
        * @param int        width of the font
        * @access public
        */
        
function RandomImageText($jpeg$fHeight 10$fWidth 10)
        {
            
$this->image     imagecreatefromjpeg($jpeg);
            
$this->iWidth     imagesx($this->image);
            
$this->iHeight     imagesy($this->image);
            
            
$this->xPos     0;
            
$this->fWidth     $fWidth;
            
$this->fHeight    $fHeight;
            
$this->fonts    = array(2345);
        }
        
        
/**
        * Paste a text into the image
        *
        * @param string        text
        * @param int        hex-code for red
        * @param int         hex-code for green
        * @param int        hex-code for blue
        * @return boolean
        * @access public
        */
        
function addText($text$r 0$g 0$b 0)
        {
            
$lengh $this->fWidth strlen($text);
            if (
$lengh >= ($this->iWidth $this->fWidth 2)) {
                return 
false;
            }
            
            
$this->xPos floor(($this->iWidth $lengh) / 2);
            
$fColor imagecolorallocate($this->image$r$g$b);
            
            
srand((float)microtime() * 1000000);
            
$fonts = array(2345);
            
$yStart floor($this->iHeight 2) - $this->fHeight;
            
$yEnd $yStart $this->fHeight;
            
$yPos range($yStart$yEnd);
            
            for (
$strPos 0$strPos $lengh$strPos++) {
                
shuffle($fonts);
                
shuffle($yPos);
                
imagestring($this->image,
                            
$fonts[0],
                            
$this->xPos,
                            
$yPos[0],
                            
substr($textstrPos1),
                            
$fColor);
                
$this->xPos += $this->fWidht;
            }
            return 
true;
        }
        
        
/**
        * Deletes all fonts
        *
        * @return void
        * @access public
        */
        
function clearFonts()
        {
            return 
$this->fonts = array();
        }
        
        
/**
        * Adds a new font
        *
        * @param string        path to the font-file
        * @return void
        * @access public
        */
        
function addFont($font)
        {
            
$this->fonts[] = imageloadfont($font);
        }
        
        
/**
        * Returns the height of the background image
        * in px
        *
        * @return int
        * @access public
        */
        
function getHeight()
        {
            return 
$this->iHeight;
        }
        
        
/**
        * Returns the width of the background image
        * in px
        *
        * @return int
        * @access public
        */
        
function getWidth()
        {
            return 
$this->iWidth;
        }
        
        
/**
        * Returns the resource id of the image
        *
        * @return resource
        * @access public
        */
        
function getImage()
        {
            return 
$this->image;
        }
        
        
/**
        * Generates a random string, which will be used in the 
        * image
        *
        * @return string
        * @access public
        */
        
function createRandString()
        {
            
srand((double)microtime() * 1000000);
            
$letters range('A''Z');
            
$numbers range,  );
            
$chars array_merge($letters$numbers);
            
            
$randString '';
            for (
$i 0$i 8$i++) {
                
shuffle($chars);
                
$randString .= $chars[0];
            }
            return 
$randString;
        }
    }
    
    
$imageText = new RandomImageText('picture.jpg');
    
    
    
$imageText->addText($imageText->createRandString());
    
    
    
header('Content-type: image/jpeg');
    
imagejpeg($imageText->getImage());
    
    

?>
greg ist offline   Mit Zitat antworten
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 05.11.2005, 17:31  
Gast
 
Beiträge: n/a
Standard

Probier mal aus ob es funktioniert wenn du
$imageText->addText("test");
benutzt und poste das Ergebnis dann hier,
damit wir nicht am falschen Ort den Fehler suchen.
  Mit Zitat antworten
Alt 05.11.2005, 17:52  
Gast
 
Beiträge: n/a
Standard

Auch abtippen will gelernt sein...

PHP-Code:
<?php
function addText(...) {
...
  
imagestring($this->image,
                    
$fonts[0],
                    
$this->xPos,
                    
$yPos[0],
                    
substr($textstrPos1), //wo haben wir denn die konstante strPos definiert?
                    
$fColor);
  
$this->xPos += $this->fWidht//wie schreibt man das noch gleich?
...
}
?>
  Mit Zitat antworten
Alt 05.11.2005, 22:27  
Benutzer
 
Registriert seit: 23.10.2005
Beiträge: 54
greg
Standard

Okay ich probier das mal aus, wird wohl an den Tippfehlern liegen, kann schonmal vorkommen

Edit: Alles klar, funktioniert jetzt. Dann kann ich jetzt auch anfangen die Klasse noch ein wenig auszubauen.


Danke
greg ist offline   Mit Zitat antworten
Alt 06.11.2005, 11:19  
Benutzer
 
Registriert seit: 23.10.2005
Beiträge: 54
greg
Standard

So ich habe jetzt mal versucht, eigene Fonts zu laden.
Das sollte ja mit der Funktion addFont($font) zu schaffen sein. (Siehe Klasse)

Allerdings bekomme ich immer eine Fehlermeldung. Nämlich folgende:
Die Grafik "http://localhost/main/Packages/Auth/random_image.php" kann nicht angezeigt werden, weil sie Fehler enthält.

Was habe ich falsch gemacht?
greg ist offline   Mit Zitat antworten
Antwort


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
Variable aus Klasse herausbekommen GSJLink PHP Tipps 2008 7 16.02.2008 22:25
Transparentes Bild über Bild legen *update gelöst* Broadcast PHP-Fortgeschrittene 11 04.02.2008 15:27
Event abfangen Ambience JavaScript, Ajax und mehr 4 30.08.2007 14:36
String-Parser Klasse - was muss rein? Matze PHP Tipps 2007 2 08.04.2007 22:14
mehr als eine Klasse einbinden Alpha Centauri PHP-Fortgeschrittene 4 13.04.2006 20:56
Instanz einer Klasse in einer anderen Klasse verwenden Buhmann PHP-Fortgeschrittene 7 28.10.2005 23:12
[Erledigt] Bild aus Datei in Bild einfügen PHP Tipps 2005-2 1 07.08.2005 23:36
[PHP«Image] Bild in ein anderes Bild einsetzen PHP Tipps 2005-2 4 07.08.2005 19:39
Bild oder String auf x/yposition auf einem Bild plazieren... PHP Tipps 2005 8 17.04.2005 14:06
Klasse macht Bild kaputt Buhmann PHP-Fortgeschrittene 3 25.02.2005 08:35
String zerlegen und jeweils als Bild ausgeben PHP Tipps 2005 6 08.02.2005 15:19
Bild aus Server anzeigen lassen flual2000 PHP Tipps 2004 4 26.10.2004 16:33
Problem bei einer Klasse mit "hat" beziehung PHP Tipps 2004 4 06.10.2004 21:29
Bild größe hat prob mit umbenennung. JEGO PHP Tipps 2004 1 08.07.2004 16:30

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
php string zu bild, php string als bild

Alle Zeitangaben in WEZ +1. Es ist jetzt 21:55 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum

Creative Commons License
Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.