<html>

<head>
  <title>CONVERT</title>
</head>

<body>

<?php
set_time_limit(0);
$maxh=600;
$maxw=600;
function createthumb($name,$filename,$new_w,$new_h)
{
	$system=explode(".",substr($name,-5));
	$pos=Array();
$pos = explode(".", substr($name,-5));
$typ = @$pos[1];
$typ= ".".strtolower($typ);
if (function_exists('mime_content_type')) {
$mtyp=mime_content_type($name);
} else {
if($typ==".jpg") {$mtyp="image/jpg";}
if($typ==".jpeg") {$mtyp="image/jpeg";}
if($typ==".pjpeg") {$mtyp="image/pjpeg";}
if($typ==".png") {$mtyp="image/png";}
if($typ==".gif") {$mtyp="image/gif";}
}
	if (($mtyp=="image/jpeg")||($mtyp=="image/jpg")||($mtyp=="image/pjpeg")){
    $src_img=imagecreatefromjpeg($name);
    }
    if ($mtyp=="image/png"){$src_img=imagecreatefrompng($name);}
    if ($mtyp=="image/gif"){$src_img=imagecreatefromgif($name);}

    //if (!src_img) {return;}

    $old_x=@imageSX($src_img);
	$old_y=@imageSY($src_img);
	if (($old_x<=$new_w)&&($old_y<=$new_h)) { $new_w=max($old_x,$old_y); $new_h=$new_w; }
	if ($old_x > $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
		$hsm=$new_w/2-$thumb_h/2;
		$wsm=0;
	}
	if ($old_x < $old_y)
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
		$wsm=$new_h/2-$thumb_w/2;
		$hsm=0;
	}
	if ($old_x == $old_y)
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
		$hsm=0;
		$wsm=0;
	}
	$dst_img=ImageCreateTrueColor(max($thumb_w,$thumb_h),max($thumb_w,$thumb_h));
    $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefill($dst_img, 0, 0, $transparent);
imageAlphaBlending($dst_img,1);
imagesavealpha($dst_img,1);
	@imagecopyresampled($dst_img,$src_img,$wsm,$hsm,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	if (preg_match("/png/",$system[1]))
	{
    echo "\n";
    //echo $filename." - OK<br>";
		imagepng($dst_img,$filename);
	}
    if (preg_match("/gif/",$system[1]))
	{
    echo "\n";
    //echo $filename." - OK<br>";
		imagegif($dst_img,$filename);
        }
    if (preg_match("/jpg/",$system[1]))
	{
    echo "\n";
    //echo $filename." - OK<br>";
		imagejpeg($dst_img,$filename,85);
	}
    if (preg_match("/jpeg/",$system[1]))
	{
    echo "\n";
    //echo $filename." - OK<br>";
		imagejpeg($dst_img,$filename,85);
	}
	imagedestroy($dst_img);
	@imagedestroy($src_img);
}

$handle=opendir("./i/");
$s=0;
while (($file = readdir($handle))!==FALSE) {
$pos=Array();
$pos = explode(".", substr($file,-5));
$typ = @$pos[1];
$typ= strtolower($typ);
if(($typ=="jpeg")||($typ=="jpg")||($typ=="png")||($typ=="gif")){
$source="./i/".$file;
$dest="./i/".$file.".tmp";
createthumb($source,$dest,$maxw,$maxh);
@unlink($source);
rename($dest,$source);
echo "\n"; flush();
}
}
closedir($handle);
echo "<h1>OK</h1>";
?>

</body>

</html>