Помогите [PHP] Превью HD скинов

  • Автор темы Автор темы Gate
  • Дата начала Дата начала
Статус
Закрыто для дальнейших ответов.

Gate

Старожил
Пользователь
Баллы
103
Имя в Minecraft
Gate
Всем привет, делаю для сайта что-то вроде галереи скинов, чтобы можно было одним кликом установить понравившийся скин. Для "собирания" скина и его нормального отображения использую данный php код:
function imageflip(&$result, &$img, $rx = 0, $ry = 0, $x = 0, $y = 0, $size_x = null, $size_y = null) {
if ($size_x < 1) $size_x = imagesx($img);
if ($size_y < 1) $size_y = imagesy($img);

imagecopyresampled($result, $img, $rx, $ry, ($x + $size_x-1), $y, $size_x, $size_y, 0-$size_x, $size_y);
}

header ("Content-type: image/png");

$way_skin = 'http://site.ru/'.$_GET['file'].'';
$skin = imagecreatefrompng($way_skin);

$preview = imagecreatetruecolor(16, 32);

$transparent = imagecolorallocatealpha($preview, 255, 255, 255, 127);
imagefill($preview, 0, 0, $transparent);

//Вид скина спереди
imagecopy($preview, $skin, 4, 0, 8, 8, 8, 8); //Башка
imagecopy($preview, $skin, 0, 8, 44, 20, 4, 12); //Одна рука
imageflip($preview, $skin, 12, 8, 44, 20, 4, 12); //Другая рука
imagecopy($preview, $skin, 4, 8, 20, 20, 8, 12); //Тело
imagecopy($preview, $skin, 4, 20, 4, 20, 4, 12); //Одна нога
imageflip($preview, $skin, 8, 20, 4, 20, 4, 12); //Другая нога
imagecopy($preview, $skin, 4, 0, 40, 8, 8, 8); //Шляпа

$fullsize = imagecreatetruecolor(100, 200);

imagesavealpha($fullsize, true);
$transparent = imagecolorallocatealpha($fullsize, 255, 255, 255, 127);
imagefill($fullsize, 0, 0, $transparent);

imagecopyresized($fullsize, $preview, 0, 0, 0, 0, imagesx($fullsize), imagesy($fullsize), imagesx($preview), imagesy($preview));


imagepng($fullsize);

imagedestroy($fullsize);
imagedestroy($preview);
imagedestroy($skin);

Но вот незадача - обычные скины отображаются нормально, а HD некорректно. Прошу помощи более верным кодом, если таковой существует:rolleyes:
 
Код:
<?php
class Skin2d {
    private $image = NULL;
 
    function __destructor () {
        if ($this->image != NULL) {
            imagedestroy($this->image);
        }
    }
 
    function AssignSkinFromFile ($file) {
        if ($this->image != NULL) {
            imagedestroy($this->image);
        }
        if(($this->image = imagecreatefrompng($file)) == False) {
 
            throw new Exception("Could not open PNG file.");
        }
 
    }
 
    function AssignSkinFromString ($data) {
        if ($this->image != NULL) {
            imagedestroy($this->image);
        }
        if(($this->image = imagecreatefromstring($data)) == False) {
 
            throw new Exception("Could not load image data from string.");
        }
 
    }
 
    function Width () {
        if($this->image != NULL) {
            return imagesx($this->image);
        } else {
            throw new Exception("No skin loaded.");
        }
    }
 
    function Height () {
        if($this->image != NULL) {
            return imagesy($this->image);
        } else {
            throw new Exception("No skin loaded.");
        }
    }
 
    function Valid () {
        return ($this->Width() != 1024 || $this->Height() != 512) ? False : True;
    }
 
    function CombinedImage ($scale = 16) {
   
 
        $newWidth = 16 * $scale;
        $newHeight = 32 * $scale;
 
        $newImage = imagecreatetruecolor(256, 512);
       
$transparent4 = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
imagefill($newImage, 0, 0, $transparent4);
 
 
       
        imagecopy($newImage, $this->image, 64, 0, 128, 128, 128, 128);
        $this->imagecopyalpha($newImage, $this->image, 64, 0, 640, 128, 128, 128, imagecolorat($this->image, 63, 0));
        imagecopy($newImage, $this->image, 64, 128, 320, 320, 128, 192);
        imagecopy($newImage, $this->image, 128, 320, 64, 320, 64, 192);
        imagecopy($newImage, $this->image, 64, 320, 64, 320, 64, 192);
        imagecopy($newImage, $this->image, 192, 128, 704, 320, 64, 192);
        imagecopy($newImage, $this->image, 0, 128, 704, 320, 64, 192);
 
        if($scale != 16) {
            $resize = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresized($resize, $newImage, 0, 0, 0, 0, $newWidth, $newHeight, 256, 512);
            imagedestroy($newImage);
            return $resize;
        }
 
        return $newImage;
    }
 
    function imagecopyalpha($dst, $src, $dst_x, $dst_y, $src_x, $src_y, $w, $h, $bg) {
        for($i = 0; $i < $w; $i++) {
            for($j = 0; $j < $h; $j++) {
 
                $rgb = imagecolorat($src, $src_x + $i, $src_y + $j);
 
                if(($rgb & 0xFFFFFF) == ($bg & 0xFFFFFF)) {
                    $alpha = 127;
                } else {
                    $colors = imagecolorsforindex($src, $rgb);
                    $alpha = $colors["alpha"];
                }
                imagecopymerge($dst, $src, $dst_x + $i, $dst_y + $j, $src_x + $i, $src_y + $j, 1, 1, 100 - (($alpha / 127) * 100));
            }
        }
 
    }
 
}
 
$path = $_GET[skinpath];
$fileopen=fopen("skins.log", "a");
fputs($fileopen, "$_GET[skinpath]\n\r");
fclose($fileopen);
$filename = "$path.png";
$scalee = 6;
 
 
if (!file_exists($filename)) {
$path2 = "charHD.png";
$test = new Skin2d($Scrolling);
$test->AssignSkinFromFile($path2);
} else {
$path2 = "$path.png";
$test = new Skin2d($Scrolling);
$test->AssignSkinFromFile($path2);
 
}
 
 
 
 
 
header("Content-Type: image/png");
 
 
$img = $test->CombinedImage($scalee);
 
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparent);
 
$transparent2 = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 12*$scalee, 0, $transparent2);
 
$transparent3 = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 21*$scalee, $transparent3);
 
imagesavealpha($img, true);
 
imagepng($img);
?>
 
  • Нравится
Реакции: Gate
@Xssnick, спасибо, работает. Но придется скорее всего сделать кэширование, так как они очень долго загружаются, когда их много :)
 
Ура, вроде разобрался, вытащив код создания скинов из WebMCR 1.8 :p
 
Статус
Закрыто для дальнейших ответов.
Назад
Верх