Jump to content

check image size in string


drisate

Recommended Posts

hey guys i need a code that finds every images inside a posted message and that checks if the size is bigger then 430 px if it is put 430.

 

I guess we would need to search for a paterne then extract the info and replace if needed ...

 

There can be only 2 paternes

 

1. $message='blablablabla <img src="http://..." border"0" alt="" />blablablabla blablablabla';

and

2. $message='blablablabla <img src="http://..." border"0" alt="" width="">blablablabla blablablabla';

 

For paterne 1 we need to get the image size from the image it self using the image adress then add a width inside the string.

For paterne 2 we need to check the width and change the value if needed

 

I have abselutly no idea how to do that ...

Link to comment
Share on other sites

$message='blablablabla <img src="somepic.jpg" border"0" alt="" />blablablabla blablablabla';
preg_match('/(<img.*?src="(.*?)".*?>)/',$message,$match);
$picinfo = getimagesize($match[2]);
$picinfo[0] = ($picinfo[0] > 430)? 430 : $picinfo[0];
$replacement =  'img src="'.$match[2].'" width="'.$picinfo[0].'" height="'.$picinfo[1].'" border="0" alt="" /';
$message = preg_replace($match[1], $replacement, $message);

 

This code will pull the image tag from the message, find out the width and height of the image, and create a new image tag with width no more than 430. But width for the replacement image tag is based off of the getimagesize of the pic, not some width in the original $message string.  Code is going to have to be changed up a bit if the width in the original $message has to be preserved (like, it's based off user input or something). 

 

Like for instance, user put in 100 as width.  You want max of 430, but getimagesize says it's 200.  That script will set it to 200, not 100.  You didn't specify whether you wanted it to be based off original image or actual pic size (hence the question in the prev post).

 

Also, code works for either of those formats. 

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.