答案 0 :(得分:0)
我修正了,我使用了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);
}