>  기사  >  백엔드 개발  >  PHP 이미지 처리 기능 예제 및 PHP 확인 코드

PHP 이미지 처리 기능 예제 및 PHP 확인 코드

WBOY
WBOY원래의
2016-07-25 08:51:45783검색
  1. //캔버스 준비
  2. $im = imagecreatetruecolor(500, 300)
  3. //페인트 준비

  4. $black = imagecolorallocate($im, 0, 0, 0)

  5. $white = imagecolorallocate($im, 255, 255, 255);

  6. //배경이 검은색으로 채워집니다

  7. imagefill($im,0,0, $black)
  8. < ;p>//직사각형을 그리고 흰색으로 채웁니다
  9. imagefilledellipse($im, 258, 151, 200, 200, $white)
  10. //브라우저에 출력하거나 저장합니다
  11. header(" content-type:image/png");
  12. //이미지 출력
  13. imagepng($im)

  14. //캔버스 닫기

  15. imagedestroy; ($im) ;
  16. ?>

코드 복사

php 이미지 처리 기능 1. 수학 함수 2. 이미지 처리 기능

수학 함수: 1,최대(); 2,분(); 3. mt_rand(); 무작위로 숫자 선택

  1. echomt_rand(1,5)
  2. ?>
코드 복사

mt_rand는 임의의 값을 취합니다.

  1. //배열에서 값을 무작위로 선택
  2. $arr = array( " a","b","c","d","e");

  3. $rkey = mt_rand(0,count($arr)-1); < ; /p>

  4. echo $arr[$rkey];

  5. ?>

코드 복사

4.ceil();천장 5.바닥(); 6.라운드();

  1. echo ceil(2.4);//3
  2. echo Floor(2.4); //2
  3. 에코 라운드(2.4);//2
  4. 에코 라운드(2.6);//3
  5. ?>

코드 복사

6.pi() //π는 pi를 사용합니다.

  1. echo(pi())
  2. echo M_PI;
코드 복사
이미지 처리 기능 사용 시나리오 1.인증코드 2.줌 3.자르기 4. 워터마크

PHP에서 이미지를 착용하는 5단계 1. 캔버스 준비 2. 페인트 준비 3. 캔버스에 이미지나 텍스트를 그립니다. 4. 최종 이미지 또는 Caocun 최종 이미지를 출력합니다. 5. 캔버스 리소스 출시

예:

  1. //1. 캔버스 준비
  2. $im = imagecreatetruecolor(500,300)
  3. //2. 페인트 준비
  4. $black = imagecolorallocate($im, 0, 0, 0)
  5. $white = imagecolorallocate($im, 255, 255, 255)
  6. //3. 캔버스에 이미지나 텍스트를 그립니다.

  7. //배경이 채워지지 않은 경우 기본값은 검정색입니다
  8. imageellipse($im,258,151,200,200,$white); p>
  9. //4. 최종 이미지를 출력하거나

  10. header("content-type:image/png")
  11. imagepng($im); //5. 캔버스 리소스 해제
  12. imagedestroy($im)
  13. ?>

코드 복사
이미지 그리기: 이미지 채우기(); imagesetpixel();//픽셀 그리기 imageline();//선 그리기 imageRectangle();//직사각형을 그립니다. imagepolygon();//다각형 그리기 imageellipse();//타원 그리기 imageare(); 호를 그립니다. imagechar();//가로로 문자 그리기 imagestring();//문자열을 가로로 한줄 그리기

예:

  1. //선 그리기

  2. //1 캔버스를 준비합니다
  3. $im. = imagecreatetruecolor(500,300);
  4. //2. 페인트 준비
  5. $black = imagecolorallocate($im, 0, 0, 0)
  6. $white = imagecolorallocate($im, 255, 255, 255) ;

  7. //3. 캔버스에 이미지나 텍스트를 그립니다

  8. //배경이 채워지지 않은 경우 기본값은 검정색입니다
  9. imageline($im,0, 0,500,300,$ 흰색);
  10. imageline($im,0,300,500,0,$white)
  11. imageline($im,0,150,500,150,$white)
  12. imageline($im,250,0,250,300,$white ); < ;/p>
  13. //4. 최종 이미지를 출력하거나

  14. header("content-type:image/png")
  15. imagepng($im );
  16. //5. 캔버스 리소스 해제
  17. imagedestroy($im)
  18. ?>/p>
코드 복사
🎜>

예:

  1. //인터페론 추가

  2. //1 캔버스 준비
  3. $ im. = imagecreatetruecolor(500,300);
  4. //2. 페인트 준비
  5. $black = imagecolorallocate($im, 0, 0, 0)
  6. $white = imagecolorallocate($im, 255, 255, 255 ) ;

  7. //3. 캔버스에 이미지나 텍스트를 그립니다.

  8. //임의의 포인트 생성
  9. ($i=0; $i < 1000; $i) ) {
  10. imagesetpixel($im,mt_rand(0,500),mt_rand(0,300),$white)

  11. }

  12. / /임의의 선 생성

  13. for ($j=0; $j < 100; $j ) {

  14. imageline($im, mt_rand(0,500), mt_rand (0,300), mt_rand(0,500), mt_rand(0,300), $white);
  15. }//4. 최종 이미지 출력 또는 최종 이미지 저장
  16. header("content-type:image/png"); imagepng($im);//5. 캔버스 리소스 해제
  17. imagedestroy($im)
  18. ?>/p>
