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.
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(2, 3, 4, 5);
}
/**
* 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(2, 3, 4, 5);
$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($text, strPos, 1),
$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( 0 , 9 );
$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());
?>
Kommentar