==== db 처리
db 처리는
이미지 저장 할 필드는 꼭 blob 형태로 해야한다.
==== img.html
<table>
<form name="form" action="./img_db.php" method="post" enctype="multipart/form-data">
<tr>
<td><input type="file" name="imagefile"> </td>
</tr>
<tr>
<td><input type="submit" value="확인"></td>
</tr>
</form>
</table>
==== img_db.php
<?php
/*디비 접속하기*/
function scaleImageFileToBlob($file) {
/* 설명
$thumb_width = '110'; //썸네일 폭
$thumb_height = '90'; //썸네일 높이
$thumb_quality = '90'; //썸네일 퀄리티_100 이하
$rate = $thumb_width / $size[0];
$height = (int)($size[1] * $rate);
if ($height < $thumb_height)
$dst = imagecreatetruecolor($thumb_width, $height);
else
$dst = imagecreatetruecolor($thumb_width, $thumb_height);
*/
$thumb_quality = '90'; //썸네일 퀄리티_100 이하
$source_pic = $file;
$max_width = 200;
$max_height = 200;
list($width, $height, $image_type) = getimagesize($file);
switch ($image_type)
{
case 1: $src = imagecreatefromgif($file); break;
case 2: $src = imagecreatefromjpeg($file); break;
case 3: $src = imagecreatefrompng($file); break;
default: return ''; break;
}
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) ){
$tn_width = $width;
$tn_height = $height;
}elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp = imagecreatetruecolor($tn_width,$tn_height);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $tn_width, $tn_height, $transparent);
}
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
/*
* imageXXX() only has two options, save as a file, or send to the browser.
* It does not provide you the oppurtunity to manipulate the final GIF/JPG/PNG file stream
* So I start the output buffering, use imageXXX() to output the data stream to the browser,
* get the contents of the stream, and use clean to silently discard the buffered contents.
*/
ob_start();
switch ($image_type)
{
case 1: imagegif($tmp); break;
case 2: imagejpeg($tmp, NULL, $thumb_quality); break; // best quality
case 3: imagepng($tmp, NULL, 0); break; // no compression
default: echo ''; break;
}
$final_image = ob_get_contents();
ob_end_clean();
return $final_image;
}
// Check if the user entered an image
if ($_FILES['imagefile']['name'] != '') {
$image = scaleImageFileToBlob($_FILES['imagefile']['tmp_name']);
if ($image == '') {
echo 'Image type not supported';
} else {
$image_name = $_FILES['imagefile']['name'];
$image_type = $_FILES['imagefile']['type'];
/* 각각의 저장방법에 따라 출력할때 다르게 출력해야함 */
//$image = addslashes($image);
$image = base64_encode($image);
$query = "insert into img_test (img_file_name,image_type, image) values ('$image_name','$image_type','$image')";
$result = mysql_query($query);
if ($result) {
echo $image;
echo "이미지 등록에 성공했습니다.";
} else {
echo "에러발생";
}
}
}
?>
#### img_print.php
<?
/* db 접속 하기 */
$sql = "select * from img_test where no_idx = 6";
$result = mysql_query($sql);
$tt = array("");
$row = mysql_fetch_array($result);
Header("Content-type:image/jpeg");
/* 저장 상태에 타라 출력물 확인하기 */
//echo $row['image'];
echo base64_decode($row['image']);
#######################################################################
이미지 출력시에는 하나의 이미지만 처리 가능한듯하다.
for / while 문을 사용해서 여러개의 이미지를 한번에 불려오기를 하니,
최초 한개만 출력이 된다.
원인 확인 해야한다.
혹시 이것 처리하신분은 댓글 강추~~ (감사합니다~)