[PHP GD Study]我的世界皮肤转迷你世界
涉及图片旋转、放大、复制
目前仅实现了头部的复制
准备工作:
- 迷你世界皮肤底图(命名为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>
升级(也是半成品)
<?php
function createScaledImage($originalImage, $width, $height, $scaleFactor) {
$scaledImage = imagecreatetruecolor($width * $scaleFactor, $height * $scaleFactor);
imagecopyresampled($scaledImage, $originalImage, 0, 0, 0, 0, $width * $scaleFactor, $height * $scaleFactor, $width, $height);
return $scaledImage;
}
function processImage($target_file, $headWidth, $headHeight, $scaleFactor, $headX, $headY, $originalHeadX, $originalHeadY) {
try {
$image = imagecreatefrompng($target_file); // 假设上传的图片为PNG格式
$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);
// 创建放大后的头部图像
$headImages = [
'head' => $headImage,
'right' => $rightheadImage,
'top' => $topheadImage,
'left' => $leftheadImage,
'bottom' => $bottomheadImage,
'back' => $backheadImage
];
$scaledHeadImages = [];
foreach ($headImages as $key => $img) {
$scaledHeadImages[$key] = createScaledImage($img, $headWidth, $headHeight, $scaleFactor);
}
// 提取左手部分
$handWidth = 4;
$handHeight = 16;
$originalHandX = 0;
$originalHandY = 16;
$lefthandImage = imagecreatetruecolor($handWidth, $handHeight);
imagecopy($lefthandImage, $image, 0, 0, $originalHandX, $originalHandY, $handWidth, $handHeight);
// 旋转左手图像 90 度
$rotatedLefthandImage = imagerotate($lefthandImage, 90, 0);
// 创建放大后的左手图像
$scaledLefthandImage = createScaledImage($rotatedLefthandImage, $handHeight, $handWidth, $scaleFactor);
// 加载 miniw.png
$miniwImage = imagecreatefrompng('miniw.png');
// 粘贴头部图像到 miniw.png
foreach ($scaledHeadImages as $key => $scaledImg) {
$dx = 0; $dy = 0;
switch ($key) {
case 'right': $dx = $headX - $headWidth * $scaleFactor; break;
case 'top': $dy = $headY - $headHeight * $scaleFactor; break;
case 'left': $dx = $headX + $headWidth * $scaleFactor; break;
case 'bottom': $dy = $headY + $headHeight * $scaleFactor; break;
case 'back': $dx = $headX + $headHeight * $scaleFactor * 2; break;
default: $dx = $headX; $dy = $headY; break;
}
imagecopy($miniwImage, $scaledImg, $dx, $dy, 0, 0, $headWidth * $scaleFactor, $headHeight * $scaleFactor);
}
// 粘贴左手图像到 miniw.png
imagecopy($miniwImage, $scaledLefthandImage, 0, 600, 0, 0, $handHeight * $scaleFactor, $handWidth * $scaleFactor);
// 输出最终图像
header('Content-Type: image/png');
imagepng($miniwImage);
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
} finally {
// 销毁所有使用的图像资源
foreach (array_merge(array_values($headImages), array_values($scaledHeadImages), [$lefthandImage, $rotatedLefthandImage, $scaledLefthandImage, $miniwImage, $image]) as $img) {
imagedestroy($img);
}
// 删除上传的文件
if (isset($target_file)) {
unlink($target_file);
}
}
}
// 检查是否有文件上传
if (isset($_FILES['fileToUpload']) && $_FILES['fileToUpload']['error'] === UPLOAD_ERR_OK) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// 检查文件是否已存在
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
} else {
// 允许上传的文件类型
$allowed_types = ['image/png', 'image/jpeg'];
if (in_array($_FILES['fileToUpload']['type'], $allowed_types)) {
// 限制文件大小
if ($_FILES['fileToUpload']['size'] < 1000000) { // 1MB
// 移动上传的文件
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// 调用处理函数
processImage($target_file, 64, 64, 2, 100, 100, 100, 100);
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo "Sorry, your file is too large.";
}
} else {
echo "Sorry, only PNG and JPEG files are allowed.";
}
}
}
// HTML 表单用于上传文件
echo '<form action="' . htmlspecialchars($_SERVER["PHP_SELF"]) . '" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>';
?>
本文来自博客园,作者:meny,转载请注明原文链接:https://www.cnblogs.com/meny233/p/18370458