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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.