Saturday, August 29, 2009

PHP scale_to function to scale a series of values such as an image or video height and width

Need to scale a series of values based on one of the values in proportion to a master value. This is good if you have an image or video that you want to display in proportion, but at a larger or smaller scale.

Parameter one is an array of numeric values such as: $dims = array(100,75);
The first index may be width and the second the height of an image, but this function will accept an unlimited amount of indexes.

Parameter two is the master value to use when creating the scale ratio: $master_value = 1000;

Parameter three is the index of of the array $dims that the function will use to create the master ratio: $master_value_index = 0;

scale_to(array(100,75),1000,0); would return an array with the values (1000,750). Since the ratio of the specified master index is 10 to 1, 75, and any other values besides the master value are all multiplied by 10.

Enjoy!

function scale_to($dims,$master_value,$master_value_index)
{

if(!is_array($dims))return false;
if(!is_int($master_value))return false;
if(!is_int($master_value_index)||
$master_value_index<0||
$master_value_index>count($dims)-1)return false;

$ratio = $master_value / $dims[$master_value_index];

foreach($dims as $k=>$v)
{

$new_dims[$k] = $master_value_index == $k ?$master_value : round($dims[$k] * $ratio);

}

return $new_dims;

}

Wednesday, April 22, 2009

PHP - How to remove a HTML element property value

It's pretty simple but I know regular expressions can be tough sometimes. The code below will remove the width property regardless if it's value is enclosed with single or double quotes.

$str = preg_replace('/width\s*=\s*["\'].*["\']/','',$str);
Let's put it into a function and change width to a variable so that this code is more flexible:

function remove_property($property,$str){
return preg_replace('/'.$property.'\s*=\s*["\'].*["\']/','',$str);
}

$str = '<img src="image.jpg" width="200" height="230">';
$stripped_string = remove_property('width',$str);
Enjoy!

PHP - extracting tag values from HTML elements

Hey all!

I needed to grab all images out of a text string and then get image property values from that string. I could not use getimagesize() because first I needed the value of the src property anyway.

function gather_images($str){
while(stristr($str,'',$start)){
$images[] = $cur = substr($str,$start,$end);
$str = str_replace($cur,"",$str);
}
return array($str,$images);
}


So now I've got my image strings in the images[] array. An image string could look something like this <img src="http://www.blogger.com/somefile.jpg" width="100" height="130" />. The next part is to get a property such as width out of the image string:

function get_tag_info($value,$str){
if(preg_match('/'.$value.'\s*=\s*".*"/',$str)){ // if enclosed with " "
$value = get_tag_value($value,$str,'"');
}
else if(preg_match('/'.$value."\s*=\s*'.*'/",$str)){ // if enclosed with ' '
$value = get_tag_value($value,$str,"'");
}
return $value;
}
function get_tag_value($value,$str,$quote){
$vpos = strpos($str,$value);
$q1 = strpos($str,$quote,$vpos);
$q2 = strpos($str,$quote,$q1+1);
return substr($str,$q1+1,$q2-$q1-1);
}


So there you have it, add customization and error handling as you like.

Ciao