я использую Представить вместе с Zend Framework 2.
Я ищу способ добавить текст в изображение, прежде чем сохранить его.
Я пытался с этим:
//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);
Я надеюсь, что один из вас, ребята, может помочь с решением.
Я исправил это, я использовал функцию PHP:
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);
}
Других решений пока нет …