LanceT Posted April 17, 2007 Share Posted April 17, 2007 Lets say I want to do a simple image code like <img src = "www.yahoo.com/image.jpg"> And I want to check if the image hosted on Yahoo wasn't a broken image (if it is broken, then I will use different code). What function can I use to do this? Quote Link to comment https://forums.phpfreaks.com/topic/47473-checking-if-image-is-not-broken-from-external-site/ Share on other sites More sharing options...
kathas Posted April 17, 2007 Share Posted April 17, 2007 well if by broken you mean inexistent... then you might try to fopen() it and see what happens... I haven't actually tried that but if fopen() gets redirected search the result for 404 else it should return false that it isn't able to open the requested file... Quote Link to comment https://forums.phpfreaks.com/topic/47473-checking-if-image-is-not-broken-from-external-site/#findComment-231672 Share on other sites More sharing options...
LanceT Posted April 18, 2007 Author Share Posted April 18, 2007 well if by broken you mean inexistent... By broken I mean that if you went to the actual site, you would get a 404 Not Found error. Quote Link to comment https://forums.phpfreaks.com/topic/47473-checking-if-image-is-not-broken-from-external-site/#findComment-232052 Share on other sites More sharing options...
Guest prozente Posted April 19, 2007 Share Posted April 19, 2007 You can connect to a server with a socket using TCP on port 80 and submit a HEAD HTTP request I'll use the telnet program as an example to illustrate the socket connection. Say you wanted to see if the file http://phpfreaks.com/forums/Themes/default/images/icons/profile_sm.gif existed on the server telnet phpfreaks.com 80 HEAD /forums/Themes/default/images/icons/profile_sm.gif HTTP/1.0 Host: phpfreaks.com Now hit enter twice and you'll receive a response HTTP/1.1 200 OK Date: Thu, 19 Apr 2007 02:55:54 GMT Server: Apache/2.2.4 (Unix) DAV/2 PHP/5.2.1 Last-Modified: Tue, 09 Jan 2007 18:46:42 GMT ETag: "070000-230-f8916410" Accept-Ranges: bytes Content-Length: 560 Connection: close Content-Type: image/gif This shows the file does exist, if it didn't you would receive a 404 error for example telnet phpfreaks.com 80 HEAD /forums/Themes/default/images/icons/nonexistant.gif HTTP/1.0 Host: phpfreaks.com Hit enter twice The response you would receive would be HTTP/1.1 404 Not Found Date: Thu, 19 Apr 2007 03:01:56 GMT Server: Apache/2.2.4 (Unix) DAV/2 PHP/5.2.1 Connection: close Content-Type: text/html; charset=iso-8859-1 This shows the file doesn't exist Now to do this in PHP you'd use a socket and craft the HTTP packet and send it to the server. Look at Example 2203 in the PHP manual on the sockets page, it shows a good example. Quote Link to comment https://forums.phpfreaks.com/topic/47473-checking-if-image-is-not-broken-from-external-site/#findComment-232809 Share on other sites More sharing options...
MadTechie Posted April 19, 2007 Share Posted April 19, 2007 what about this, if the image fails it will display an image saying "Error loading $imgname" use <img src = "testimage.php?file=http://www.yahoo.com/image.jpg"> create script below filename = testimage.php <?php function LoadJpeg($imgname) { $im = @imagecreatefromjpeg($imgname); /* Attempt to open */ if (!$im) { /* See if it failed */ $im = imagecreatetruecolor(150, 30); /* Create a black image */ $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); /* Output an errmsg */ imagestring($im, 1, 5, 5, "Error loading $imgname", $tc); } return $im; } header("Content-Type: image/jpeg"); $img = LoadJpeg($_GET['file']); imagejpeg($img); ?> Hope it helps --MadTechie EDIT you may need to use urlencode & urldecode for passing the URL 4:22am here, i'm off to bed Quote Link to comment https://forums.phpfreaks.com/topic/47473-checking-if-image-is-not-broken-from-external-site/#findComment-232822 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.