PHP에서 원격 이미지의 유효성을 체크하는 방법

2018. 4. 12. 11:44PHP

반응형
< ?

function check_image($url) 
{
function check_image($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$output = curl_exec($ch);
curl_close($ch);

$headers = array();
foreach(explode("\n",$output) as $line){
$parts = explode(':' ,$line);
if(count($parts) == 2){
$headers[trim($parts[0])] = trim($parts[1]);
}

}

return isset($headers["Content-Type"]) && strpos($headers['Content-Type'], 'image/') === 0;
}
} ? >


< ? if (check_image($image_url) == 1) { // 이미지 있음 } else { // 이미지 없음 } ?>


curl 이 설치되어 있지 않으면 에러가 발생한다.

아래참고. (우분투)



1 확인[편집]

php -m | grep curl
dpkg --get-selections | grep php | grep curl
root@zetawiki:~# php -m | grep curl
root@zetawiki:~# dpkg --get-selections | grep php | grep curl
root@zetawiki:~#

2 설치[편집]

# ubuntu14, php5
apt-get install php5-curl
# ubuntu16, php7
apt-get install php-curl
root@zetawiki:~# apt-get install php-curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libcurl3 php7.0-curl
The following NEW packages will be installed:
  libcurl3 php-curl php7.0-curl
0 upgraded, 3 newly installed, 0 to remove and 37 not upgraded.
Need to get 215 kB of archives.
After this operation, 692 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://kr.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libcurl3 amd64 7.47.0-1ubuntu2.2 [186 kB]
Get:2 http://kr.archive.ubuntu.com/ubuntu xenial-updates/main amd64 php7.0-curl amd64 7.0.15-0ubuntu0.16.04.4 [27.4 kB]
Get:3 http://kr.archive.ubuntu.com/ubuntu xenial/main amd64 php-curl all 1:7.0+35ubuntu6 [1,930 B]
... (생략)
Setting up php-curl (1:7.0+35ubuntu6) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for php7.0-fpm (7.0.15-0ubuntu0.16.04.4) ...

3 확인 2[편집]

php -m | grep curl
dpkg --get-selections | grep php | grep curl
root@zetawiki:~# php -m | grep curl
curl
root@zetawiki:~# dpkg --get-selections | grep php | grep curl
php-curl					install
php7.0-curl					install


반응형