gdImageCreateFromPng / fopen
글쓴이: zzuina / 작성시간: 월, 2007/07/09 - 11:31오전
URL을 입력받아 이미지를 /tmp 디렉토리에 다운로드 받고 이미지의 전체 픽셀의 rgb값을 뽑아내려고 합니다.
현재 아래 코드처럼 구현을 해놓았는데요,
jpg URL을 주면 fopen에서 에러가 나고,
png URL을 주면 gdImageCreateFromPng에서 에러가 나는 듯 싶습니다.
무엇이 잘못된건지 못찾겠네요 ㅠㅠ
고수님들의 도움을 부탁드립니다 >_ <
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>
#define BUF_SIZE 1024
#define MAX_URL_LEN 1024
char url[MAX_URL_LEN]; // URL String
char fileName[BUF_SIZE]; // File Name
int dw_file();
int make_rgb_file();
int main(int argc, char *argv[])
{
if(argv[1]!=0) {
strncpy(url, argv[1], MAX_URL_LEN);
} else {
printf("ERROR: URL input error!\n");
return -1;
}
/* download file from URL */
dw_file();
/* extract the extension and call the function */
make_rgb_file();
return 0;
}
int dw_file()
{
char wg_cmd[BUF_SIZE] = "wget "; // wget command
char wg_cmd_opt[100] = " -P /tmp -nH -r";
char bn_cmd[BUF_SIZE] = "basename "; // basename command
FILE *bn_result;
/* command: wget [URL] -P /tmp -nH -r
-P /tmp: set download directory
-nH: no host-directories
-r: re-downloading and overwriting */
strncat(wg_cmd, url, BUF_SIZE);
strncat(wg_cmd, wg_cmd_opt, BUF_SIZE);
/* command: basename [URL] */
strncat(bn_cmd, url, BUF_SIZE);
/* execute wget shell command */
if(system(wg_cmd)!=0) {
printf("ERROR: wget command error!\n");
return -1;
}
printf("\n\n");
/* execute basename shell command and get file name */
if((bn_result = popen(bn_cmd, "r"))!=0) {
fgets(fileName, BUF_SIZE, bn_result);
printf("FILENAME: %s\n", fileName);
} else {
printf("ERROR: basename command error!\n");
return -1;
}
return 0;
}
int make_rgb_file()
{
char *ext;
char path[BUF_SIZE] = "/tmp/";
FILE *fp;
gdImagePtr im;
ext = rindex(fileName, (int)'.');
strncat(path, fileName, BUF_SIZE);
printf("%s\n", path);
if((fp=fopen(path, "rb")) == NULL ) {
printf("ERROR: file open error!\n");
return -1;
} else {
if(!strncmp(ext,".jpg",4) || !strncmp(ext,".jpeg",4) ||
!strncmp(ext,".JPG",4) || !strncmp(ext,".JPEG",4)) {
printf("jpg area\n");
} else if(!strncmp(ext,".gif",3) || !strncmp(ext,".GIF",3)) {
printf("gif area\n");
} else if(!strncmp(ext,".png",3) || !strncmp(ext,".PNG",3)) {
printf("png area\n");
if((im = gdImageCreateFromPng(fp))==NULL) {
printf("ERROR: image create error!\n");
return -1;
} else {
printf("%d %d\n", im->sx, im->sy);
}
} else printf("What is this?\n");
}
fclose(fp);
gdImageDestroy(im);
return 0;
}Forums:


답변은 아니나... [
답변은 아니나...
[ code ] 태그보다는
< code > 태그를 쓰면 소스코드가 더 예쁘게 출력되더군요...
^^;
< code > 태그를 쓴건데도 그르네요 ^^; 왜 탭이 안먹히는 걸까요...;;;
댓글 달기