예:

    //직사각형 그리기:

  1. //1 캔버스를 준비합니다
  2. $ im = imagecreatetruecolor(500,300);
  3. //2. 페인트 준비
  4. $black = imagecolorallocate($im, 0, 0, 0)
  5. $white = imagecolorallocate($im, 255, 255, 255 );

  6. //3. 캔버스에 이미지나 텍스트를 그립니다.

  7. imageRectangle($im, 20, 20, 480, 280, $white);//
  8. imagefilled직사각형($im, 20, 20, 480, 280, $white);//Filling

  9. //4. 최종 이미지 출력 또는 최종 이미지 저장

  10. header( "content -type:image/png");
  11. imagepng($im)
  12. //5. 캔버스 리소스 해제
  13. imagedestroy($im)?>
  14. 코드 복사
예:

//imagepolygon 그리기 다각형_삼각형 그리기

    //1 캔버스 준비
  1. $im = imagecreatetruecolor(500,300);
  2. //2. 페인트 준비
  3. $black = imagecolorallocate($im, 0, 0, 0)
  4. $white = imagecolorallocate($im, 255, 255, 255);

  5. //3. 캔버스에 이미지나 텍스트를 그립니다.

  6. $arr = array(
  7. 250,20,
  8. 60,240,
  9. 440,240
  10. );
  11. imagepolygon($im, $arr, 3, $white)

  12. //4. 최종 이미지를 출력하거나 저장하세요

  13. header("content-type:image/png");
  14. imagepng($im)
  15. //5. 캔버스 리소스 해제
  16. imagedestroy($im)?
  17. 코드 복사
3D 파이 차트 그리기 예시

