涉及图片旋转、放大、复制
目前仅实现了头部的复制
准备工作:
- 迷你世界皮肤底图(命名为miniw.png):
- 我的世界皮肤:
源代码:
⚠AI生成
<?php
// 设置上传文件的目标目录
$target_dir = "uploads/";
// 创建目标目录如果不存在
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
// 设置上传文件的名称
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// 检查是否有文件被上传
if (isset($_POST["submit"])) {
// 检查文件是否已上传且不是错误上传
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// 加载图片
$image = imagecreatefrompng($target_file);
// 加载 miniw.png 文件
$miniwFile = 'miniw.png';
$miniwImage = imagecreatefrompng($miniwFile);
// 指定头部在 miniw.png 中的位置
$headX = 490;
$headY = 600;
// 提取头部区域
$headWidth = 8;
$headHeight = 8;
$originalHeadX = 8;
$originalHeadY = 8;
$scaleFactor = 50;
// 创建一个新的图像来存储头部
$headImage = imagecreatetruecolor($headWidth, $headHeight);
$rightheadImage = imagecreatetruecolor($headWidth, $headHeight);
$topheadImage = imagecreatetruecolor($headWidth, $headHeight);
$leftheadImage = imagecreatetruecolor($headWidth, $headHeight);
$bottomheadImage = imagecreatetruecolor($headWidth, $headHeight);
$backheadImage = imagecreatetruecolor($headWidth, $headHeight); // 新增
// 复制头部区域到新图像
imagecopy($headImage, $image, 0, 0, $originalHeadX, $originalHeadY, $headWidth, $headHeight);
imagecopy($rightheadImage, $image, 0, 0, $originalHeadX-8, $originalHeadY, $headWidth, $headHeight);
imagecopy($topheadImage, $image, 0, 0, $originalHeadX, $originalHeadY-8, $headWidth, $headHeight);
imagecopy($leftheadImage, $image, 0, 0, $originalHeadX+8, $originalHeadY, $headWidth, $headHeight);
imagecopy($bottomheadImage, $image, 0, 0, $originalHeadX+8, $originalHeadY-8, $headWidth, $headHeight);
imagecopy($backheadImage, $image, 0, 0, $originalHeadX + 8 + 8, $originalHeadY, $headWidth, $headHeight); // 新增
// 创建放大后的头部图像
$scaledHeadImage = imagecreatetruecolor($headWidth * $scaleFactor, $headHeight * $scaleFactor);
$scaledRightHeadImage = imagecreatetruecolor($headWidth * $scaleFactor, $headHeight * $scaleFactor);
$scaledTopHeadImage = imagecreatetruecolor($headWidth * $scaleFactor, $headHeight * $scaleFactor);
$scaledLeftHeadImage = imagecreatetruecolor($headWidth * $scaleFactor, $headHeight * $scaleFactor);
$scaledBottomHeadImage = imagecreatetruecolor($headWidth * $scaleFactor, $headHeight * $scaleFactor);
$scaledBackHeadImage = imagecreatetruecolor($headWidth * $scaleFactor, $headHeight * $scaleFactor); // 新增
// 放大头部图像
imagecopyresampled($scaledHeadImage, $headImage, 0, 0, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor, $headWidth, $headHeight);
imagecopyresampled($scaledRightHeadImage, $rightheadImage, 0, 0, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor, $headWidth, $headHeight);
imagecopyresampled($scaledTopHeadImage, $topheadImage, 0, 0, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor, $headWidth, $headHeight);
imagecopyresampled($scaledLeftHeadImage, $leftheadImage, 0, 0, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor, $headWidth, $headHeight);
imagecopyresampled($scaledBottomHeadImage, $bottomheadImage, 0, 0, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor, $headWidth, $headHeight);
imagecopyresampled($scaledBackHeadImage, $backheadImage, 0, 0, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor, $headWidth, $headHeight); // 新增
// 将头部图像粘贴到 miniw.png 上
imagecopy($miniwImage, $scaledHeadImage, $headX, $headY, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor);
imagecopy($miniwImage, $scaledRightHeadImage, $headX - $headWidth * $scaleFactor, $headY, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor);
imagecopy($miniwImage, $scaledTopHeadImage, $headX, $headY-$headHeight * $scaleFactor, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor);
imagecopy($miniwImage, $scaledLeftHeadImage, $headX+$headWidth * $scaleFactor, $headY, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor);
imagecopy($miniwImage, $scaledBottomHeadImage, $headX, $headY+$headHeight * $scaleFactor, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor);
imagecopy($miniwImage, $scaledBackHeadImage, $headX + $headHeight * $scaleFactor * 2, $headY, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor); // 新增
// 输出最终图像
header('Content-Type: image/png');
imagepng($miniwImage);
imagedestroy($image);
imagedestroy($headImage);
imagedestroy($rightheadImage);
imagedestroy($topheadImage);
imagedestroy($leftheadImage);
imagedestroy($bottomheadImage);
imagedestroy($backheadImage); // 新增
imagedestroy($scaledHeadImage);
imagedestroy($scaledRightHeadImage);
imagedestroy($scaledTopHeadImage);
imagedestroy($scaledLeftHeadImage);
imagedestroy($scaledBottomHeadImage);
imagedestroy($scaledBackHeadImage); // 新增
imagedestroy($miniwImage);
// 删除上传的文件
unlink($target_file);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
// HTML 表单用于上传文件
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST" enctype="multipart/form-data">
Select Minecraft skin to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Skin" name="submit">
</form>
标签:迷你,世界,Study,皮肤,GD,PHP
From: https://www.cnblogs.com/meny233/p/18370458