이미지 업로드시 일정 크기 초과시 레사이즈 하여 올리는 함수
Posted by Albert 4783Day 20Hour 59Min 43Sec ago [2012-03-16]
에디터로 이미지 업로드시 사이즈 큰 이미지 그대로 올리면 프레임이 망가질수있당
그래서 기본으로 넓이를 체크하여 넓이사이즈가 지정 사이즈보다
클때에는 이미지를 resize 시켜 업로드하는 함수이다...
휴... 반나절 시간 아깝당.... ㅜㅜ
=====================================================================
<?php
// bmp 파일은 자동 php 상 제공 하고 있지 않는사항으로 함수 추가
function imagecreatefrombmp($p_sFile)
{
// Load the image into a string
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
// Process the header
// Structure: http://www.fastgraph.com/help/bmp_header_format.html
if (substr($header,0,4)=="424d")
{
// Cut it in parts of 2 bytes
$header_parts = str_split($header,2);
// Get the width 4 bytes
$width = hexdec($header_parts[19].$header_parts[18]);
// Get the height 4 bytes
$height = hexdec($header_parts[23].$header_parts[22]);
// Unset the header params
unset($header_parts);
}
// Define starting X and Y
$x = 0;
$y = 1;
// Create newimage
$image = imagecreatetruecolor($width,$height);
// Grab the body from the image
$body = substr($hex,108);
// Calculate if padding at the end-line is needed
// Divided by two to keep overview.
// 1 byte = 2 HEX-chars
$body_size = (strlen($body)/2);
$header_size = ($width*$height);
// Use end-line padding? Only when needed
$usePadding = ($body_size>($header_size*3)+4);
// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
// Calculate the next DWORD-position in the body
for ($i=0;$i<$body_size;$i+=3)
{
// Calculate line-ending and padding
if ($x>=$width)
{
// If padding needed, ignore image-padding
// Shift i to the ending of the current 32-bit-block
if ($usePadding)
$i += $width%4;
// Reset horizontal position
$x = 0;
// Raise the height-position (bottom-up)
$y++;
// Reached the image-height? Break the for-loop
if ($y>$height)
break;
}
// Calculation of the RGB-pixel (defined as BGR in image-data)
// Define $i_pos as absolute position in the body
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);
// Calculate and draw the pixel
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
// Raise the horizontal position
$x++;
}
// Unset the body / free the memory
unset($body);
// Return image-object
return $image;
}
// 이미지 사이즈 변경 함수
function img_resize( $tmpname, $size, $save_dir, $save_name )
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = GetImageSize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
case "6": $imorig = imagecreatefrombmp($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
$x = imageSX($imorig);
$y = imageSY($imorig);
if($gis[0] <= $size)
{
$av = $x;
$ah = $y;
}
else
{
$yc = $y*1.3333333;
$d = $x>$yc?$x:$yc;
$c = $d>$size ? $size/$d : $size;
$av = $x*$c; //высота исходной картинки
$ah = $y*$c; //длина исходной картинки
}
$im = imagecreate($av, $ah);
$im = imagecreatetruecolor($av,$ah);
if (imagecopyresampled($im,$imorig , 0,0,0,0,$av,$ah,$x,$y))
if (imagejpeg($im, $save_dir.$save_name))
return true;
else
return false;
}
// 이미지 resize 함수 종료
// 이미지 기본 내용 가져오기
$upload_dir = 'upfiles/img/';
$tgname = date("YmdGis");
$file_path = $upload_dir . $_FILES['userfile']['name'];
$ffname = $_FILES['userfile']['name'];
$pinfo=pathinfo($_FILES['userfile']["name"]);
$ext = $pinfo[extension];
$tmp_name = $_FILES['userfile']['tmp_name'];
$targetname = "img_".$tgname.".".$ext;
$MAX_SIZE = 20000000;
echo $_POST['buttoninfo'];
if(!is_dir($upload_dir))
{
if(!mkdir($upload_dir))
echo "파일폴더가 존재하지않거나 생성할수없습니다. 관리자에게 문의하여 주세요.";
if(!chmod($upload_dir,0755))
echo "업로드 하려는 폴더에 쓰기권한이 없습니다.";
}
if($_FILES['userfile']['size']>$MAX_SIZE)
echo "업로드 하려는 최대치를 초과하였습니다.";
if($_FILES['userfile']['size'] == 0)
echo "파일을 선택하여 주세요.";
if (sizeof(explode('.php', $ffname))>1 ||
sizeof(explode('.phtm', $ffname))>1 ||
sizeof(explode('.htm', $ffname))>1 ||
sizeof(explode('.cgi', $ffname))>1 ||
sizeof(explode('.pl', $ffname))>1 ||
sizeof(explode('.exe', $ffname))>1 ||
sizeof(explode('.jsp', $ffname))>1 ||
sizeof(explode('.asp', $ffname))>1 ||
sizeof(explode('.js', $ffname))>1 ||
sizeof(explode('.inc', $ffname))>1 )
{
echo "업로드에 실패하였습니다. 정확한 타입의 파일을 선택하여 주세요.";
exit;
}
else
{
list($width, $height, $type, $attr) = getimagesize($tmp_name);
if($width>670 )
{
// 파일 사이즈가 670보다 클때 resize 한당 ^^
@img_resize( $tmp_name , 600 , $upload_dir , $targetname);
echo "success__".$ffname."__".$targetname;
break;
}
else
{
// 파일 넓이 사이즈가 670보다 작을때 작을때
copy($tmp_name, $upload_dir.$targetname);
echo "success__".$ffname."__".$targetname;
break;
}
}
// 끝
?>