Rocco The Taco
Rocco The Taco

Reputation: 3777

PHP with CURL fetch to dynamically display images

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++;
}
?>

Upvotes: 0

Views: 275

Answers (1)

Wouter Thielen
Wouter Thielen

Reputation: 1036

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

Upvotes: 2

Related Questions