MIME 이용 이미지 함수 간단 사용
▶ DreamPHP.com - 비혼의 조그만 세상 (http://dreamphp.com/)
getimagesize() 함수를 이용해 아래와 같이 이미지의 크기와 종류를 구할 수 있습니다.
Array
(
[0] => 1024
[1] => 768
[2] => 2
[3] => width="1024" height="768"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
위 정보와 가변 함수를 이용하면, 이미지 함수를 간편하게 사용할 수 있습니다.

실행 예 - 첨부 파일의 test.php 참고
getimagesize() 함수를 이용해 아래와 같이 이미지의 크기와 종류를 구할 수 있습니다.
Array
(
[0] => 1024
[1] => 768
[2] => 2
[3] => width="1024" height="768"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
위 정보와 가변 함수를 이용하면, 이미지 함수를 간편하게 사용할 수 있습니다.
image.php
<?php
// DreamPHP.com - 비혼의 조그만 세상
// http://dreamphp.com/ by BiHon
function not()
{
$im = imagecreatefrompng('./not.png');
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
exit;
}
if ( !isset($_GET['file']) || !is_file('./'.basename($_GET['file'])) ) not(); // 미지정, 파일 없음
$file = './'.basename($_GET['file']);
$mime['image/gif'] = 'gif';
$mime['image/jpeg'] = 'jpeg';
$mime['image/png'] = 'png';
$size = getimagesize($file);
if ( !$size || !isset($mime[$size['mime']]) ) not(); // 이미지 아니거나 지원하지 않음
$load = 'imagecreatefrom'.$mime[$size['mime']]; // ★ imagecreatefrom + gif, jpeg, png
$save = 'image'.$mime[$size['mime']]; // ★ image + gif, jpeg, png
$image = $load($file);
header('Content-Type: '.$size['mime']);
$save($image);
imagedestroy($image);
?>
// DreamPHP.com - 비혼의 조그만 세상
// http://dreamphp.com/ by BiHon
function not()
{
$im = imagecreatefrompng('./not.png');
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
exit;
}
if ( !isset($_GET['file']) || !is_file('./'.basename($_GET['file'])) ) not(); // 미지정, 파일 없음
$file = './'.basename($_GET['file']);
$mime['image/gif'] = 'gif';
$mime['image/jpeg'] = 'jpeg';
$mime['image/png'] = 'png';
$size = getimagesize($file);
if ( !$size || !isset($mime[$size['mime']]) ) not(); // 이미지 아니거나 지원하지 않음
$load = 'imagecreatefrom'.$mime[$size['mime']]; // ★ imagecreatefrom + gif, jpeg, png
$save = 'image'.$mime[$size['mime']]; // ★ image + gif, jpeg, png
$image = $load($file);
header('Content-Type: '.$size['mime']);
$save($image);
imagedestroy($image);
?>
실행 예 - 첨부 파일의 test.php 참고
함께 나눈 이야기
댓글댓글 남기기