//1. 캔버스 준비

    $im = imagecreatetruecolor(500,300)
  1. //2. 페인트 준비
  2. $black = imagecolorallocate($im, 0, 0, 0)
  3. $red = imagecolorallocate($im, 255, 0, 0); imagecolorallocate($im, 255, 100, 100);
  4. $green = imagecolorallocate($im, 0, 255, 0)
  5. $blue = imagecolorallocate($im, 0, 0, 255); >$gray = imagecolorallocate($im, 200, 200, 200);
  6. $white = imagecolorallocate($im, 255, 255, 255)

  7. //3. 캔버스에 이미지나 텍스트를 그립니다.

  8. for ($i=0; $i < 10; $i ) {
  9. imagefilledarc($im, 250, 150 $i, 200, 200, 0, 70, $ grey,IMG_ARC_PIE);
  10. imagefilledarc($im, 250, 150 $i, 200, 200, 70, 190, $grayred,IMG_ARC_PIE)
  11. imagefilledarc($im, 250, 150 $i, 200, 200 , 190, 270, $green,IMG_ARC_PIE)
  12. imagefilledarc($im, 250, 150 $i, 200, 200, 270, 360, $blue,IMG_ARC_PIE)

  13. < p> ;}
  14. imagefilledarc($im, 250, 150, 200, 200, 0, 70, $white,IMG_ARC_PIE)
  15. imagefilledarc($im, 250, 150, 200, 200, 70, 190, $ 빨간색,IMG_ARC_PIE);
  16. imagefilledarc($im, 250, 150, 200, 200, 190, 270, $green,IMG_ARC_PIE)
  17. imagefilledarc($im, 250, 150, 200, 200, 270, 360 , $blue,IMG_ARC_PIE);

  18. //4. 최종 이미지를 출력하거나

  19. header("content-type:image/png"); >imagepng( $im);
  20. //5. 캔버스 리소스 해제
  21. imagedestroy($im)
  22. ?>/p>
  23. 코드
  24. 예:

    1. //텍스트 쓰기:

    2. //1 캔버스를 준비합니다
    3. $. im = imagecreatetruecolor(500,300);
    4. //2. 페인트 준비
    5. $black = imagecolorallocate($im, 0, 0, 0)
    6. $red = imagecolorallocate($im, 255, 0, 0 );
    7. $grayred = imagecolorallocate($im, 255, 100, 100);
    8. $green = imagecolorallocate($im, 0, 255, 0)
    9. $blue = imagecolorallocate($im, 0 , 0, 255);
    10. $gray = imagecolorallocate($im, 200, 200, 200);
    11. $white = imagecolorallocate($im, 255, 255, 255);

      //3. 캔버스에 이미지나 텍스트를 그립니다.

    12. $str= "PHP는 매우 좋습니다."

    13. imagestring ( $im, 5, 260, 160, $str, $green);

    14. //4. 최종 이미지 출력 또는 최종 이미지 저장
    15. header("content-type:image/png"); > imagepng($im);
    16. //5. 캔버스 리소스 해제
    17. imagedestroy($im)
    18. ?>/p>
    코드
    예:

    1. //단일 문자 쓰기:

    2. //1 캔버스를 준비합니다. 🎜> $im = imagecreatetruecolor(500,300);
    3. //2. 페인트 준비
    4. $black = imagecolorallocate($im, 0, 0, 0)
    5. $red = imagecolorallocate($im, 255, 0, 0);
    6. $grayred = imagecolorallocate($im, 255, 100, 100)
    7. $green = imagecolorallocate($im, 0, 255, 0)
    8. $blue = imagecolorallocate($ im, 0, 0, 255);
    9. $gray = imagecolorallocate($im, 200, 200, 200)
    10. $white = imagecolorallocate($im, 255, 255, 255);
    11. //3. 캔버스에 이미지나 텍스트를 그립니다.

    12. $str= "P"

    13. imagechar( $ im, 5, 260, 160, $str, $green);

    14. //4. 최종 이미지를 출력하거나
    15. header("content-type:image/png"); >imagepng( $im);
    16. //5. 캔버스 리소스 해제
    17. imagedestroy($im)
    18. ?>/p>
    19. 코드
    예,

    //그림에 쓰기

      //1 캔버스를 준비합니다 $im = imagecreatetruecolor(500,300);
    1. //2. 페인트 준비
    2. $black = imagecolorallocate($im, 0, 0, 0)
    3. $red = imagecolorallocate($im, 255, 0 , 0);
    4. $grayred = imagecolorallocate($im, 255, 100, 100);
    5. $green = imagecolorallocate($im, 0, 255, 0)
    6. $blue = imagecolorallocate($im , 0, 255);
    7. $gray = imagecolorallocate($im, 200, 200, 200)
    8. $white = imagecolorallocate($im, 255, 255, 255); 🎜 >

      //3. 캔버스에 이미지나 텍스트를 그립니다.

    9. $str= "junzaivip"

    10. $file = "E:/PHP/fonts/SIMYOU .TTF";
    11. // $file = "fonts/SIMYOU.TTF";

    12. imagettftext($im, 50, 0, 100, 200, $green, $file , $str);

    13. //4. 최종 이미지를 출력하거나 최종 이미지를 저장합니다

    14. header("content-type:image/png")
    15. imagepng ($ im);//5. 캔버스 리소스 해제
    16. ?>/p>
    17. 코드 복사
    18. PHP 인증코드 디자인

    //캔버스 준비

    $im = imagecreatetruecolor(100,50)
    //페인트 준비
    $black = imagecolorallocate($im, 0, 0, 0)
      $gray = imagecolorallocate($im, 200, 200, 200)

    1. //배경 채우기

    2. imagefill($im, 0, 0, $gray)

    3. //텍스트 좌표

    4. $x = (100- 4 *20)/2 6;
    5. $y = (50-20)/2 20

    6. //캔버스에 이미지나 텍스트 그리기

    7. //3개의 배열 연결

    8. $strarr = array_merge(range(1, 9),range(a, z),range(A, Z))

    9. < p>//배열 섞기
    10. shuffle($strarr);

    11. //array_slice: 배열의 처음 몇 자리를 가져옵니다

    12. //배열을 문자열을 사용하고 첫 번째 변수를 구분 기호로 사용합니다.
    13. $str = Join('',array_slice($strarr, 0,4))

    14. $file = "E :/PHP/fonts/msyh.ttf";

    15. // $file = "fonts/msyh.ttf";

    16. imagettftext($im, 20, 0, $x , $y, $black, $file, $str);

    17. //최종 이미지 출력 또는 최종 이미지 저장

    18. header("content-type:image/png ");
    19. imagepng($im);
    20. //캔버스 리소스 해제
    21. imagedestroy($im);
    22. ?>
    23. 코드 복사
    24. PHP 인증 코드 디자인: 여기에는 index.php 및 reg.php라는 두 페이지가 포함됩니다. 설명:

      이 인증코드 버전은 인증 이미지의 동적 획득만 구현합니다. 프론트 엔드 등록 페이지의 인증 코드와 생성된 이미지의 인증 코드를 비교하세요 인증코드는 숫자, 소문자, 대문자로 랜덤하게 구성됩니다

      index.php//사용자 등록

      1. reg >
      2. 사용자 등록 페이지

        hr>
      3. 이름:
        비밀번호:<입력 유형 = "password" name="password" id = "">
        인증 코드:
        <입력 유형 = "제출" 값 = "제출" 이름 = "제출"><입력 유형 = "재설정" value = "재설정" name = "제출"

      4. 코드 복사
      5. reg.php//인증코드가 맞는지 확인하는데 사용됩니다

      session_start()

      // echo $_POST['username'];
        // echo $_POST['password'];
      1. $code = strtolower($_POST['vcode'])

      2. // echo $code; ;/p>

      3. // echo "

        " </li>
        <li>// print_r($_SESSION)// echo "
        "; vstr = strtolower($_SESSION['vstr']);

      4. if ($code==$vstr) {

      5. //페이지 이동 실현
      6. echo " < script>location='http://www.xingzuo51.com'";
      7. }else{
      8. echo "