디비에서 읽어온 이미지를 화면에 뿌릴때는 제가 정한 사이즈로 뿌리고 싶습니다.
자르기라기보단 비율을 줄이는게 맞겠네요.
이에 관한 예제나 해법을 알고 계시면 알려주세요.
답변 2개 / 댓글 4개
채택된 답변
+20 포인트
Innisfree
2018-12-20 (목) 11:57:00
안녕하세요?
평정심 님 말씀대로 관리자모드 설정에서 조절하는 방법도 있겠지만
서버 내부의 이미지를 원하는 크기로 리사이징(확대 또는 축소)해서 보여주려면
다음과 같은 PHP 스크립트를 작성할 수 있겠네요 ^^
다만 이 방법은 웹호스팅의 설정에 따라 getimagesize() 함수가 외부서버에 대해 작동하지 않을 수 있고,
대용량 파일의 경우에는 제대로 작동하지 않을 가능성이 있습니다.
Copy
<?php
function resize_image($file, $dst_w, $dst_h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$ratio = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($ratio-$dst_w/$dst_h)));
} else {
$height = ceil($height-($height*abs($ratio-$dst_w/$dst_h)));
}
$newwidth = $dst_w;
$newheight = $dst_h;
} else {
if ($dst_w/$dst_h > $ratio) {
$newwidth = $dst_h*$ratio;
$newheight = $dst_h;
} else {
$newheight = $dst_w/$ratio;
$newwidth = $dst_w;
}
}
$src = imagecreatefromjpeg($file);
$result = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($result, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $result;
}
$img = resize_image('PATH', WIDTH, HEIGHT); // 파일경로, 폭, 높이를 입력하세요
ob_start();
imagejpeg($img);
$output = base64_encode(ob_get_contents());
ob_end_clean();
?>
<img src="data:image/jpeg;base64,<?php echo $output; ?>"/>
답변에 대한 댓글 4개
Evelyn
2018-12-20 (목) 13:58:29
안녕하세요?
채택해주셔서 감사합니다 ^^
PNG 파일의 경우에는 22, 29번째 행에 있는 jpeg를 png로 변경하시면
PNG에 해당하는 PHP 함수가 있기 때문에 정상작동합니다 :)
그리고 33번째 행도 png로 바꿔주시면 됩니다.
조금 더 간단하게 다양한 확장자를 처리하고 싶으시다면 아래 URL의 스크립트를 참고하세요~
switch 문을 이용하여 확장자를 해결하였습니다.
https://ourcodeworld.com/articles/read/197/how-to-resize-an-image-and-reduce-quality-in-php-without-imagick
그럼 뜻깊은 연말 되세요~! 감사합니다 ^-^
채택해주셔서 감사합니다 ^^
PNG 파일의 경우에는 22, 29번째 행에 있는 jpeg를 png로 변경하시면
PNG에 해당하는 PHP 함수가 있기 때문에 정상작동합니다 :)
그리고 33번째 행도 png로 바꿔주시면 됩니다.
조금 더 간단하게 다양한 확장자를 처리하고 싶으시다면 아래 URL의 스크립트를 참고하세요~
switch 문을 이용하여 확장자를 해결하였습니다.
https://ourcodeworld.com/articles/read/197/how-to-resize-an-image-and-reduce-quality-in-php-without-imagick
그럼 뜻깊은 연말 되세요~! 감사합니다 ^-^
Evelyn
2018-12-20 (목) 14:09:21
[code]
<?php
function resize_image($file, $dst_w, $dst_h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$ratio = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($ratio-$dst_w/$dst_h)));
} else {
$height = ceil($height-($height*abs($ratio-$dst_w/$dst_h)));
}
$newwidth = $dst_w;
$newheight = $dst_h;
} else {
if ($dst_w/$dst_h > $ratio) {
$newwidth = $dst_h*$ratio;
$newheight = $dst_h;
} else {
$newheight = $dst_w/$ratio;
$newwidth = $dst_w;
}
}
$exploding = explode(".",$file);
$ext = end($exploding);
switch($ext){
case "png":
$src = imagecreatefrompng($file);
break;
case "jpeg":
case "jpg":
$src = imagecreatefromjpeg($file);
break;
case "gif":
$src = imagecreatefromgif($file);
break;
default:
$src = imagecreatefromjpeg($file);
break;
}
$result = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($result, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return array($result, $ext);
}
$img = resize_image('PATH', WIDTH, HEIGHT); // 파일경로, 폭, 높이를 입력하세요
ob_start();
switch($img[1]){
case "png":
$src = imagepng($img[0]);
break;
case "jpeg":
case "jpg":
$src = imagejpeg($img[0]);
break;
case "gif":
$src = imagegif($img[0]);
break;
default:
$src = imagejpeg($img[0]);
break;
}
$output = base64_encode(ob_get_contents());
ob_end_clean();
?>
<img src="data:image/<?php echo $img[1]; ?>;base64,<?php echo $output; ?>"/>
[/code]
이와 같은 방법으로 JPG와 PNG 모두 잘 작동하는 것을 확인했습니다 ^-^
<?php
function resize_image($file, $dst_w, $dst_h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$ratio = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($ratio-$dst_w/$dst_h)));
} else {
$height = ceil($height-($height*abs($ratio-$dst_w/$dst_h)));
}
$newwidth = $dst_w;
$newheight = $dst_h;
} else {
if ($dst_w/$dst_h > $ratio) {
$newwidth = $dst_h*$ratio;
$newheight = $dst_h;
} else {
$newheight = $dst_w/$ratio;
$newwidth = $dst_w;
}
}
$exploding = explode(".",$file);
$ext = end($exploding);
switch($ext){
case "png":
$src = imagecreatefrompng($file);
break;
case "jpeg":
case "jpg":
$src = imagecreatefromjpeg($file);
break;
case "gif":
$src = imagecreatefromgif($file);
break;
default:
$src = imagecreatefromjpeg($file);
break;
}
$result = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($result, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return array($result, $ext);
}
$img = resize_image('PATH', WIDTH, HEIGHT); // 파일경로, 폭, 높이를 입력하세요
ob_start();
switch($img[1]){
case "png":
$src = imagepng($img[0]);
break;
case "jpeg":
case "jpg":
$src = imagejpeg($img[0]);
break;
case "gif":
$src = imagegif($img[0]);
break;
default:
$src = imagejpeg($img[0]);
break;
}
$output = base64_encode(ob_get_contents());
ob_end_clean();
?>
<img src="data:image/<?php echo $img[1]; ?>;base64,<?php echo $output; ?>"/>
[/code]
이와 같은 방법으로 JPG와 PNG 모두 잘 작동하는 것을 확인했습니다 ^-^
2018-12-20 (목) 11:41:56
관리자모드 > 해당게시판 수정 들어가셔서
이미지 폭 크기를 조절하시면 될 것 같은데요.
답변을 작성하려면 로그인이 필요합니다.