2012-08-15 4 views
0

PHP로 작성된 이미지 다운로드 서비스에 테스트 케이스를 작성하고 있습니다. 우리는 phpunit을 사용하고 있습니다. 검색된 바이너리 데이터가 이미지인지 어떻게 확인할 수 있습니까?Phpunit 이미지 다운로드 테스트

+0

있는 URL 이미지가 있는지 확인하기 위해 [가장 좋은 방법의 중복 가능성 PHP] (http://stackoverflow.com/questions/676949/best-way-to-determine-if-a-url-is-an-image-in-php) 및 http://stackoverflow.com/questions/ 10662915/check-a-file-is-an-image-or-not 및 http://stackoverflow.com/questions/6391916/is-it-important-to-verify-that-the-uploaded-file- –

+0

'getimagesize()'는 일부 이미지 형식의 일반 이름입니다. 어떤 것을 지원해야합니까? –

답변

1

exif_imagetype (manual 참조)을 사용하는 것이 좋지만 로컬 디스크에 파일이 있어야합니다. 당신은 몇 가지 매직 넘버를 하드 코딩 괜찮다면, 당신은 직접 이미지 유형을 확인 다음 예에서 testFetchWithoutSaving를 볼 수 있습니다 :

class ImageTest extends PHPUnit_Framework_TestCase 
{ 

/** 
* @see http://stackoverflow.com/a/676975/841830 
*/ 
public function testFetchWithoutSaving(){ 
    $s=file_get_contents("https://www.google.com/images/srpr/logo3w.png"); 
    $this->assertEquals("\x89PNG\x0d\x0a\x1a\x0a",substr($s,0,8)); 

    $s=file_get_contents("https://www.google.com/"); 
    $this->assertEquals("\x89PNG\x0d\x0a\x1a\x0a",substr($s,0,8),"Fails: first 8 bytes are actually '<!doctyp'"); 
    } 

/** 
* @see http://php.net/manual/en/function.exif-imagetype.php 
*/ 
public function testFetchWithTempFile(){ 
    $s=file_get_contents("https://www.google.com/images/srpr/logo3w.png"); 
    $tempFilename="/tmp/phpunit.testImage.testFetchWithTempFile"; 
    file_put_contents($tempFilename,$s); 
    $type=exif_imagetype($tempFilename); 
    unlink($tempFilename); 
    $this->assertTrue($type!==false); //Any recognized image type 
    $this->assertEquals(IMAGETYPE_PNG,$type); //A specific image type 
    } 

} 
관련 문제