geen transparatie in png na gebruik Class

Status
Niet open voor verdere reacties.

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:
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
 
Nu weet ik niet welke van de volgende werkt maar moet je maar bekijken
dan leer je er iets van LET OP php.ini en gd lib eens zoeken op internet hoe het zit.
@ zal er voor zorgen dat elke mogelijk error wordt genegeert
script 1
PHP:
function createthumb($name, $newname, $new_w, $new_h, $border=false, $transparency=true, $base64=false) {
    if(file_exists($newname))
        @unlink($newname);
    if(!file_exists($name))
        return false;
    $arr = split("\.",$name);
    $ext = $arr[count($arr)-1];

    if($ext=="jpeg" || $ext=="jpg"){
        $img = @imagecreatefromjpeg($name);
    } elseif($ext=="png"){
        $img = @imagecreatefrompng($name);
    } elseif($ext=="gif") {
        $img = @imagecreatefromgif($name);
    }
    if(!$img)
        return false;
    $old_x = imageSX($img);
    $old_y = imageSY($img);
    if($old_x < $new_w && $old_y < $new_h) {
        $thumb_w = $old_x;
        $thumb_h = $old_y;
    } elseif ($old_x > $old_y) {
        $thumb_w = $new_w;
        $thumb_h = floor(($old_y*($new_h/$old_x)));
    } elseif ($old_x < $old_y) {
        $thumb_w = floor($old_x*($new_w/$old_y));
        $thumb_h = $new_h;
    } elseif ($old_x == $old_y) {
        $thumb_w = $new_w;
        $thumb_h = $new_h;
    }
    $thumb_w = ($thumb_w<1) ? 1 : $thumb_w;
    $thumb_h = ($thumb_h<1) ? 1 : $thumb_h;
    $new_img = ImageCreateTrueColor($thumb_w, $thumb_h);
   
    if($transparency) {
        if($ext=="png") {
            imagealphablending($new_img, false);
            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
            imagefill($new_img, 0, 0, $colorTransparent);
            imagesavealpha($new_img, true);
        } elseif($ext=="gif") {
            $trnprt_indx = imagecolortransparent($img);
            if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }
   
    imagecopyresampled($new_img, $img, 0,0,0,0, $thumb_w, $thumb_h, $old_x, $old_y);
    if($border) {
        $black = imagecolorallocate($new_img, 0, 0, 0);
        imagerectangle($new_img,0,0, $thumb_w, $thumb_h, $black);
    }
    if($base64) {
        ob_start();
        imagepng($new_img);
        $img = ob_get_contents();
        ob_end_clean();
        $return = base64_encode($img);
    } else {
        if($ext=="jpeg" || $ext=="jpg"){
            imagejpeg($new_img, $newname);
            $return = true;
        } elseif($ext=="png"){
            imagepng($new_img, $newname);
            $return = true;
        } elseif($ext=="gif") {
            imagegif($new_img, $newname);
            $return = true;
        }
    }
    imagedestroy($new_img);
    imagedestroy($img);
    return $return;
}
//example useage
createthumb("img.gif", "tn_img.gif", 64,64,true, true, false);
script 2 Dit is deels afkomstig van sitemasters.be door herstructurering van die site is de link niet meer de zelfde.Maar ik had die nog staan in een testfolder. werking ook niet getest maar ik hoor het wel
PHP:
<?php
	 $imgSource = imagecreatefromgif('source.gif');

$imgDestination3 = imagecreatetruecolor(90, 90);
$imgDestination4 = imagecreatetruecolor(90, 90);

$black = imagecolorallocate($imgDestination3, 0, 0, 0);
$white = imagecolorallocate($imgDestination4, 255, 255, 255);

imagefill($imgDestination3, 0, 0, $black);
imagefill($imgDestination4, 0, 0, $white);

imagecopyresampled($imgDestination3, $imgSource, 0, 0, 0, 0, 90, 90, $intImage_Width, $intImage_Height);
imagecopyresampled($imgDestination4, $imgSource, 0, 0, 0, 0, 90, 90, $intImage_Width, $intImage_Height);

$intImage_Width = 90;
$intImage_Height = 90;

$arrColors = array();
for($i=0; $i<$intImage_Width; $i++)
{
    for($j=0; $j<$intImage_Height; $j++)
      {
           $pos1 = imagecolorat($imgDestination3, $i, $j);
           $color1 = imagecolorsforindex($imgDestination3, $pos1);
        
        $pos2 = imagecolorat($imgDestination4, $i, $j);
        $color2 = imagecolorsforindex($imgDestination4, $pos2);
        
        if (($color1['red'] == $color2['red']) && ($color1['green'] == $color2['green']) && ($color1['blue'] == $color2['blue']))
        {
            $arrColors[count($arrColors)] = $color1['red'].$color1['green'].$color1['blue'];
        }
     }
}
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$i = 0;
while ($i < count($arrColors)){
    if ($arrColors[$i] == $red.$green.$blue) {
        $red = rand(0,255);
        $green = rand(0,255);
        $blue = rand(0,255);
        $i = 0;
    }
    $i++;
}
$imgDestination5 = imagecreatetruecolor(90, 90);
$transparent = imagecolorallocate($imgDestination5, $red, $green, $blue);
imagefill($imgDestination5, 0, 0, $transparent);
imagecolortransparent($imgDestination5, $transparent);
$intImage_Width = imagesx($imgSource);
$intImage_Height = imagesy($imgSource);
imagecopyresampled($imgDestination5, $imgSource, 0, 0, 0, 0, 90, 90, $intImage_Width, $intImage_Height);
imagetruecolortopalette($imgDestination5, false, 256);
imagegif($imgDestination5, 'thumbnail.gif');  
?>
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan