1. Chèn chữ vào ảnh (ảnh trống tạo bằng php)
input: text hoặc array chứa text
output: ảnh png chứa các dòng text đã nhập, mỗi phần tử của array nằm trên một dòng
/**
* Chuyển Text thành Image, định dạng .png
*/
public function textToImage(...$text)
{
// $text là array không cố định số lượng phần tử
$name = uniqid(); // tạo tên ngẫu nhiên
$font_size = 8;
$opacity = 30;
$width = 0;
foreach ($text as $k => $string) {
$width = max($width, strlen($string)); // lấy chiều ngang lớn nhất từ các string
}
$width = imagefontwidth($font_size) * $width; // chiều ngang của ảnh
$height = imagefontheight($font_size) * count($text); // chiều cao của ảnh
$img = imagecreatetruecolor($width, $height); // tạo ảnh mới
$x = imagefontwidth($font_size); // vị trí chiều ngang chèn chữ vào ảnh
$y = imagefontheight($font_size); // vị trí chiều dọc chèn chữ vào ảnh
$bg = imagecolorallocate($img, 255, 255, 255); // màu nền
imagefilledrectangle($img, 0, 0, $width, $height, $bg); // đổ màu vào ảnh
$font = public_path().'/fonts/Roboto/Roboto-Regular.ttf';
$color = imagecolorallocate($img, 0, 0, 0); // màu chữ
foreach ($text as $k => $string) {
imagettftext($img, $font_size, 0, $x, $y, $color, $font, $string);
$y += $y;
// phần này tạo chữ theo font mặc định
// imagechar mỗi lần chèn 1 ký tự từ vị trí sau ký tự trước
// kết qua giống như imagettftext nhưng bằng font mặc định
// $len = strlen($string);
// $ypos = 0;
// for ($i = 0; $i < $len; $i++) {
// $xpos = $i * $x;
// $ypos = $k * $y;
// imagechar($img, $font_size, $xpos, $ypos, $string, $color);
// $string = substr($string, 1);
// }
}
imagecolortransparent($img, $bg); // chuyển màu nền thành trong suốt
$blank = imagecreatetruecolor($width, $height); // tao ảnh mới
$tbg = imagecolorallocate($blank, 255, 255, 255); // màu nền
imagefilledrectangle($blank, 0, 0, $width, $height, $tbg); // đổ màu nền
imagecolortransparent($blank, $tbg); // chuyển thành trong suốt
// Create watermark image - merger 2 ảnh vào nhau và đặt opacity
imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $opacity);
$imagepath = public_path() . '/' . $name . ".png"; // đường dẫn lư ảnh mới
imagepng($blank, $imagepath);
return $imagepath;
}
2. Chèn chữ vào ảnh có sẵn
input: text, đường dẫn file ảnh cần chèn text
output: ảnh mới sau khi đẫ thêm text
/**
* Chèn tên và thời gian ký vào ảnh xác thực ký và tạo ra ảnh mới
*/
public function writeTextOverImage($username)
{
header("Content-type: image/png");
$imgPath = public_path() . '/images/cert-logo-trans.png';
$img = imagecreatefrompng($imgPath); // tạo ảnh từ png
$color = imagecolorallocate($img , 0, 0, 0); // lấy mã màu
imagealphablending($img, false); // tắt chế độ hòa trộn
imagesavealpha($img, true); // lưu thông tin alpha(transparrency)
$string1 = $username;
$string2 = date('d/m/Y H:i:s');
$fontSize = 25;
$font = public_path().'/fonts/Roboto/Roboto-Regular.ttf';
// Chèn text vào ảnh có sẵn
imagettftext($img, $fontSize, 0, 400, 130, $color, $font, $string1);
imagettftext($img, $fontSize, 0, 400, 200, $color, $font, $string2);
$imagepath = public_path() . '/' . uniqid() . ".png";
imagepng($img,$imagepath); //tạo ảnh png mới
return $imagepath;
}
Tham khảo: https://www.codeproject.com/Articles/545046/write-text-over-image-or-picture-with-php
Không có nhận xét nào:
Đăng nhận xét