Voting

: four plus four?
(Example: nine)

The Note You're Voting On

dyer85 at gmail dot com
19 years ago
Took me a while, but thanks to a couple of the user notes on the array_values PHP documentation page, I was able to come up with a way to dynamically compute the divisor.

I'm using PHP 5.1.0b2 on Win32 with the bundled GD library. When I try and use the imageconvolution function, whether normally, or via the functions below, the resulting image (I've only tried JPEGs and GIFs), always comes out far too bright, even when the divisor makes matrix sum equal to 1. The only thing that would reduce the brightness was to make the offset argument ridiculously large. So, I'm not sure if this effects anyone else.

Here are the functions with an example:

<?php
$im
= imagecreatefromjpeg('path/to/pic.jpg');
$matrix = array( array(5,5,5),
array(
5,15,5),
array(
5,5,5) );
makeFilter($im, $matrix);

header ( 'Content-Type: image/jpeg' );
imagejpeg($im);
imagedestroy($im);

/**
* functions
*/
// This flattens the 3X3 array matrix, so we can get the sum of all the values
function array_flatten($array) {
(array)
$tempArray = array();

foreach (
$array as $value ) {
if (
is_array($value) ) {
$tempArray = array_merge($tempArray, array_flatten($value));
} else {
$tempArray[] = $value;
}
}

return
$tempArray;
}

// Creates the divisor value dynamically, and passes offset
function makeFilter($resource, $matrix, $offset=1.0) {
global $
$resource;
(float)
$divisor = array_sum(array_flatten($matrix));
return
imageconvolution($resource, $matrix, $divisor, $offset) ? true : false;
}
?>

<< Back to user notes page

To Top