Jump to content

Check image size (kb) in posts and replace with url


eevan79

Recommended Posts

How to check size of image in post (text) and replace it with url?

Example:

"click on this image:

[img ]http://link_to_image.jpg[/img ]

I need to check if img tags exists in post and check size in kb of that image. If size >=200kb (example) than replace tags with: [url ]http://link_to_image.jpg[/url ].

 

This is code that I use to replace all [ img] tags with [ url]

$text = str_replace ("[img ]","[url ]",$text );
$text = str_replace ("[/img ]","[/url ]",$text );

This code doesnt check image size, but replace all images with urls.

I found function that check file size (remote)

function remote_filesize($url, $user = "", $pw = "")
{
    ob_start();
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);

    if(!empty($user) && !empty($pw))
    {
        $headers = array('Authorization: Basic ' .  base64_encode("$user:$pw"));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    $ok = curl_exec($ch);
    curl_close($ch);
    $head = ob_get_contents();
    ob_end_clean();

    $regex = '/Content-Length:\s([0-9].+?)\s/';
    $count = preg_match($regex, $head, $matches);

    return isset($matches[1]) ? $matches[1] : "unknown";
}
echo remote_filesize($text,'','');

 

How to use this function to check images in posts? First I need to find url between [ img] [/ img] tags and than use this function to check. If file size is >= 200 replace this [ img] tags with

Why not just thumbnail images, and link from the thumbnail? Then the user doesn't have to open the full resolution image to decide if he wants to open the full resolution image.

Not bad idea, but it also need to check if there is image in post and then thumbnail that image.

 

Anyway, I think Its better to replace image with link. I dont want to create thumbs if there is small images.

 

Also, I have tested getimagesize and its very slow if there is more than one image.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.