如何将多个图像和文本合并为一个图像?
How to merge multiple images and text into single image?
我在 div 中有多个 PNG 图像,这些图像是 PNG 并且根据自定义选项 he/she 已选择,作为单个图像呈现给用户。此外,添加文本也作为其他功能启用,这允许 div 将文本添加到这些图像上方。
现在我想生成一个包含多个图像和文本的图像,同时保持 font-family 和文本大小。
例如。界面上出现的图像,图像和文字相结合。 (它设法以 css 定位出现)
这是由下面的两张图片和文字组成的
这是我尝试拍摄的图像:create-image.php 文件
<?php
createimageinstantly();
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=1000;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(1000, 1000);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
imagecopy($outputImage,$first,0,0,0,0, $x, $y);
imagecopy($outputImage,$second,0,0,0,0, $x, $y);
imagecopy($outputImage,$third,0,200,-200,0, $x, $y);
imagepng($outputImage, $targetPath .round(microtime(true) * 1000).'.png');
imagedestroy($outputImage);
}
?>
但这给了我全黑的图像
此外,我需要在最终生成的图像上混合文字
已编辑 :
jpg图片改为png
imagecopymege
改为 imagecopy
最新结果:
<?php
createimageinstantly();
//$targetFolder = '/gw/media/uploads/processed/';
//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
//$img3 = $targetPath.'img3.png';
//print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=600;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(600, 600);
// set background to white
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);
imagepng($outputImage, $targetPath .round(microtime(true)).'.png');
imagedestroy($outputImage);
}
?>
和输出图像
首先,jpeg图像没有alpha通道 - 这意味着图像中的每个像素都有一些颜色,没有有关其透明度的信息。可能很难为此创建解决方法,但您可以 google 它。如果可以 - 我建议你使用 PNG 格式,它包含透明度信息。
其次 - 尝试阅读 PHP docs for imagecopymerge. This comment 中的评论部分提供了一些您可以使用的有用信息:
Sina Salek: I've just checked PHP's issue tracker and a core developer says that
this function was never meant to support alpha channel! and they
refused to commit the provided patch!
然后评论者提供了一个我认为可行的示例。
我是这样实现的,
使用了这 3 张图片,img1.png,img2.png,img3.png
和create-image.php
文件
<?php
createimageinstantly();
//$targetFolder = '/gw/media/uploads/processed/';
//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
//$img3 = $targetPath.'img3.png';
//print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=600;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(600, 600);
// set background to white
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);
// Add the text
//imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
//$white = imagecolorallocate($im, 255, 255, 255);
$text = 'School Name Here';
$font = 'OldeEnglish.ttf';
imagettftext($outputImage, 32, 0, 150, 150, $white, $font, $text);
$filename =$targetPath .round(microtime(true)).'.png';
imagepng($outputImage, $filename);
imagedestroy($outputImage);
}
?>
结果图像是
参考:imagecopyresized & imagettftext
感谢您通过 comments/answers 提出的建议。
我还详细地写了博客 http://sumankc.com/2016/01/30/merge-multiple-images-and-text-to-create-single-image-php-gd-library/
美好的一天!!
我在 div 中有多个 PNG 图像,这些图像是 PNG 并且根据自定义选项 he/she 已选择,作为单个图像呈现给用户。此外,添加文本也作为其他功能启用,这允许 div 将文本添加到这些图像上方。
现在我想生成一个包含多个图像和文本的图像,同时保持 font-family 和文本大小。
例如。界面上出现的图像,图像和文字相结合。 (它设法以 css 定位出现)
这是我尝试拍摄的图像:create-image.php 文件
<?php
createimageinstantly();
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=1000;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(1000, 1000);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
imagecopy($outputImage,$first,0,0,0,0, $x, $y);
imagecopy($outputImage,$second,0,0,0,0, $x, $y);
imagecopy($outputImage,$third,0,200,-200,0, $x, $y);
imagepng($outputImage, $targetPath .round(microtime(true) * 1000).'.png');
imagedestroy($outputImage);
}
?>
但这给了我全黑的图像
此外,我需要在最终生成的图像上混合文字
已编辑 :
jpg图片改为png
imagecopymege
改为imagecopy
最新结果:
<?php
createimageinstantly();
//$targetFolder = '/gw/media/uploads/processed/';
//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
//$img3 = $targetPath.'img3.png';
//print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=600;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(600, 600);
// set background to white
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);
imagepng($outputImage, $targetPath .round(microtime(true)).'.png');
imagedestroy($outputImage);
}
?>
和输出图像
首先,jpeg图像没有alpha通道 - 这意味着图像中的每个像素都有一些颜色,没有有关其透明度的信息。可能很难为此创建解决方法,但您可以 google 它。如果可以 - 我建议你使用 PNG 格式,它包含透明度信息。
其次 - 尝试阅读 PHP docs for imagecopymerge. This comment 中的评论部分提供了一些您可以使用的有用信息:
Sina Salek: I've just checked PHP's issue tracker and a core developer says that this function was never meant to support alpha channel! and they refused to commit the provided patch!
然后评论者提供了一个我认为可行的示例。
我是这样实现的,
使用了这 3 张图片,img1.png,img2.png,img3.png
和create-image.php
文件
<?php
createimageinstantly();
//$targetFolder = '/gw/media/uploads/processed/';
//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
//$img3 = $targetPath.'img3.png';
//print_r(getimagesize('http://www.vapor-rage.com/wp-content/uploads/2014/05/sample.jpg'));
function createimageinstantly($img1='',$img2='',$img3=''){
$x=$y=600;
header('Content-Type: image/png');
$targetFolder = '/gw/media/uploads/processed/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$img1 = $targetPath.'img1.png';
$img2 = $targetPath.'img2.png';
$img3 = $targetPath.'img3.png';
$outputImage = imagecreatetruecolor(600, 600);
// set background to white
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($img1);
$second = imagecreatefrompng($img2);
$third = imagecreatefrompng($img3);
//imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
imagecopyresized($outputImage,$first,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$second,0,0,0,0, $x, $y,$x,$y);
imagecopyresized($outputImage,$third,200,200,0,0, 100, 100, 204, 148);
// Add the text
//imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
//$white = imagecolorallocate($im, 255, 255, 255);
$text = 'School Name Here';
$font = 'OldeEnglish.ttf';
imagettftext($outputImage, 32, 0, 150, 150, $white, $font, $text);
$filename =$targetPath .round(microtime(true)).'.png';
imagepng($outputImage, $filename);
imagedestroy($outputImage);
}
?>
结果图像是
参考:imagecopyresized & imagettftext
感谢您通过 comments/answers 提出的建议。 我还详细地写了博客 http://sumankc.com/2016/01/30/merge-multiple-images-and-text-to-create-single-image-php-gd-library/ 美好的一天!!