<?php
/*
* All copyright reserved (c) 2006-2008 by Gohappy
* Designer:Gohappy
*/
// ???????????????????Ç÷???????§³??????????????? 
// ?????????gif,jpg,jpe,jpeg,png 
// ???????????jpg 
// ???????: 
// $srcFile ???? 
// $dstFile ?????? 
// $dstW ???????? 
// $dstH ???????? 
//????Gohappy
function makethumb($srcFile,$dstFile,$dstW,$dstH,$img_quality) 
{ 
if ($img_quality	=='')
{
		$img_quality=75;
}
$data = GetImageSize($srcFile); 
switch ($data[2])
{ 
case 1: 
$im = @ImageCreateFromGIF($srcFile); 
break; 
case 2: 
$im = @imagecreatefromjpeg($srcFile); 
break; 
case 3: 
$im = @ImageCreateFromPNG($srcFile); 
break; 
} 
$srcW=ImageSX($im); 
$srcH=ImageSY($im); 
$dstX=0; 
$dstY=0; 
if ($srcW*$dstH>$srcH*$dstW) { $fdstH=round($srcH*$dstW/$srcW); $dstY=floor(($dstH- 
$fdstH)/2); $fdstW=$dstW; } 
else { $fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW)/2); $fdstH=$dstH; } 
//$ni=ImageCreate($dstW,$dstH); 

$ni=ImageCreateTrueColor($dstW,$dstH); 

$dstX=($dstX<0)?0:$dstX; 
$dstY=($dstX<0)?0:$dstY; 
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX; 
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY; 
$black = ImageColorAllocate($ni, 255,255,255);//??????????????????????????????? 
imagefilledrectangle($ni,0,0,$dstW,$dstH,$black); 
imagecopyresampled($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH); 
//ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH); 
Imagejpeg($ni,$dstFile,$img_quality);//????????????????????????? 
//?????????????????????header()???????mine?????? 
imagedestroy($im); 
imagedestroy($ni); 
} 

function makethumb_png($srcFile,$dstFile,$dstW,$dstH,$img_quality) 
{ 
if ($img_quality	=='')
{
		$img_quality=100;
}
$data = GetImageSize($srcFile); 
switch ($data[2])
{ 
case 1: 
$im = @ImageCreateFromGIF($srcFile); 
break; 
case 2: 
$im = @imagecreatefromjpeg($srcFile); 
break; 
case 3: 
$im = @ImageCreateFromPNG($srcFile); 
break; 
} 
$srcW=ImageSX($im); 
$srcH=ImageSY($im); 
$dstX=0; 
$dstY=0; 
if ($srcW*$dstH>$srcH*$dstW) { $fdstH=round($srcH*$dstW/$srcW); $dstY=floor(($dstH- 
$fdstH)/2); $fdstW=$dstW; } 
else { $fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW)/2); $fdstH=$dstH; } 
//$ni=ImageCreate($dstW,$dstH); 

$ni=ImageCreateTrueColor($dstW,$dstH); 

$dstX=($dstX<0)?0:$dstX; 
$dstY=($dstX<0)?0:$dstY; 
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX; 
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY; 
$black = ImageColorAllocate($ni, 255,255,255);//??????????????????????????????? 
imagefilledrectangle($ni,0,0,$dstW,$dstH,$black); 
imagecopyresampled($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH); 
Imagepng($ni,$dstFile);//????????????????????????? 
//?????????????????????header()???????mine?????? 
imagedestroy($im); 
imagedestroy($ni); 
} 



// ??????? ??????
// ?????????gif,jpg,jpe,jpeg,png 
// ???????????jpg 
// $srcFile ???? 
// $new_File ?????? 
// $srcH ???????? 
// $srcH ???????? 
//????Gohappy
function make_scale($srcFile,$new_File,$img_quality,$logo_path) 
{ 
$data = GetImageSize($srcFile); //???????????
switch ($data[2])
{ 
case 1: 
$im = @ImageCreateFromGIF($srcFile); 
break; 
case 2: 
$im = @imagecreatefromjpeg($srcFile); 
break; 
case 3: 
$im = @ImageCreateFromPNG($srcFile); 
break; 
} 
$srcH=ImageSY($im); //????????
$srcW=ImageSX($im); //????????

//????
	$simage1 =imagecreatefromPNG($logo_path);
	imagecopy($im,$simage1,$srcW-197,$srcH-22,0,0,192,17);
	Imagejpeg($im,$new_File,$img_quality);//????????????????????????? 
	//?????????????????????header()???????mine?????? 
	imagedestroy($im); 

} 


