Reputation: 705
I'm trying to check if url is image, so I did use getheader
; the getheader is working on localhost but not working on hosting server. I also tried to use getimagesize
also it's working on localhost but not working on web hosting server even my php.ini is set to
allow_url_fopen = On
allow_url_include = On
for get_header my code is
$url = "https://hgtvhome.sndimg.com/content/dam/images/hgtv/fullset/2016/2/16/2/Orig-Paul-Schultz_Toybox-Home-kitchen-1.jpg.rend.hgtvcom.616.411.suffix/1455654217545";
$url_headers=get_headers($url, 1);
if(isset($url_headers['Content-Type'])){
$type=strtolower($url_headers['Content-Type']);
$valid_image_type=array();
$valid_image_type['image/png']='';
$valid_image_type['image/jpg']='';
$valid_image_type['image/jpeg']='';
$valid_image_type['image/jpe']='';
$valid_image_type['image/gif']='';
$valid_image_type['image/tif']='';
$valid_image_type['image/tiff']='';
$valid_image_type['image/svg']='';
$valid_image_type['image/ico']='';
$valid_image_type['image/icon']='';
$valid_image_type['image/x-icon']='';
if(isset($valid_image_type[$type])){
echo "Yes it's Images";
}
else
{
echo "no image";
}
}
var_dump of getheader on localhost display
array (size=11)
0 => string 'HTTP/1.0 200 OK' (length=15)
'Content-Type' => string 'image/jpeg' (length=10)
'Server' => string 'Apache' (length=6)
'X-Content-Type-Options' => string 'nosniff' (length=7)
'X-Frame-Options' => string 'SAMEORIGIN' (length=10)
'Content-Length' => string '38885' (length=5)
'Cache-Control' => string 'max-age=2522004' (length=15)
'Expires' => string 'Fri, 03 May 2019 08:55:29 GMT' (length=29)
'Date' => string 'Thu, 04 Apr 2019 04:22:05 GMT' (length=29)
'Connection' => string 'close' (length=5)
'Vary' => string 'User-Agent' (length=10)
But var_dump on web hosting server display
Array
(
[server] => AkamaiGHost, Apache
[content-length] => 0, 2077
[location] => https://www.foodnetwork.com/not-available.html
[cache-control] => max-age=0, max-age=433
[expires] => Thu, 04 Apr 2019 05:16:02 GMT, Thu, 04 Apr 2019 05:23:15 GMT
[date] => Thu, 04 Apr 2019 05:16:02 GMT, Thu, 04 Apr 2019 05:16:02 GMT
[connection] => keep-alive, keep-alive
[vary] => User-Agent, User-Agent, Accept-Encoding
[content-type] => text/html; charset=UTF-8
[access-control-allow-method] => GET
[server-timing] => edge; dur=0, cdn-cache; desc=HIT
[x-akamai-transformed] => 9 - 0 pmb=mRUM,3
[access-control-allow-origin] => *
[content-encoding] => gzip
)
and for getimagesize my code is
if (@getimagesize($url)) {
echo "image exists ";
} else {
echo "image does not exist ";
}
also is working on localhost but not working on web host server the var_dump of @getimagesize($url) is displaying :
bool(false)
Upvotes: 1
Views: 1546
Reputation: 544
@getimagesize function take much time to respond. use below code for quick respond
function image_urlexists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($code == 200); // verifica se recebe "status OK"
}
$url = 'https://www.example.com/static/cs/img/pages/home/security-card-xl.png';
if (image_urlexists($url))
echo "valid Url of image";
else
echo "Not valid url image";
Upvotes: 0
Reputation: 3869
As per you comment, You image url is wrong. Is should be : https://hgtvhome.sndimg.com/content/dam/images/hgtv/fullset/2016/2/16/2/Orig-Paul-Schultz_Toybox-Home-kitchen-1.jpg
You should remove image caching and image versioning at the time of checking.
Best solution to check weather file is image or not
Solution 1
if(@is_array(getimagesize($mediapath))){
$image = true;
} else {
$image = false;
}
Solution 2
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['fupload']['tmp_name']);
$error = !in_array($detectedType, $allowedTypes);
Solution 3
$supported_image = array('gif','jpg','jpeg','png','bmp');
$src_file_name = 'https://hgtvhome.sndimg.com/content/dam/images/hgtv/fullset/2016/2/16/2/Orig-Paul-Schultz_Toybox-Home-kitchen-1.jpg';
$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_image)){
echo "it's image";
}
else {
echo 'not image';
}
Upvotes: 1
Reputation: 1524
You should follow redirect headers (codes 30x)
In this case the image seems 'not available' because the redirect is to a not available page, it seems. Maybe the URL is wrong?
What you should also watch out for: many content providers check at least for a HTTP referrer header or they deny access to images (to make crawling a bit harder). Others check for a previously set cookie. And finally some can use some complex token-based authentication (which I will not get into). But for the first issues and also following redirects, you should use Curl: https://www.php.net/manual/en/book.curl.php
Content-type headers are very often unreliable. I would recommend using the exif image type returned by getimagesize
in result index 2, which you seem to be calling anyway.
So to recap: use Curl to set referrer and follow redirects and maybe propagate cookies from a previous referrer page that you might browse, and once you get the content use getimagesize
to determine type.
Upvotes: 0