Skip to content

Instantly share code, notes, and snippets.

@kureikei
Last active December 11, 2016 07:36
Show Gist options
  • Save kureikei/523a9032f355ea5189586637809d65dc to your computer and use it in GitHub Desktop.
Save kureikei/523a9032f355ea5189586637809d65dc to your computer and use it in GitHub Desktop.
<?php
$result = "";
if (isset($_FILES['file']['error']) && is_int($_FILES['file']['error'])) {
$file = $_FILES['file']['tmp_name'];
$dir = sys_get_temp_dir();
$id = uniqid();
exec("cp -R /home/pronamajp/blendfiles/ $dir/$id"); // Blender データファイル一式を一時フォルダーへコピー
createTshirtTex($file, "$dir/$id/Tsyatu.png"); // アップロードされた画像をTシャツテクスチャ画像へ合成
exec("/usr/local/blender/blender -b $dir/$id/pronama-chan.blend -o //out# -F PNG -x 1 -f 1"); // レンダリング実行
exec("mv $dir/$id/out1.png /home/pronamajp/DocumentRoot/blendertest/out/$id.png"); // 出力画像を移動
exec("rm -rf $dir/$id"); // 一時フォルダーを削除
$result = "http://pronama.jp/blendertest/out/$id.png"; // 出力画像の URL
}
// Tシャツテクスチャ画像への合成
function createTshirtTex($filename, $out_filename)
{
// Tシャツのテクスチャ画像
$tex_filename = "/home/pronamajp/DocumentRoot/blendertest/Tsyatu.png";
// Tシャツのデザイン位置・サイズ
$offset_x = 164;
$offset_y = 516;
$width = 220;
$height = 320;
// アップロード画像をリサイズ
$resized_im = resizeImage($filename, $width, $height);
$w = imagesx($resized_im);
$h = imagesy($resized_im);
// テクスチャ画像にアップロード画像を描画して指定先に保存
$tex_im = imagecreatefrompng($tex_filename);
imagecopy($tex_im, $resized_im, $offset_x + floor(($width - $w) / 2), $offset_y + floor(($height - $h) / 2), 0, 0, $w, $h);
imagepng($tex_im, $out_filename);
imagedestroy($tex_im);
imagedestroy($resized_im);
}
// 指定サイズに画像をリサイズ
function resizeImage($filename, $max_width, $max_height)
{
list($orig_width, $orig_height) = getimagesize($filename);
$width = $orig_width;
$height = $orig_height;
if ($height > $max_height) {
// taller
$width = ($max_height / $height) * $width;
$height = $max_height;
}
if ($width > $max_width) {
// wider
$height = ($max_width / $width) * $height;
$width = $max_width;
}
$image_p = imagecreatetruecolor($width, $height);
imagealphablending($image_p, false);
imagesavealpha($image_p, true);
$image = imagecreatefromstring(file_get_contents($filename));
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
imagedestroy($image);
return $image_p;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment