phobia
Terugkerende gebruiker
- Lid geworden
- 4 sep 2006
- Berichten
- 1.777
Ik heb een class gemaakt die text toevoegt aan een png.
Nu weet ik zeker dat de orginele png image wel transparantie heeft.
Maar de output geeft de transparantie plekken zwart weer.
Nu ben ik al enige tijd aan het stoeien om het te fixen, maar ik heb
geen ideeen meer.
Dit is mijn Class:
Ik hoop dat iemand mij kan vertellen waar ik de mist in ga of hoe ik het kan fixen.
Alvast Thnx
Nu weet ik zeker dat de orginele png image wel transparantie heeft.
Maar de output geeft de transparantie plekken zwart weer.
Nu ben ik al enige tijd aan het stoeien om het te fixen, maar ik heb
geen ideeen meer.
Dit is mijn Class:
PHP:
<?php
/* -= omschrijving =-
* Deze file Neemt een string
* en plaatst die in het midden van een image.
* Hoe meer karakter in de strind deste kleiner de fontsize word
* Momenteel werkt het alleen met een png
*
* -= hoe te gebruiken =-
* echo '<img src=" http;//localhost/texttoimage/text/fonttype/newwidth/backgroundimage" />';
*
* -= variabelen legenda =-
* #text = the text to be added to the image
* #fonttype = the font to be used if the requested font is not found arial will be used.
* #newwidth = the width of the image that will be made
* #backgroundimage = the name of the image file where the the will be added to, the extention(.png) will be added
*
* @author Morphius
*/
class texttoimage_controler {
// Images variables
protected $imagePath;
protected $oldWidth;
protected $oldHeight;
protected $newWidth;
protected $newHeight;
protected $bgImage;
// Font variables
protected $fontFolder;
protected $fontSize;
// Text variables
protected $imageText;
protected $textX;
protected $textY;
// Color variabels
protected $White;
protected $Grey;
protected $Black;
// New images variables
protected $newImage;
public function __construct() {
$this->imageFolder = BASE_RESOURCE . 'images\button_models\\';
$this->fontFolder = BASE_RESOURCE . 'fonts\\';
$text = Router::getAction();
$arg = Router::getArg();
if ($this->setImageVars($text, $arg)) {
$this->buildNewImage();
} else {
die('Image could not been build');
}
}
// Main function to make the text to a image
protected function setImageVars($text, $arg) {
$this->imageText = trim(str_replace('_', ' ', $text));
$this->bgImage = trim($arg[2]) . '.png';
$this->newWidth = $arg[1];
$this->fontFolder = $this->getFolderPath(trim(strtolower($arg[0])));
$this->bgImage = $this->loadBgImage();
$this->newHeight = $this->calculateNewHeight();
$this->fontSize = $this->calculateFontSize();
$this->newImage = $this->setNewImage();
$this->setTextXY(); // Set the text X and Y coordinates
$this->setColors(); // Set the White/Grey/Black colors
return TRUE;
}
// Start building new image
protected function buildNewImage() {
$this->addTextAndShadow();
imagesavealpha($this->newImage, true);
$this->displayNewImage();
}
// Create path to requested font
protected function getFolderPath($font = 'arial') {
$fontFiles = $this->getFontInFolder($font);
$font = (isset($fontFiles[$font])) ? $fontFiles[$font] : $fontFiles['arial'];
return $this->fontFolder . $font;
}
// Get all fonts from folder
protected function getFontInFolder($fontName) {
$handle = opendir($this->fontFolder);
if ($handle) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' || $file != '..') {
$fileName = explode('.', $file);
$font[strtolower($fileName[0])] = $file;
}
}
closedir($handle);
}
return $font;
}
// Load background image
protected function loadBgImage() {
$path = $this->imageFolder . $this->bgImage;
if(is_readable($path)) {
$image = ImageCreateFrompng($path);
imagealphablending($image, true);
$this->imagePath = $image;
}
return $path;
}
// Create holder New Images
protected function setNewImage() {
$new_image = imagecreatetruecolor($this->newWidth, $this->newHeight);
imagealphablending($new_image, true);
imagecopyresampled($new_image, $this->imagePath, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->oldWidth, $this->oldHeight);
imagealphablending($new_image, true);
return $new_image;
}
// Calculate new height by ratio
protected function calculateNewHeight() {
list($width, $height, $type, $attr) = getimagesize($this->bgImage);
$ratio = $height / $width;
$this->oldWidth = $width;
$this->oldHeight = $height;
return $this->newWidth * $ratio;
}
// Calculate fontsize
protected function calculateFontSize() {
$calFont = ceil($this->newWidth / strlen($this->imageText));
return ($calFont >= $this->newHeight) ? ceil($this->newHeight * .8) : $calFont;
}
// Set the X and Y position of the text to center it
protected function setTextXY() {
$bbox = imagettfbbox($this->fontSize, 0, $this->fontFolder, $this->imageText);
$this->textX = $bbox[0] + (imagesx($this->newImage) / 2) - ($bbox[4] / 2);
$this->textY = (imagesy($this->newImage) / 2) - ($bbox[5] / 2);
}
// Create some colors
protected function setColors() {
$this->White = imagecolorallocate($this->imagePath, 255, 255, 255);
$this->Grey = imagecolorallocate($this->imagePath, 128, 128, 128);
$this->Black = imagecolorallocate($this->imagePath, 0, 0, 0);
}
// Add Text and some Shadow to the image
protected function addTextAndShadow() {
// Add some shadow to the text
imagettftext($this->newImage, $this->fontSize, 0, $this->textX + 1, $this->textY + 1, $this->Black, $this->fontFolder, $this->imageText);
//imagealphablending($new_image, true);
// Add the text
imagettftext($this->newImage, $this->fontSize, 0, $this->textX, $this->textY, $this->White, $this->fontFolder, $this->imageText);
// imagealphablending($new_image, true);
}
protected function displayNewImage() {
imagesavealpha($this->newImage, true);
// Set the content-type
header("Content-type: image/png");
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($this->newImage);
imagedestroy($this->imagePath);
imagedestroy($this->newImage);
exit();
}
}
// End Class
Ik hoop dat iemand mij kan vertellen waar ik de mist in ga of hoe ik het kan fixen.
Alvast Thnx