function png2jpg($source_img,$target_img,$img_quality)
{
	$temp_img = @imagecreatefrompng($source_img);
	
	$new_width  = imagesx($temp_img);
  $new_height = imagesy($temp_img);
  //????????
  $new_img = @imagecreatetruecolor($new_width,$new_height);
  @imagejpeg($temp_img,$target_img,$img_quality);
	imagedestroy($temp_img);   
}

   /**********************
    ????PHP??GD?????????????
    ??????????jpg,gif,png

    $source_img:  ????????¡¤??
    $target_img:	??????????
    $new_width:   ???????
    $new_height:  ???????
    $if_cut:      ?????
        1???????: ???????????§³??????????
        0???????: ????????????????????
    $y_height_num  ???2???????
    $y_width_num   ???????????
    $img_quality   ??????
    **********************/
function HrResize($source_img,$target_img,$new_width,$new_height,$if_cut,$y_height_num,$y_width_num,$img_quality)
{
        //??????
       	//$img_type = strtolower(substr(strrchr($source_img,"."),1));
        //???????????¡¤??
        //$tar_url = $target_dir."/".$target_name;

        //????????
        //if($img_type=="jpg") $temp_img = @imagecreatefromjpeg($source_img);
        //if($img_type=="gif") $temp_img = @imagecreatefromgif($source_img);
        //if($img_type=="png") {$temp_img = @imagecreatefrompng($source_img); @imagesavealpha($source_img, true); }

				$data = GetImageSize($source_img); //???????????
				switch ($data[2])
				{ 
				case 1: 
					$temp_img = @ImageCreateFromGIF($source_img); $img_type="gif";
					break; 
					case 2: 
					$temp_img = @imagecreatefromjpeg($source_img); $img_type="jpg";
					break; 
					case 3: 
					$temp_img = @ImageCreateFromPNG($source_img); $img_type="png";
					 @imagesavealpha($source_img, true);
					break; 
				} 



        //?????????
        $old_width  = imagesx($temp_img);
        $old_height = imagesy($temp_img);

        //??????????????
        $new_ratio = $new_width/$new_height;
        $old_ratio = $old_width/$old_height;
		
        //?????????????
        //????????? ????????§³??????????
        if($if_cut=="1")
        {
            $new_width  = $new_width;
            $new_height = $new_height;
            //??????
            $y_height	= ($old_height/10)*$y_height_num;
            $y_width	= ($old_width/10)*$y_width_num;
            
            //???????
            if($old_ratio>=$new_ratio)
            {
               $old_width  = $old_height*$new_ratio;
                $old_height = $old_height;
            }
            //???????
            else
            {
                $old_width  = $old_width;
                $old_height = $old_width/$new_ratio;  
            }
        }
        //???????????? ????????????????
        else
        {
            $old_width  = $old_width;
            $old_height = $old_height;
            //???????
            if($old_ratio>=$new_ratio)
            {
                $new_width  = $new_width;
                $new_height = $new_width/$old_ratio;
            }
            //???????
            else
            {
                $new_width  = $new_height*$old_ratio;
                $new_height = $new_height;
            }
        }
        
        //????????
       $new_img = @imagecreatetruecolor($new_width,$new_height);
       if($img_type=="png") 
       {
       	@imagesavealpha($new_img, true);
				@imagealphablending($new_img, false);
				@imagefilledrectangle($new_img, 0, 0, 1000, 1000, 0x7fffffff);
				@imagealphablending($new_img, true);
			}
			
	
       @imagecopyresampled($new_img,$temp_img,0,0,$y_width,$y_height,$new_width,$new_height,$old_width,$old_height);
       if($img_type=="jpg") @imagejpeg($new_img,$target_img,$img_quality);
       if($img_type=="png") @imagepng($new_img,$target_img); 
}
    
    
function Get_picsize($srcFile)
{
	$data = GetImageSize($srcFile); 
	switch ($data[2])
	{ 
		case 1: 
		$im = @ImageCreateFromGIF($srcFile); 
		break; 
		case 2: 
		$im = @imagecreatefromjpeg($srcFile); 
		break; 
		case 3: 
		$im = @ImageCreateFromPNG($srcFile); 
		break; 
	}
	$srcW=ImageSX($im); 
	$srcH=ImageSY($im);
	return $srcW.'??'.$srcH;
}




