I want to know how to detect if the URL that has been added into a reply field is an image URL.
If it was an image URL then I want to insert an img
tag to the reply, if not then I just want to insert an anchor tag.
use this code :
<?PHP
$URL = 'https://s3.amazonaws.com/wrapbootstrap/live/products/icons/WB0XLB528.jpg';
$isImage = false;
$header = get_headers( $URL , true );
if( preg_match( '!image/*!si' , $header['Content-Type'] ) )
{
$isImage = true;
}
var_dump( $isImage );
?>
if is $isImage
is true, your link is a image
Edit :
with get_headers function get header of url, in header if Content-Type is image/png or image/jpg or image/bmp or etc ( image/* ) url return an image .
$filename = 'insert_url_here';
$contents = file_get_contents($filename);
$finfo=finfo_open(FILEINFO_MIME_TYPE)
if($finfo == "image/png" || $finfo == "image/jpg" || $finfo == "image/bmp"){
// add mimetypes to test for
echo "<img src='" . $filename . "' alt='image'></img>";
}
else{
echo "<a href='" . $filename . "'></img>";
}
Try this:
$url = "http://wewwefwe.com/image.png";
$image_extentions = array('gif', 'jpg', 'jpeg', 'png', 'tiff');
$url = explode('.', $url);
$extention = end($url);
if(in_array($extention, $image_extentions)){
echo "Image";
}else{
echo "Not an Image";
}