이미지 업로드시 리사이즈 하면 이미지 까맣게 나오는 증상이 있네요.
본문
이미지 업로드시 리사이즈 하면 이미지 까맣게 나오는 증상이 있네요.
어떤 경우에는 됐다가 어떤 경우에는 안됐다가 하네요.
이미지를 편집해서 올리는데, 이때 문제일까요?
소스 코드는 아래와 같습니다.
<?
function resize_bo($string, $iwidth=0)
{
global $g4, $board;
global $ret;
//print_r($board);
// 전역변수를 받아들이기 때문에 설정값이 없으면, 기본으로 게시판의 폭을 지정. 게시판 폭도 없으면 기본으로 500
$max_img_width = $iwidth;
if ($max_img_width <= 0) {
if ((int)$board['bo_image_width'] > 0)
$max_img_width = $board['bo_image_width'];
else
$max_img_width = 500;
}
// max_img_height에 값이 있는 경우는 crop을 허용 합니다.
$max_img_height = (int) $board['resize_img_height'];
$is_crop = false;
if ($max_img_height > 0)
$is_crop = true;
// 실행할 때마다 image를 create할지 설정 (무조건 false)
$is_create = false;
// 이미지의 quality 값을 설정 (없으면, thumb의 기본값으로 70이 적용됨)
$quality = (int) $board['resize_img_quality'];
if ($quality <= 0)
$quality = 70;
// $board[thumb_create]에 값이 있으면 무조건 썸네일을 생성 합니다.
if ($board[thumb_create])
$thumb_create = 1;
// 이미지 필터 - 기본으로 UnSharpMask
if ($board[image_filter]) {
$filter[type] = $board[image_filter][type];
$filter[arg1] = $board[image_filter][arg1];
$filter[arg2] = $board[image_filter][arg2];
$filter[arg3] = $board[image_filter][arg3];
$filter[arg4] = $board[image_filter][arg4];
} else {
$filter[type] = 99;
$filter[arg1] = 10;
$filter[arg2] = 1;
$filter[arg3] = 2;
}
// 변수를 setting
$return = $string['0'];
preg_match_all('@(?P<attribute>[^\s\'\"]+)\s*=\s*(\'|\")?(?P<value>[^\s\'\"]+)(\'|\")?@i', $return, $match);
$img['src'] = $string;
// 실제 디렉토리 이름을 구하고 절대경로에서 잘라낼 글자수를 계산
$real_dir = dirname($_SERVER['DOCUMENT_ROOT'] . "/nothing");
$cut_len = strlen($real_dir);
// 가끔씩 img의 파일이름이 깨어지는 경우가 있어서 decoding 해줍니다 (예: on = on)
$img['src'] = html_entity_decode($img[src]);
// 이미지 파일의 경로를 설정 (외부? 내부? 내부인경우 절대경로? 상대경로?)
if (eregi("^(http|https|ftp|telnet|news|mms)://", $img['src'])) {
// 내 서버에 있는 이미지?
$img_src = @getimagesize($img['src']);
if (preg_match("/" . $_SERVER[HTTP_HOST] . "/", $img[src], $matches)) {
$url = parse_url($img[src]);
$img[src] = $url[path];
$thumb_path = "1";
} else {
$thumb_path = "";
}
} else {
$thumb_path="1";
}
if ($thumb_path) {
$dir = dirname(file_path($img['src']));
$file = basename($img['src']);
$img_path = $dir . "/" . $file;
// 첨부파일의 이름은 urlencode로 들어가게 됩니다. 따라서, decode해줘야 합니다. (/bbs/write_update.php 참조)
$img_path = urldecode($img_path);
$img_src = @getimagesize($img_path);
// 잊어버리지말고 여기도 urldecode 해줘야죠?
$thumb_path = urldecode($img['src']);
}
// 이미지파일의 정보를 얻지 못했을 때
if (!$img_src) {
return $return;
}
// 무조건 이미지 생성이더라도,
// 이미지생성의 최소 넓이가 있으면, 이미지가 그 크기 이상일때만, 썸을 만들어야징.
// 이거는 작은 아이콘 같은 것의 썸을 만들지 않게 하려고 하는거임
if ($thumb_create && $board[image_min] && $img_src[0] < $board[image_min])
return $return;
// 무조건 이미지 생성이더라도,
// 이미지생성의 최소 파일용량이 있으면, 이미지가 그 파일크기 이상일때만, 썸을 만들어야징.
// 이거는 작은 아이콘 같은 것이나 효율적으로 줄어든 이미지의 썸을 만들지 않게 하려고 하는거임
if ($thumb_create && $board[image_min_kb]) {
// 파일의 크기를 구해서
$fsize = filesize2bytes(filesize($img_path));
}
if(isset($img['width']) == false) {
$img_width = $img_src[0];
$img_height = $img_src[1];
} else {
$img_width = $img['width'];
$img_height = $img['height'];
}
if((int)$img_width > $max_img_width)
{
// width를 조정
if (isset($img['width']) == true)
$return = preg_replace('/width\=(\'|\")?[^\s\'\"]+(\'|\")?/i', 'width="' . $max_img_width . '"', $return);
else
$return = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 width='" . $max_img_width . "' \\2 \\3", $return);
// height를 삭제
$return = preg_replace('/height\=(\'|\")?[^\s\'\"]+(\'|\")?/i', null, $return);
// 이름도 그누의 javascript resize할 수 있게 수정
if (isset($img[name]) == true)
$return = preg_replace('/name\=(\'|\")?[^\s\'\"]+(\'|\")?/i', ' name="target_resize_image[]" ', $return);
else
$return = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' \\2 \\3", $return);
// thumbnail을 생성
if ($thumb_path) {
include_once("$g4[path]/lib/thumb.lib.php");
$thumb_path=thumbnail($thumb_path, $max_img_width,$max_img_height,$is_create,$is_crop,$quality, "", "", $filter,"","","true");
$ret['width'] = $max_img_width;
$ret['height'] = $max_img_height;
$ret['thumb_path'] = $thumb_path;
}
}
else
{
// width를 조정
if (isset($img['width']) == true)
$return = preg_replace('/width\=(\'|\")?[^\s\'\"]+(\'|\")?/i', 'width="' . $img_width . '"', $return);
else
$return = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 width='" . $img_width . "' \\2 \\3", $return);
// height를 삭제
$return = preg_replace('/height\=(\'|\")?[^\s\'\"]+(\'|\")?/i', null, $return);
// 이름도 그누의 javascript resize할 수 있게 수정
if (isset($img[name]) == true)
$return = preg_replace('/name\=(\'|\")?[^\s\'\"]+(\'|\")?/i', ' name="target_resize_image[]" ', $return);
else
$return = preg_replace("/(\<img )([^\>]*)(\>)/i", "\\1 name='target_resize_image[]' \\2 \\3", $return);
// $thumb_create가 true이면, 이미지 크기가 $max_img_width보다 작지만, 그래도 thumb를 생성
if ($thumb_create && $thumb_path) {
include_once("$g4[path]/lib/thumb.lib.php");
$thumb_path=thumbnail($thumb_path, $max_img_width,$max_img_height,$is_create,$is_crop,$quality, "", "", $filter,"","","true");
$ret['width'] = $max_img_width;
$ret['height'] = $max_img_height;
$ret['thumb_path'] = $thumb_path;
}
}
return $ret;
}
if(!function_exists('file_path')){
function file_path($path) {
$dir = dirname($path);
$file = basename($path);
if (substr($dir,0,1) == "/") {
$real_dir = dirname($_SERVER['DOCUMENT_ROOT'] . "/nothing");
$dir = $real_dir . $dir;
}
return $dir . "/" . $file;
}
}
?>
답변 2
gd 로 줄일 때 투명 배경은 따로 설정해 줘야 합니다..
기본값으로 하면 검은 배경이 됩니다.
아래 것으로 해보세요
[code]
<?php
function resize_bo($string, $iwidth=0)
{
global $g4, $board;
global $ret;
$max_img_width = $iwidth;
if ($max_img_width <= 0) {
if ((int)$board['bo_image_width'] > 0)
$max_img_width = $board['bo_image_width'];
else
$max_img_width = 500;
}
$max_img_height = (int) $board['resize_img_height'];
$is_crop = $max_img_height > 0;
$is_create = false;
$quality = (int) $board['resize_img_quality'] > 0 ? (int) $board['resize_img_quality'] : 70;
$thumb_create = $board['thumb_create'] ?? 0;
if ($board['image_filter']) {
$filter = $board['image_filter'];
} else {
$filter = ['type' => 99, 'arg1' => 10, 'arg2' => 1, 'arg3' => 2];
}
$return = $string[0];
preg_match_all('@(?P<attribute>[^\s\'\"]+)\s*=\s*(\'|\")?(?P<value>[^\s\'\"]+)(\'|\")?@i', $return, $match);
$img['src'] = html_entity_decode($string);
$real_dir = dirname($_SERVER['DOCUMENT_ROOT'] . "/nothing");
$cut_len = strlen($real_dir);
if (eregi("^(http|https|ftp|telnet|news|mms)://", $img['src'])) {
$img_src = @getimagesize($img['src']);
if (preg_match("/" . $_SERVER['HTTP_HOST'] . "/", $img['src'])) {
$url = parse_url($img['src']);
$img['src'] = $url['path'];
$thumb_path = "1";
} else {
$thumb_path = "";
}
} else {
$thumb_path = "1";
}
if ($thumb_path) {
$dir = dirname(file_path($img['src']));
$file = basename($img['src']);
$img_path = urldecode($dir . "/" . $file);
$img_src = @getimagesize($img_path);
$thumb_path = urldecode($img['src']);
}
if (!$img_src) return $return;
if ($thumb_create && $board['image_min'] && $img_src[0] < $board['image_min']) return $return;
if ($thumb_create && $board['image_min_kb']) {
$fsize = filesize2bytes(filesize($img_path));
}
$img_width = $img_src[0];
$img_height = $img_src[1];
if ((int)$img_width > $max_img_width) {
$return = preg_replace('/width=(\'|\")?[^\s\'\"]+(\'|\")?/i', 'width="' . $max_img_width . '"', $return);
$return = preg_replace('/height=(\'|\")?[^\s\'\"]+(\'|\")?/i', null, $return);
$return = preg_replace('/name=(\'|\")?[^\s\'\"]+(\'|\")?/i', ' name="target_resize_image[]" ', $return);
$return = preg_replace("/(<img )([^>]*)(>)/i", "\\1 name='target_resize_image[]' \\2 \\3", $return);
if ($thumb_path) {
include_once("$g4[path]/lib/thumb.lib.php");
$thumb_path = thumbnail($thumb_path, $max_img_width, $max_img_height, $is_create, $is_crop, $quality, '', '', $filter, '', '', 'true');
$ret['width'] = $max_img_width;
$ret['height'] = $max_img_height;
$ret['thumb_path'] = $thumb_path;
// 투명 배경 처리
$file_ext = pathinfo($thumb_path, PATHINFO_EXTENSION);
$dst = imagecreatetruecolor($max_img_width, $max_img_height);
if ($file_ext === 'png') {
imagealphablending($dst, false);
imagesavealpha($dst, true);
} else if ($file_ext === 'gif') {
$src = @imagecreatefromgif($thumb_path);
$src_transparency = @imagecolortransparent($src);
$palletsize = imagecolorstotal($src);
if ($src_transparency >= 0 && $src_transparency < $palletsize) {
$transparent_color = imagecolorsforindex($src, $src_transparency);
$current_transparent = imagecolorallocate($dst, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($dst, 0, 0, $current_transparent);
imagecolortransparent($dst, $current_transparent);
}
}
}
} else {
$return = preg_replace('/width=(\'|\")?[^\s\'\"]+(\'|\")?/i', 'width="' . $img_width . '"', $return);
$return = preg_replace('/height=(\'|\")?[^\s\'\"]+(\'|\")?/i', null, $return);
$return = preg_replace('/name=(\'|\")?[^\s\'\"]+(\'|\")?/i', ' name="target_resize_image[]" ', $return);
$return = preg_replace("/(<img )([^>]*)(>)/i", "\\1 name='target_resize_image[]' \\2 \\3", $return);
if ($thumb_create && $thumb_path) {
include_once("$g4[path]/lib/thumb.lib.php");
$thumb_path = thumbnail($thumb_path, $max_img_width, $max_img_height, $is_create, $is_crop, $quality, '', '', $filter, '', '', 'true');
$ret['width'] = $max_img_width;
$ret['height'] = $max_img_height;
$ret['thumb_path'] = $thumb_path;
}
}
return $ret;
}
if (!function_exists('file_path')) {
function file_path($path) {
$dir = dirname($path);
$file = basename($path);
if (substr($dir, 0, 1) == "/") {
$real_dir = dirname($_SERVER['DOCUMENT_ROOT'] . "/nothing");
$dir = $real_dir . $dir;
}
return $dir . "/" . $file;
}
}
?>
[code]
/lib/thumbnail.lib.php 파일에 있는 아래 함수를 이용해서 줄이는 것을 권합니다.
function thumbnail($filename, $source_path, $target_path, $thumb_width, $thumb_height, $is_create, $is_crop=false, $crop_mode='center', $is_sharpen=false, $um_value='80/0.5/3')