1
0
mckaygerhard 25e4c96f24 img display 2 : finally build and get the preview of the skin texture
* the uploading and display rendering process is done.. 100%
* preview path is hidden cos the image is rendering using base64
* we must then use small DB to display images in upstream on new images
2024-04-29 18:07:05 -04:00

146 lines
5.3 KiB
PHP

<?php
/** turn flip the image template for the rear of the skin */
function ImageCopyFliped($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h )
{
$img_temp = imagecreatetruecolor($src_w, $src_h);
imagesavealpha($img_temp,true);
$white = imagecolorallocatealpha($img_temp, 255, 255, 255, 127);
imagefill($img_temp, 0, 0, $white);
imageCopy($img_temp, $src_im, 0, 0, $src_x, $src_y, $src_w,$src_h);
imageflip($img_temp,IMG_FLIP_HORIZONTAL);
imageCopy($dst_im, $img_temp, $dst_x, $dst_y, 0, 0, $src_w,$src_h);
imagedestroy($img_temp) ;
}
/** creat the front view of the skin, using the image resource, inside we call for the rear also */
function create2d($img_src,$scale)
{
imagesavealpha($img_src,true);
$width = 16;
$height = 32;
$img_out = imagecreatetruecolor($width, $height);
$white = imagecolorallocatealpha($img_out, 255, 255, 255, 127);
imagefill($img_out, 0, 0, $white);
imagesavealpha($img_out,true);
imageCopy($img_out, $img_src, 4, 0, 8, 8, 8, 8);
imageCopy($img_out, $img_src, 0, 8, 44, 20, 4, 12);
imageCopyFliped($img_out, $img_src, 12, 8, 44, 20, 4, 12);
imageCopy($img_out, $img_src, 4, 8, 20, 20, 8, 12);
imageCopy($img_out, $img_src, 8, 20, 4, 20, 4, 12);
imageCopyFliped($img_out, $img_src, 4, 20, 4, 20, 4, 12);
//imageantialias($img_out, false); // not works cos only usefully in 32bit images without alpha
$width = 16;
$height = 32;
$newwidth = $width * $scale;
$newheight = $height * $scale;
$out = imagecreatetruecolor($newwidth, $newheight);
$white = imagecolorallocatealpha($out, 255, 255, 255, 127);
imagefill($out, 0, 0, $white);
//imagesavealpha($img_out,true);
//imagesavealpha($thumb,true);
imagecopyresized($out, $img_out, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagesavealpha($out,true);
return $out;
}
$realrootscript = basename(__FILE__);
$realcompleteuri = $_SERVER['REQUEST_URI'];
$realquerystring = $_SERVER['QUERY_STRING'];
$realrootroute = $_SERVER['PATH_INFO'];
$realrooturibase = substr($realcompleteuri ,0,strpos($realcompleteuri,$realrootscript));
$realrooturiscript = $realrooturibase . $realrootscript;
if ( ! str_contains($realrootroute, 'process') )
{
?>
<!DOCTYPE html>
<html>
<body>
<p>
<?php echo $urlquery . ' | '. $urluri . ' | '. $filepath . ' | ' . $urlroute;?>
</p>
<form action="<?php echo $realrooturiscript;?>/process" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="img" id="img">
<input type="submit" value="Enviar" name="imagesended">
</form>
</body>
</html>
<?php
}
else
{
$receivedpost = '';
$imgarrayparts = NULL;
if( is_array($_POST) OR is_array($_FILES) )
{
if( array_key_exists('imagesended',$_POST) )
$receivedpost = $_POST['imagesended'];
else
echo "error, is not a post request";
if( array_key_exists('img',$_FILES) )
$imgarrayparts = $_FILES['img'];
else
echo "error, is not a post request";
if( !array_key_exists('error',$imgarrayparts) )
echo "Error on sending files, something uknown";
}
if( !is_array($_POST) OR !is_array($_FILES) )
{
echo "Not sending by POST, end of process";
die;
}
if( trim($receivedpost) == '' OR $imgarrayparts == NULL)
{
echo "Not sending nothing, end of process";
die;
}
if( mb_strlen($imgarrayparts['name'],"UTF-8") > 225 )
{
echo "Invalid filename, must be max 255 chars file name";
die;
}
if( preg_match("`^[-0-9A-Z_\.]+$`i",$imgarrayparts['name']) != 1)
{
echo "Invalid filename, must be only alpha numeric chars file name: ".$imgarrayparts['name'];
die;
}
$imgdirupload = "work/";
$imgarrayparts = $_FILES['img'];
$imgnamefile = $imgarrayparts['name'];
$imgorigtempo = $imgarrayparts['tmp_name'];
$imgbinarydata = file_get_contents($imgorigtempo);
$imgbinarytype = pathinfo($imgorigtempo,PATHINFO_EXTENSION);
$imgbinabase64 = 'data:image/' . $imgbinarytype . ';base64,' . base64_encode($imgbinarydata);
$imgbinarysize = getimagesizefromstring($imgbinarydata);
$imgbinaryreso = imagecreatefromstring($imgbinarydata);
$uploadres = move_uploaded_file($imgorigtempo,$imgdirupload . $imgnamefile);
// img size must be array 0 64 array 1 32 array mime image/png
$thumb = create2d($imgbinaryreso,2);
echo 'img: <img src="'.$imgbinabase64.'"> !'.PHP_EOL;
echo "<br>";
echo 'size: '.print_r($imgbinarysize,true);
echo '<br>';
echo "dec: ".print_r($_POST,true)." !".PHP_EOL;
echo "<br>";
echo "dec: ".print_r($_FILES,true)." !".PHP_EOL;
mkdir("./skins");
$imggenprevie = "./skins/".$imgnamefile."_preview.png";
imagepng($thumb,$imggenprevie);
$imggenprevie64 = 'data:image/' . $imgbinarytype . ';base64,' . base64_encode(file_get_contents($imggenprevie));
echo 'img: <img src="'.$imggenprevie64.'"> !'.PHP_EOL;
}