Best Way to Determine If a Url Is an Image in PHP

best way to determine if a URL is an image in PHP

You could use an HTTP HEAD request and check the content-type. This might be a good compromise. It can be done using PHP Streams. Wez Furlong has an article that shows how to use this approach to send post requests, but it can be easily adapted to send HEAD requests instead. You can retrieve the headers from an http response using stream_get_meta_data().

Of course this isn't really 100%. Some servers send incorrect headers. It will however handle cases where images are delivered through a script and the correct file extension isn't available. The only way to be really certain is to actually retrieve the image - either all of it, or the first few bytes, as suggested by thomasrutter.

how to check if a url is an image url with php?

If you want to be absolutely sure, and your PHP is enabled for remote connections, you can just use

getimagesize('url');

If it returns an array, it is an image type recognized by PHP, even if the image extension is not in the url (per your second link). You have to keep in mind that this method will make a remote connection for each request, so perhaps cache urls that you already probed in a database to lower connections.

PHP: Check if link is image and check if exists

Try

$headers = get_headers('https://media.giphy.com/media/BvvBz8BnRqZOg/giphy.gif', 1);
if (strpos($headers['Content-Type'], 'image/') !== false) {
echo "Work";
} else {
echo "Not Image";
}

You only need to check the headers to see it it is an image, $headers['Content-Type'] in the example is 'image/gif' which is caught by the if statement. For reference check out get headers in the docs http://php.net/manual/en/function.getallheaders.php

Check URL is an image or not using PHP

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';
}

Check if URL exist, and if it's an image

You can never be 100% sure but i would at least check for:

  1. Content Headers rather than extension (works even if the image is being served dynamically with the extension of ".php" or anything else)
  2. Check the content length header to make sure it's greater than zero and the server is not sending me a soft 404
  3. Finally check if the final image is a redirect. (incase of 404 page or a default image file)

    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $content_length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    $content_redirect = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT );

    $imageTypes = array('image/png','image/jpeg','image/gif');

    if(in_array($content_type,$imageTypes) && $content_redirect == 0 && $content_length >0){
    // is a vald image
    }

to stop curl from downloading the whole image file set CURLOPT_NOBODY to true.

curl_setopt($ch, CURLOPT_NOBODY, true);

How to check Url Image is exist or not in php

This function will solve your problem :

function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}

Just pass the image url in this function and it will return yes if image exists otherwise false

How to check whether an image is present or not at a specific URL using PHP?

I use this little guy:

function remoteFileExists($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec($ch)) return true;
else return false;
}

Use like:

if (remoteFileExists('https://www.google.com/images/srpr/logo11w.png')){
echo 'Yay! Photo is there.';
} else {
echo 'Photo no home.';
}


Related Topics



Leave a reply



Submit