{"flag":true,"single":true,"pageTitle":"PHP Code to Check if Image Exists or not : Ensure Your Website Displays Images Correctly","post":{"id":34,"user_id":"1","slug":"php-code-to-check-if-image-exists-or-not-ensure-your-website-displays-images-correctly-bcsx","title":"PHP Code to Check if Image Exists or not : Ensure Your Website Displays Images Correctly","body":"<p>Check if given url is image or not , also check if link exist or not<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n  function is_image_exist($url){\r\n      $ch = curl_init($url);\r\n      curl_setopt($ch, CURLOPT_NOBODY, true);\r\n      curl_exec($ch);\r\n      $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);\r\n      curl_close($ch);\r\n\r\n      if ($http_status == 200) {\r\n        $image_info = getimagesize($url);\r\n        if ($image_info !== false) {\r\n          return true;\r\n        }else{\r\n          return false;\r\n        }\r\n      } else {\r\n        return false;\r\n      }\r\n\r\n  }                          \r\n                            \r\n$image_url = 'https:\/\/cdn-static.roomster.com\/pics\/Original\/L-27092602-c78c872fa5074d9ea8f62a33108679e5.jpg';\r\nif(is_image_exist($image_url)){ ?&gt;\r\n  &lt;img class=\"img img-fluid\" src=\"&lt;?php echo $image_url;?&gt;\"&gt;\r\n&lt;?php }else{ ?&gt;\r\n  &lt;img class=\"img img-fluid\" src=\"https:\/\/dummyimage.com\/200x200\/000\/fff&amp;text=404\"&gt;\r\n\r\n&lt;?php } ?&gt;<\/code><\/pre>","category_id":"1","is_private":"0","created_at":"2023-03-21T00:57:58.000000Z","updated_at":"2023-03-21T00:57:58.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Check if given url is image or not , also check if link exist or not &lt;?php   function is_image_exist($url){       $ch = curl_init($url);  - PHP Code to Check if Image Exists or not : Ensure Your Website Displays Images Correctly (Updated: March 21, 2023) - Read more about PHP Code to Check if Image Exists or not : Ensure Your Website Displays Images Correctly at my programming site [SITE]","categories":[]}