Skip to content

Commit 6562767

Browse files
added validator function for the threshold value
1 parent 330acce commit 6562767

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

References.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,29 @@ public function interlace(string $interlaceType): string
222222
'http://www.imagemagick.org/script/command-line-options.php#interlace'
223223
));
224224
}
225+
226+
/**
227+
* Checks that threshold value is valid according to ImageMagick command line reference.
228+
*/
229+
public function threshold(string $threshold): float
230+
{
231+
$threshold = \trim($threshold);
232+
233+
if (substr($threshold, -1) == '%') {
234+
$percent_num = substr($threshold, 0, -1);
235+
if (is_numeric($percent_num))
236+
return $threshold;
237+
} else {
238+
if (is_int(filter_var($threshold, FILTER_VALIDATE_INT)) && intval($threshold) >= 0)
239+
return $threshold;
240+
}
241+
242+
throw new \InvalidArgumentException(\sprintf(
243+
'The specified threshold parameter (%s) is invalid.'."\n".
244+
'The value must be in percentage or an absolute integer'."\n".
245+
'Please refer to ImageMagick command line documentation:'."\n%s",
246+
$threshold, 'http://www.imagemagick.org/script/command-line-options.php#threshold'
247+
));
248+
249+
}
225250
}

0 commit comments

Comments
 (0)