/**
* ??jpeg????????????????????????png??
* @param string $jpg_img ??????????
* @param string $png_img ?????????
* @param string $bg_color ???????????? ???? #ffffff
* @param int $alpha ??????? ????? 0 ?? 127??0 ?????????????127 ???????????
**/
function jpg2transpng($inimg,$png_img,$bg_color,$alpha)
{
	$size = GetImageSize($inimg); 
	switch ($size[2])
	{ 
		case 1: 
		$im_in = @ImageCreateFromGIF($inimg); 
		break; 
		case 2: 
		$im_in = @imagecreatefromjpeg($inimg); 
		break; 
		case 3: 
		$im_in = @ImageCreateFromPNG($inimg); 
		break;
	} 

  if(!$im_in)
  {
    return false;
  }
  
   //???????????
  $im_out =imagecreatetruecolor($size[0], $size[1]);
  imagealphablending($im_out, true);
  imagesavealpha($im_out, true);
  $trans_colour = imagecolorallocatealpha($im_out, 0, 0, 0, 127);
  imagefill($im_out, 0, 0, $trans_colour);

  //????????????
  for ($j=0;$j<=$size[1]-1;$j++)
  {
      for ($i=0;$i<=$size[0]-1;$i++)
      {
        $rgb = imagecolorat($im_in,$i,$j);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        if($r > $g){$r=$g;}
        if($r > $b){$r=$b;} $g=$b=$r;
               
        if($r < 230)$alpha=$r*(127/255);
        if($r >= 230)
        {
        	$alpha=127;
      	}
        $color=imagecolorallocatealpha($im_out,$r,$g,$b,$alpha);
        imagesetpixel($im_out,$dst_x+$i,$dst_y+$j,$color);
      }
  }

  //??????
  imagepng($im_out,$png_img);
  imagedestroy($im_in);
  imagedestroy($im_out);
  return true;
}

//-------------------------------------------------------
function png2transpng($inimg,$png_img,$bg_color,$alpha)
{
	$size = GetImageSize($inimg);
	$im_in = @ImageCreateFromPNG($inimg); @imagesavealpha($inimg, true); 
  //???????????
  $im_out =@imagecreatetruecolor($size[0], $size[1]);
 	@imagesavealpha($im_out, true);
	@imagealphablending($im_out, false);
	@imagefilledrectangle($im_out, 0, 0, 1000, 1000, 0x7fffffff);
	@imagealphablending($im_out, true);  
  $trans_colour = imagecolorallocatealpha($im_out, 0, 0, 0, 127);
  imagefill($im_out, 0, 0, $trans_colour);	
  //????????????
  for ($j=0;$j<=$size[1]-1;$j++)
  {
      for ($i=0;$i<=$size[0]-1;$i++)
      {
        $rgb = imagecolorat($im_in,$i,$j);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;  
				$r=$g=$b=($r+$g+$b)/3;
        if($r >= 230)
        {
        	$alpha=127;
      	}
      	else
      	{
        	$alpha=10;
        	$g=$b=$r=0;
      	}
        $color=@imagecolorallocatealpha($im_out,$r,$g,$b,$alpha);
        //echo $color.'<br>';
        imagesetpixel($im_out,$i,$j,$color);
      }
  }
  //??????
  @imagepng($im_out,$png_img);
  imagedestroy($im_in);
  imagedestroy($im_out); 
  return true;
}

?>