원격이미지 조건에 따른 썸네일 생성
먼저 함수들입니다.
<?php
//변수가 정의 되었으면 해당 값을 반환하고 정의 되어있지 않으면 지정된 기본값을 반환합니다.
function Yvar_check(&$var, $return=''){
return (isset($var)) ? $var : $return;
}
//변수가 정의되지 않았거나 값이 NULL값 이거나, 값이 0, false 일경우 지정된 기본값을 반환합니다.
function Yvar_empty_check(&$var, $return=''){
return (!empty($var)) ? $var : $return;
}
//http, GET 방식의 소켓연결
function Ysockopen($url, $referer='', $accept='*/*', $contenttype='application/x-www-form-urlencoded', $cachecontrol='private', $pragma='no-cache', $connection='close'){
if (!empty($url) && preg_match("`^http://.+`i", $url)) {
$array = parse_url($url);
$url_scheme = Yvar_check($array['scheme']);
$url_server = Yvar_check($array['host']);
$url_path = Yvar_check($array['path']);
$url_query= Yvar_check($array['query']);
$url_port = Yvar_empty_check($array['port'], 80);
$fp = fsockopen($url_server, $url_port);
if (empty($fp)) {
return false;
}
fputs($fp, "GET " . $url_path . "?" . $url_query . " HTTP/1.0\n");
if (!empty($accept)) fputs($fp, "Accept: " . $accept . "\n");
if (!empty($contenttype)) fputs($fp, "Content-Type: " . $contenttype . "\n");
fputs($fp, "Host: $url_server:$url_port\n");
if (!empty($referer)) fputs($fp, "Referer: " . $referer . "\n");
if (!empty($cachecontrol)) fputs($fp, "Cache-control: " . $cachecontrol . "\n");
if (!empty($pragma)) fputs($fp, "Pragma: " . $pragma . "\n");
if (!empty($connection)) fputs($fp, "Connection: " . $connection . "\n\n");
return $fp;
}
else {
return false;
}
}
function Ysockclose(&$fp){
if (!empty($fp)) fclose($fp);
return true;
}
//소켓으로 외부파일 통째로 텍스트로 읽어옴
function Yreadfile($url, $referer='', $accept='*/*', $contenttype='application/x-www-form-urlencoded', $cachecontrol='private', $pragma='no-cache', $connection='close') {
$fp = Ysockopen($url, $referer, $accept, $contenttype, $cachecontrol, $pragma, $connection);
if (!empty($fp)) {
$text = '';
$check_header = false;
while (!feof($fp)){
$temp = fgets($fp, 1024);
if ($check_header == false){
//실제 파일이 아닌 헤더 정보인지 체크 하여 추가하지 않는다.
if (preg_match("'^(\r)?\n$'", $temp)) {
$check_header = true;
continue;
}
}
else{
//헤더 정보가 아닌 경우에만 추가한다.
$text .= $temp;
}
}
Ysockclose($fp);
return $text;
}
else {
return '';
}
}
//원본이미지의 경로, 크기, 높이, 타입을 알 경우에 사용가능
function make_smallimg ($src, $src_w, $src_h, $src_t, $copy, $copy_w, $copy_h, $copy_rule='width', $copy_pos='1'){
if ($src_t == 1)
$src = @imagecreatefromgif($src);
else if ($src_t == 2)
$src = @imagecreatefromjpeg($src);
else if ($src_t == 3)
$src = @imagecreatefrompng($src);
else
return false;
if (empty($src)) return false;
if (empty($copy) || $copy_w < 10 || $copy_h < 10) return false;
$src_x = 0;
$src_y = 0;
$copy_x = 0;
$copy_y = 0;
if ($copy_pos != 5) {//강제 크기 설정
$copy_x = $copy_y = $src_x = $src_y = 0;
$new_w = $copy_w;
$new_h = $copy_h;
}
else{
if ($copy_rule == 'width' || empty($copy_rule)) {//너비에 맞춤
$rate = $src_h / $src_w;
$new_w = $copy_w;
$new_h = (int) ($rate * $copy_w);
if ($new_h < $copy_h){//만들어질 썸네일 높이가 비율대로 줄여진 높이보다 클경우 가운데 위치시킴
if ($copy_pos != 4) $copy_y = (int) (($copy_h - $new_h) / 2);
else $copy_h = $new_h;//비율대로
}
else {
if ($copy_pos == '1' || empty($copy_pos)) {//원본에서 상단을 기준으로 가져옴
//기본값 그대로
}
else if ($copy_pos == '2') {//원본에서 중앙을 기준으로 가져옴
$temp_h = (int) ($copy_h / $copy_w * $src_w);
$src_y = (int) (($src_h - $temp_h) / 2);
}
else if ($copy_pos == '3') {//원본에서 하단을 기준으로 가져옴
$temp_h = (int) ($copy_h / $copy_w * $src_w);
$src_y = $src_h - $temp_h;
}
else if ($copy_pos == '4') {//비율대로
$copy_h = $new_h;
}
}
}
else {//높이에 맞춤
$rate = $src_w / $src_h;
$new_h = $copy_h;
$new_w = (int) ($rate * $copy_h);
if ($new_w < $copy_w){//만들어질 썸네일 너비가 비율대로 줄여진 너비보다 클경우 가운데 위치시킴
if ($copy_pos != 4) $copy_x = (int) (($copy_w - $new_w) / 2);
else $copy_w = $new_w;//비율대로
}
else {
if ($copy_pos == '1' || empty($copy_pos)) {//원본에서 왼쪽을 기준으로 가져옴
//기본값 그대로
}
else if ($copy_pos == '2') {//원본에서 중앙을 기준으로 가져옴
$temp_w = (int) ($copy_w / $copy_h * $src_h);
$src_x = (int) (($src_w - $temp_w) / 2);
}
else if ($copy_pos == '3') {//원본에서 오른쪽을 기준으로 가져옴
$temp_w = (int) ($copy_w / $copy_h * $src_h);
$src_x = $src_w - $temp_w;
}
else if ($copy_pos == '4') {//비율대로
$copy_w = $new_w;
}
}
}
}
$dst = @imagecreatetruecolor($copy_w, $copy_h);
if (empty($dst)) return false;
$background_color = @imagecolorallocate($dst, 255, 255, 255);
if (empty($background_color)) return false;
imagefilledrectangle($dst, 0, 0, $copy_w, $copy_h, $background_color);
imagecopyresampled($dst, $src, $copy_x, $copy_y, $src_x, $src_y, $new_w, $new_h, $src_w, $src_h);
imagepng($dst, $copy);
chmod($copy, 0606);
return true;
}
function make_small_remoteimage($image_url, $referer, $save_path, $copy, $copy_w, $copy_h, $copy_rule='width', $copy_pos='1'){
$image_url = trim($image_url);
if (empty($image_url) || !preg_match("`^http`i", $image_url)) return false;//원격이미지 주소 없음
//원격이미지 주소에서 이미지 이름 뽑아냄
preg_match("`/([^/]+)$`i", $image_url, $m);
$image_name = $m[1];
$save_path = trim($save_path);
if (empty($save_path) || !is_dir($save_path)) return false;//임시저장 디렉토리 없음
$referer = trim($referer);
if (empty($referer) || !preg_match("`^http`i", $referer)) {
preg_match("`(https?://[^/]+)/`i", $image_url, $m);
$referer = $m[1];
}
//원격이미지 가져와서 임시저장 디렉토리에 저장
$text = Yreadfile($image_url, $referer);
if (empty($text)) return false;//이미지 읽어오기 실패
$temp_file = preg_replace("`/+$`", '', $save_path) . '/' . uniqid('') . '_' . $image_name;
$fp = @fopen ($temp_file, "w");
@fwrite($fp, $text);
@fclose($fp);
@chmod ($temp_file, 0777);
if (!is_file($temp_file)) return false;//파일 생성 실패
$temp = getimagesize($temp_file);
$src_w = $temp[0];
$src_h = $temp[1];
$src_t = $temp[2];
//썸네일 규칙에 따라 썸네일 생성
$return = make_smallimg ($temp_file, $src_w, $src_h, $src_t, $copy, $copy_w, $copy_h, $copy_rule='width', $copy_pos='1');
@unlink($temp_file);
return $return;
}
사용법은
make_small_remoteimage($image_url, $referer, $save_path, $copy, $copy_w, $copy_h, $copy_rule='width', $copy_pos='1');
순서대로
이미지url
리퍼러
임시저장경로(./temp)
썸네일 저장경로(./temp/abc.png)
썸네일 너비
썸네일 높이
썸네일 생성기준 width 나 height
썸네일 생성 방법 1에서 5까지, 5는 지정된 너비, 높이로 강제 생성
댓글 4개
감사합니다.
일단 적용해보겠습니다.
여의봉보다 더 좋은 천하의 명검 손에 쥐어준들
내공이 감당치 못하니 오호 애제라 오호 애제라
질문 1
위화일을 ~lib.php 로 만들어서,
스킨 안에 집어넣고,
리스트에서 includ_once 한 후,
필요한 곳에
----- make_small_remoteimage($image_url, $referer, $save_path, $copy, $copy_w, $copy_h, $copy_rule='width', $copy_pos='2'); ----
하라는 말씀인가요?
죄송하지만 위의 소스를 적용한 2차소스를 참고할 수 없을까요?
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 2430 | 15년 전 | 2501 | ||
| 2429 | 15년 전 | 5492 | ||
| 2428 | 15년 전 | 1459 | ||
| 2427 |
Sizkein
|
15년 전 | 4224 | |
| 2426 | 15년 전 | 1352 | ||
| 2425 | 15년 전 | 1646 | ||
| 2424 | 15년 전 | 1614 | ||
| 2423 |
haejuksun
|
15년 전 | 1606 | |
| 2422 |
haejuksun
|
15년 전 | 1116 | |
| 2421 |
haejuksun
|
15년 전 | 1360 | |
| 2420 |
haejuksun
|
15년 전 | 1508 | |
| 2419 |
|
15년 전 | 1403 | |
| 2418 |
|
15년 전 | 2128 | |
| 2417 |
원더풀라입
|
15년 전 | 1278 | |
| 2416 |
원더풀라입
|
15년 전 | 1327 | |
| 2415 |
원더풀라입
|
15년 전 | 873 | |
| 2414 |
원더풀라입
|
15년 전 | 991 | |
| 2413 |
원더풀라입
|
15년 전 | 952 | |
| 2412 |
원더풀라입
|
15년 전 | 918 | |
| 2411 |
원더풀라입
|
15년 전 | 869 | |
| 2410 |
원더풀라입
|
15년 전 | 924 | |
| 2409 |
원더풀라입
|
15년 전 | 1073 | |
| 2408 |
원더풀라입
|
15년 전 | 1081 | |
| 2407 |
원더풀라입
|
15년 전 | 2605 | |
| 2406 |
원더풀라입
|
15년 전 | 941 | |
| 2405 |
원더풀라입
|
15년 전 | 1591 | |
| 2404 |
원더풀라입
|
15년 전 | 1458 | |
| 2403 | 15년 전 | 1580 | ||
| 2402 | 15년 전 | 1289 | ||
| 2401 |
나도빌더만들까
|
15년 전 | 1770 | |
| 2400 | 15년 전 | 594 | ||
| 2399 | 15년 전 | 1080 | ||
| 2398 | 15년 전 | 1048 | ||
| 2397 | 15년 전 | 2837 | ||
| 2396 | 15년 전 | 3151 | ||
| 2395 | 15년 전 | 1828 | ||
| 2394 | 15년 전 | 1335 | ||
| 2393 |
gamebomb
|
15년 전 | 1956 | |
| 2392 |
gamebomb
|
15년 전 | 1464 | |
| 2391 |
gamebomb
|
15년 전 | 1062 | |
| 2390 | 15년 전 | 3758 | ||
| 2389 |
leadK
|
15년 전 | 1404 | |
| 2388 |
kim7031
|
15년 전 | 1503 | |
| 2387 | 15년 전 | 2727 | ||
| 2386 | 15년 전 | 1433 | ||
| 2385 | 15년 전 | 4157 | ||
| 2384 |
확실한방법
|
15년 전 | 2368 | |
| 2383 |
intel
|
15년 전 | 3144 | |
| 2382 |
확실한방법
|
15년 전 | 1756 | |
| 2381 |
|
15년 전 | 1989 | |
| 2380 |
|
15년 전 | 1298 | |
| 2379 | 15년 전 | 4371 | ||
| 2378 | 15년 전 | 3941 | ||
| 2377 |
메가팩토리
|
15년 전 | 3770 | |
| 2376 | 15년 전 | 5179 | ||
| 2375 | 15년 전 | 3403 | ||
| 2374 | 15년 전 | 1783 | ||
| 2373 | 15년 전 | 1743 | ||
| 2372 | 15년 전 | 1888 | ||
| 2371 | 15년 전 | 1588 | ||
| 2370 | 15년 전 | 3986 | ||
| 2369 | 15년 전 | 2895 | ||
| 2368 | 15년 전 | 2093 | ||
| 2367 | 15년 전 | 1661 | ||
| 2366 | 15년 전 | 1900 | ||
| 2365 | 15년 전 | 2380 | ||
| 2364 | 15년 전 | 1469 | ||
| 2363 | 15년 전 | 2402 | ||
| 2362 | 15년 전 | 1728 | ||
| 2361 | 15년 전 | 1822 | ||
| 2360 | 15년 전 | 1470 | ||
| 2359 | 15년 전 | 2163 | ||
| 2358 | 15년 전 | 1496 | ||
| 2357 |
|
15년 전 | 1912 | |
| 2356 |
헐크매니아
|
15년 전 | 2111 | |
| 2355 |
WebRIA
|
15년 전 | 1187 | |
| 2354 |
WebRIA
|
15년 전 | 1240 | |
| 2353 |
WebRIA
|
15년 전 | 1882 | |
| 2352 |
WebRIA
|
15년 전 | 5328 | |
| 2351 | 15년 전 | 1626 | ||
| 2350 |
WebRIA
|
15년 전 | 2139 | |
| 2349 | 15년 전 | 2335 | ||
| 2348 |
WebRIA
|
15년 전 | 1484 | |
| 2347 |
WebRIA
|
15년 전 | 1296 | |
| 2346 |
WebRIA
|
15년 전 | 2210 | |
| 2345 |
WebRIA
|
15년 전 | 4933 | |
| 2344 | 15년 전 | 2200 | ||
| 2343 | 15년 전 | 2862 | ||
| 2342 | 15년 전 | 1746 | ||
| 2341 | 15년 전 | 3592 | ||
| 2340 | 15년 전 | 4425 | ||
| 2339 | 15년 전 | 3106 | ||
| 2338 | 15년 전 | 2352 | ||
| 2337 | 15년 전 | 2432 | ||
| 2336 | 15년 전 | 2681 | ||
| 2335 | 15년 전 | 2164 | ||
| 2334 |
Sizkein
|
15년 전 | 5502 | |
| 2333 | 15년 전 | 3365 | ||
| 2332 | 15년 전 | 1351 | ||
| 2331 | 15년 전 | 1695 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기