0

I'm using Imagine together with Zend Framework 2.

I'm looking for a way to add a text in image before save it.

I tried with this :

//create required instances...
$Imagine = new \Imagine\Gd\Imagine();
$Palette = new \Imagine\Image\Palette\RGB();
//Background color of the upcomming image basend on settings passed by user...
$BackgroundColor = $Palette->color('fff', 0);
//...some other code...
$TextColor = $Palette->color('#000', 100);
//create an ImageBox for upcomming image based on given width and height
$ImageBox = new \Imagine\Image\Box(300, 300);
//get center X|Y coordinates for ImageBox
$ImageCenterPosition = new \Imagine\Image\Point\Center($ImageBox);
//create Text Object with font-family, font-size and color from settgins
$TextFont = new \Imagine\Gd\Font( BASE_PATH .  "/public/assets/plugins/font-awesome/fonts/fontawesome-webfont.ttf" , 13,  $TextColor);
//create a Box (dimensions) based on font and given string...
$TextBox = $TextFont->box( "test texte" );
//get center X|Y coordinates for the box containing the text
$TextCenterPosition = new \Imagine\Image\Point\Center( $TextBox );
//calculate center X|Y coordanates from ImageBox center coordanates and FontBox center coordanates
$CenteredTextPosition = new \Imagine\Image\Point( $ImageCenterPosition->getX() - $TextCenterPosition->getX(), $ImageCenterPosition->getY() - $TextCenterPosition->getY());
//create an image with ImageBox dimensions and BackgroundColor
$_img_ = $Imagine->create($ImageBox, $BackgroundColor);
//now draw the text...
$_img_->draw()->text("test texte 222", $TextFont, $CenteredTextPosition);
$_img_->save($newName);

I hope one of you guys can help out with a solution.

Zakaria
  • 66
  • 1
  • 9

1 Answers1

0

I Fixed it, i used a php function :

public function saveImageWithText($source_file, $sText = null, $font = null)
    {
        if (!file_exists($source_file))
            throw new Exception('De afbeeldingbestand dat u heeft aangegeven is niet gevonden.');

        $allowedExtensions = array(
            1, // [] gif
            2, // [] jpg
            3, // [] png
            6 // [] bmp
        );

        // Copy and resample the imag
        list($width, $height, $extension) = getimagesize($source_file);

        if (!in_array($extension, $allowedExtensions)) 
            throw new Exception('De afbeelding extentie wordt niet odersteund.');

        switch ($extension) {
            case 1 :
                $image = imageCreateFromGif($source_file);
                break;
            case 2 :
                $image = imageCreateFromJpeg($source_file);
                break;
            case 3 :
                $image = imageCreateFromPng($source_file);
                break;
            case 6 :
                $image = imageCreateFromBmp($source_file);
                break;
        }

        $image_p = imagecreatetruecolor($width, $height);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 

        // Prepare font size and colors
        $text_color = imagecolorallocate($image_p, 0, 0, 0);
        $bg_color   = imagecolorallocate($image_p, 255, 255, 255);
        $font_size  = 20;

        if(is_null($font))
            $font = BASE_PATH .  '/public/assets/data/fonts/clear-sans.regular.ttf';

        if (!file_exists($font))
            throw new Exception('De fontbestand dat u heeft aangegeven is niet gevonden.');

        // Set the offset x and y for the text position
        $offset_x = 5;
        $offset_y = 50;

        // Set text to be write on image
        if(is_null($sText))
            $sText = "Foto opgenomen op : " . date("d-m-Y H:i");

        // Get the size of the text area
        $dims = imagettfbbox($font_size, 0, $font, $sText);

        $text_width = $dims[4] - $dims[6] + $offset_x;
        $text_height = $dims[3] - $dims[5] + $offset_y;

        // Add text background
        imagefilledrectangle($image_p, 0, 0, $text_width + 10, $text_height, $bg_color);

        // Add text
        imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $sText);

        // Save the picture
        switch ($extension) {
            case 1 :
                imagegif($image_p, $source_file);
                break;
            case 2 :
                imagejpeg($image_p, $source_file); 
                break;
            case 3 :
                imagepng($image_p, $source_file);
                break;
            case 6 :
                imagepng($image_p, $source_file);
                break;
        }

        // Clear
        imagedestroy($image); 
        imagedestroy($image_p);
    }   
Zakaria
  • 66
  • 1
  • 9