0

I'm using the following code to query a database for image values up to 25 thumbnail images. I am relying on CURL to return a 200 code to find out if the image exists. If not I want to end the thumbnails.

My problem is that it is fetching the images but leaving blank thumbnails if not all 25 images are found. I want to NOT forma the

<?
$image = "<div class='wrap_img_small'><br>";
$ListingRid = $row['matrix_unique_id'];
$img_cnt = 1;
for ($c=1;$c<25;$c++) {
    $c_ext = $c;
    $ch = curl_init("http://www.domain.com/feeds/fort/rets_images/{$ListingRid}_{$c_ext}.jpg");
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($retcode == '200')
        $image .= "<a href=http://www.domain.com/feeds/fort/rets_images/{$ListingRid}_{$c_ext}.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea,trigger:click\" title=\"\"><img src=http://www.domain.com/feeds/fort/rets_images/{$ListingRid}_{$c_ext}.jpg title='' width='100' height='75' border='0' /></a>";
    else
    $c=31;

    $img_cnt++;
}
?>
4
  • 1
    HTTP Code is of type int so you can check against 200 instead of '200'. And why do you set your counter $c to 31 if one image isn't found? Commented Oct 7, 2014 at 11:57
  • To end the for loop when an image isn't found just add a break; statement after your else clause. Commented Oct 7, 2014 at 11:59
  • Thanks guys, I attempted both and it still displays thumbnails where an image does not exist. c31 is just something past the 25 count and was supposed to end forming of img thumbs Commented Oct 7, 2014 at 12:01
  • 1
    Bonus points: Cache your results. Commented Oct 7, 2014 at 12:24

1 Answer 1

2

Your problem is that myagentsbuddy.com does not return a 404 when the image is not found. I checked that with one of your broken images and curl -I, and it gave me a 200. You are better off checking its content-type, and see whether it is an image/* or text/html.

Here is what I got for an existing image:

$ curl -I http://www.myagentsbuddy.com/feeds/fort/rets_images/2332012_8.jpg
HTTP/1.1 200 OK
Date: Tue, 07 Oct 2014 12:20:16 GMT
Server: Apache
Last-Modified: Fri, 21 Feb 2014 18:03:02 GMT
ETag: "4e75339-647c-6ece6180"
Accept-Ranges: bytes
Content-Length: 25724
X-Powered-By: PleskLin
Connection: close
Content-Type: image/jpeg

And for a broken image:

$ curl -I http://www.myagentsbuddy.com/feeds/fort/rets_images/2332012_9.jpg
HTTP/1.1 200 OK
Date: Tue, 07 Oct 2014 12:18:42 GMT
Server: Apache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Pingback: http://www.myagentsbuddy.com/xmlrpc.php
Last-Modified: Tue, 07 Oct 2014 12:18:42 GMT
X-Powered-By: PleskLin
Connection: close
Content-Type: text/html; charset=UTF-8

See the last lines on both. There is the difference.

So you need:

$image = "<div class='wrap_img_small'><br>";
$ListingRid = $row['matrix_unique_id'];
$img_cnt = 1;
for ($c=1;$c<25;$c++) {
    $c_ext = $c;
    $ch = curl_init("http://www.myagentsbuddy.com/feeds/fort/rets_images/{$ListingRid}_{$c_ext}.jpg");
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $contenttype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    curl_close($ch);
    if (strpos($contenttype, 'image') === false) break;
    $image .= "<a href=http://www.myagentsbuddy.com/feeds/fort/rets_images/{$ListingRid}_{$c_ext}.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea,trigger:click\" title=\"\"><img src=http://www.myagentsbuddy.com/feeds/fort/rets_images/{$ListingRid}_{$c_ext}.jpg title='' width='100' height='75' border='0' /></a>";

    $img_cnt++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @wouter How would I go about checking for text/html instead?
@RoccoTheTaco Just updated the answer with the actual solution :)
Weird, I tried replacing the if ($retcode == '200') but am getting a syntax error? Can you post the entire snippet adjusted please?
Okay, I see what I was doing wrong. Thanks so much for the help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.