gpt4 book ai didi

PHP自定义明信片

转载 作者:IT王子 更新时间:2023-10-28 23:49:47 28 4
gpt4 key购买 nike

我正在我的网站上创建一个新功能,允许人们向 friend 发送明信片。在这个部分,他们可以选择他们想要发送的图像(他们已经将图像上传到他们的个人资料 -> 我的图片部分)

我正在使用 php 函数来创建右侧的文本,但是我怎样才能将另一个图像添加到带有文本的图像中?

我使用 imagettftext 创建文本,imagecreatefromjpeg 打开主图像(见下文),完成后使用 imagedestroy

谢谢

我正在使用这张明信片: postcard-template.jpg

最佳答案

首先,您必须裁剪图像以适合您的明信片。根据您的图片,您必须执行以下操作:

<?php

$sourceImage = './postcard-template.jpg';
$uploadedImage = '/path/to/image/hong-kong2.jpg'; // let's get hong kong as example
$mime = '';
$font = '/path/to/font/arial.ttf';

function CroppedThumbnail($source, $width, $height, &$mime) {
$data = getimagesize($source);
$sourceWidth = $data[0];
$sourceHeight = $data[1];
$mime = $data['mime'];
$image = imagecreatefromjpeg($source);
$sourceRatio = $sourceWidth/$sourceHeight;
if (($width/$height) > $sourceRatio) {
$newHeight = $width/$sourceRatio;
$newWidth = $width;
}
else {
$newWidth = $height*$sourceRatio;
$newHeight = $height;
}
$croppedImage = imagecreatetruecolor(round($newWidth), round($newHeight));
imagecopyresampled($croppedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $croppedImage, 0, 0, (($newWidth/2)-($width/2)), (($newHeight/2)-($height/2)), $width, $height, $width, $height);
imagedestroy($croppedImage);
imagedestroy($image);
return $thumb;
}

// Create the cropped image first
$newThumb = CroppedThumbnail($uploadedImage,240,315, $mime);
switch($mime) {
case 'image/gif':
$image = imagecreatefromgif($sourceImage);
break;
case 'image/jpeg':
$image = imagecreatefromjpeg($sourceImage);
break;
case 'image/png':
$image = imagecreatefrompng($sourceImage);
break;
default:
// error or stop script
break;
}
$message = "this is some text\nsome other text\ntext text";

imagettftext($image, 21, 0, 320, 255, imagecolorallocate($image, 0, 0, 0), $font, $message);
imagecopy($image, $newThumb, 40, 40, 0, 0, 240, 315);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

例如我使用这张图片(需要裁剪):

testimage

然后它会输出:

final

关于PHP自定义明信片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8196840/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com