Question

Please consider the following code:

$imagePath = "https://s22.postimg.org/3k3o9ly8t/testigo.jpg";
$imagedata = get that image data through curl and store in this variable;
echo strlen($imagedata); // outputs 4699

if(strlen($imagedata) == 4699 ) {
    echo "length is 4699";
}

The above if-condition never becomes true even though the strlen value is 4600. It seems very strange; am I missing anything? I've already tried mb_strlen, but to no avail.

Update: It seems to work for certain images, but not for the following image.


Code:

$strImageURL = "https://s22.postimg.org/3k3o9ly8t/testigo.jpg";

$strImageData = getData($strImageURL);

echo "<br />" . strlen($strImageData);

if(strlen($strImageData) === 4699) {
    echo "true";
}

function getData($strSubmitURL)
{

    $strData = null;

    $ch = curl_init();

    //return parameter
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 140);

    //site name
    curl_setopt($ch, CURLOPT_URL,$strSubmitURL);


    // don' verify ssl host
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    $strData = curl_exec ($ch);

    if (!$strData) {
        //die ("cURL error: " . curl_error ($ch) . "\n");
        return '';
    }
    curl_close ($ch);

    unset($ch);

    return $strData;
}
Was it helpful?

Solution

This works for me:

$url = "http://static.bbci.co.uk/frameworks/barlesque/2.51.4/orb/4/img/bbc-blocks-dark.png";
$imagedata = file_get_contents($url);
//echo strlen($imagedata); // outputs 1020

if(strlen($imagedata) == 1020 ) {

    echo "length is 1020";

}

And as further troubleshooting, I would try a var_dump(get_defined_vars()); at the end of your code and inside the if statement to see whats going on.

//Edit/Update: Using your url, and also putting in a var dump twice:

$url = "http://s22.postimg.org/3k3o9ly8t/testigo.jpg";
$imagedata = file_get_contents($url);
$strlen =  strlen($imagedata); // outputs 4669
var_dump($strlen);

if($strlen == 4669 ) {

    echo "length is 4669 \n";
    var_dump($strlen);
}

returns:

PhpMate running PHP 5.3.15 with (/usr/bin/php)
 >>> untitled

int(4669)
length is 4669 
int(4669)

OTHER TIPS

strlen() is used to count the bytes that a string equates an one character does not necessarily equal 1 byte in UTF-8

Another issue could be type-casting since PHP has such loose rules about this. Would this work for you?

if((int)strlen($imagedata) == 4600 ) {

    echo "length is 4600";

}

or

if(strlen($imagedata) == '4600' ) {

    echo "length is 4600";

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top