강의 컨설팅 트레이닝 무료진단 무료책자 마케팅편지 마케팅정보공유 다이어리 서비스제휴 고객센터

png
작성자 : 99 단국강토
등록날짜 : 2008.12.30 17:01
1,119
You have probably seen avatars and banners like this before in forums and such.
Avatars that changes when you reload the page. And when you look at the avatars extention, you see that it is a .png or whatever.
This can be done using PHP and .htaccess.
PHP is used to create a new image from a random image and .htaccess is used to change the file extention to make it look like a regular .png file.

This is exactly what we're going to do now.
So find a few avatars you want to use, like these:
1.png2.png3.png4.png5.png
(Try to make sure they are 8-bit png, not 24-bit)

Create a new php file called "avatar.php" and enter this code:

<?php
header("Content-type: image/png");

$avatars[] = "avatars/1.png";
$avatars[] = "avatars/2.png";
$avatars[] = "avatars/3.png";
$avatars[] = "avatars/4.png";
$avatars[] = "avatars/5.png";

$avatar = $avatars[mt_rand(0, count($avatars)-1)];

$newavatar = imagecreatefrompng($avatar);
imagepng($newavatar);
imagedestroy($newavatar);

?>


Ok, here's what the code does:

header("Content-type: image/png");
- Makes the php file act like a png image file.

Then we add all the avatars we want into an array called $avatars.
Makes sure you use the correct path to the images.

$avatar = $avatars[mt_rand(0, count($avatars)-1)];
- This code selects a random avatar from the $avatars array and store it in a new variable called $avatar. mt_rand() is a function that returns a random number between 0 and the amount of avatars in the $avatars array in this case. count() returns how many avatars there is in the $avatars array.

$newavatar = imagecreatefrompng($avatar);
- Uses the imagecreatefrompng() function from the GD library to create a new image from a file. The avatar stored in the $avatar array in this case.

imagepng($newavatar);
- Outputs the image to the browser.

imagedestroy($newavatar);
- Deletes the image from the memory.

Now, if you run this script you will just see alot of weid characters, but thats where .htaccess comes in.
Create a new file called ".htaccess" and enter the following code:

RewriteEngine On
RewriteRule avatar.png avatar.php

This code will make avatar.png act like avatar.php.
So if you open avatar.png (wich doesnt even exist) you will get the output from avatar.php. - A random image!

And that's about it.
If you have any troubble, make sure your server have the GD library installed and that you have permission to use .htaccess.

Anyways, here's my result:

[출처] 웹디황용

"쇼핑몰·홈페이지·오픈마켓
블로그·페이스북·이메일 등의 각종 마케팅 글쓰기,
각종 광고, 영업, 판매, 제안서, 전단지
반응율 3배×10배 이상 높이는 마법의 8단계 공식"
자세히보기

Comments

번호 제목 글쓴이 날짜 조회
3285 URL Cache를 사용하여 웹을 더욱 빠르게 13 김영철 01.13 979
3284 php5 99 단국강토 01.02 1054
3283 php 세션css 99 단국강토 12.30 1065
3282 [hatelove님의 JBBS 알고리즘 강좌 9] 13 김영철 01.14 1069
3281 reset , foreach 13 김영철 01.13 1105
열람중 png 99 단국강토 12.30 1120
3279 플래시로 만든 php 함수 사전 13 김영철 01.13 1162
3278 PHP강좌】PHP URL함수 13 김영철 01.13 1166
3277 foreach 와 배열 13 김영철 01.14 1172
3276 주화면의 최신글을 preload로 빠르게 13 김영철 01.13 1183
3275 [php] 내장함수 13 김영철 01.13 1188
3274 태그 허용 함수???? 이제 개념을 바꾸자 13 김영철 01.14 1205
3273 플래쉬 Panels 에 대한 기본개념들 99 단국강토 01.06 1212
3272 foreach 13 김영철 01.13 1213
3271 파일관련함수 13 김영철 01.13 1213
3270 [hatelove님의 JBBS 알고리즘 강좌 7] 13 김영철 01.14 1215
3269 PHP도 객체지향형 프로그램이다..!!(클래스,상속동...) 13 김영철 01.13 1215
3268 PHP입문 - 함수 13 김영철 01.13 1215
3267 플래시에서 pc cam 영상보여주기 99 단국강토 02.16 1218
3266 php기본함수 정리!! 13 김영철 01.13 1219
3265 시스템콜인 open 계열 함수와 스트림기반의 fopen 계열.. 13 김영철 01.13 1221
3264 소스를 간편하게 만들어 주는 with문 99 단국강토 02.10 1224
3263 객체 정의하기[이론,예제] 99 단국강토 01.29 1229
3262 php 파일 업, 다운로드 13 김영철 01.13 1231
3261 역인덱스 게시판 | 13 김영철 01.14 1241
3260 파일업로드 썸네일 제작 class 13 김영철 01.13 1244
3259 초보자용 이것저것 몇가지 팁 13 김영철 01.14 1246
3258 간단 날짜계산 99 단국강토 02.16 1248
3257 window 객체 M 최고의하루 12.20 1249
3256 디렉토리 폴더 모든파일 표시 [php] 13 김영철 01.14 1250
마케팅
특별 마케팅자료
다운로드 마케팅자료
창업,경영
기획,카피,상품전략
동기부여,성취