chatgpt PHP Proxy 이미지 중계 예제
페이지 정보
작성자 관리자 작성일 25-04-29 17:27 조회 100 댓글 0본문
PHP Proxy 이미지 중계
PHP에서 cURL
을 사용하면, HTTP로 된 외부 이미지를 서버 쪽에서 중계(proxy) 해주어, 클라이언트(브라우저)는 혼합 콘텐츠 문제 없이 이미지를 볼 수 있습니다.
즉, HTTPS 사이트에서 HTTP 이미지를 직접 불러오지 않고, PHP가 이미지를 받아서 다시 보내주는 방식입니다.
<?php
// 예: image-proxy.php?url=http://example.com/image.jpg
// 외부 이미지 URL
$url = $_GET['url'] ?? '';
// URL 유효성 검사
if (!filter_var($url, FILTER_VALIDATE_URL) || !str_starts_with($url, 'http://')) {
http_response_code(400);
echo 'Invalid URL';
exit;
}
// 이미지 가져오기
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // HTTP라서 필요 없음
$data = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
// MIME 타입 설정
$contentType = $info['content_type'] ?? 'image/jpeg';
header("Content-Type: $contentType");
// 이미지 출력
echo $data;
HTML 사용 예시
<img src="https://yourdomain.com/image-proxy.php?url=http://external.com/image.jpg">
확장자 | MIME 타입 | 작동 여부 |
---|---|---|
.jpg | image/jpeg | ✅ 가능 |
.png | image/png | ✅ 가능 |
.gif | image/gif | ✅ 가능 |
.svg | image/svg+xml | ✅ 가능 |
.webp | image/webp | ✅ 가능 (브라우저 지원 필요) |
댓글목록 0
등록된 댓글이 없습니다.