note 120139 added to ref.image
| From: | mpyw | Date: | Thu, 10 Nov 2016 08:45:44 +0000 |
| Subject: | note 120139 added to ref.image | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-207060@lists.php.net to get a copy of this message | ||
This is an example of black-and-white imagebmp() implementation.
<?php
/**
* Output a black-and-white BMP image to either the browser or a file.
*
* @param resource $image
* An image resource, returned by one of the image creation functions,
* such as imagecreatetruecolor().
*
* @param string|null $to
* The path or an open stream resource
* (which is automatically being closed after this function returns)
* to save the file to.
* If not set or NULL, the raw image stream will be outputted directly.
*
* @param float $threshold
* The number in the range of 0.0 to 1.0.
* Brighter for larger, or darker for smaller.
*
* @return bool Returns TRUE on success or FALSE on failure.
*
*/
function imagebwbmp($image, $to = null, $threshold = 0.5)
{
if (func_num_args() < 1) {
$fmt = "imagebwbmp() expects a least 1 parameters, %d given";
trigger_error(sprintf($fmt, func_num_args()), E_USER_WARNING);
return;
}
if (!is_resource($image)) {
$fmt = "imagebwbmp() expects parameter 1 to be resource, %s given";
trigger_error(sprintf($fmt, gettype($image)), E_USER_WARNING);
return;
}
if (!is_numeric($threshold)) {
$fmt = "imagebwbmp() expects parameter 3 to be float, %s given";
trigger_error(sprintf($fmt, gettype($threshold)), E_USER_WARNING);
return;
}
if (get_resource_type($image) !== 'gd') {
$msg = "imagebwbmp(): supplied resource is not a valid gd resource";
trigger_error($msg, E_USER_WARNING);
return false;
}
switch (true) {
case $to === null:
break;
case is_resource($to) && get_resource_type($to) === 'stream':
case is_string($to) && $to = fopen($to, 'wb'):
if (preg_match('/[waxc+]/', stream_get_meta_data($to)['mode'])) {
break;
}
default:
$msg = "imagebwbmp(): Invalid 2nd parameter, it must a writable filename or a
writable stream";
trigger_error($msg, E_USER_WARNING);
return false;
}
if ($to === null) {
$to = fopen('php://output', 'wb');
}
$biWidth = imagesx($image);
$biHeight = imagesy($image);
$biSizeImage = ((int)ceil($biWidth / 32) * 32 / 8 * $biHeight);
$bfOffBits = 54 + 4 * 2; // Use two colors (black and white)
$bfSize = $bfOffBits + $biSizeImage;
fwrite($to, 'BM');
fwrite($to, pack('VvvV', $bfSize, 0, 0, $bfOffBits));
fwrite($to, pack('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 1, 0, $biSizeImage, 0, 0,
0, 0));
fwrite($to, "\xff\xff\xff\x00"); // white
fwrite($to, "\x00\x00\x00\x00"); // black
for ($y = $biHeight - 1; $y >= 0; --$y) {
$byte = 0;
for ($x = 0; $x < $biWidth; ++$x) {
$rgb = imagecolorsforindex($image, imagecolorat($image, $x, $y));
$value = (0.299 * $rgb['red'] + 0.587 * $rgb['green'] + 0.114 *
$rgb['blue']) / 0xff;
$color = (int)($value > $threshold);
$byte = ($byte << 1) | $color;
if ($x % 8 === 7) {
fwrite($to, pack('C', $byte));
$byte = 0;
}
}
if ($x % 8) {
fwrite($to, pack('C', $byte << (8 - $x % 8)));
}
if ($x % 32) {
fwrite($to, str_repeat("\x00", (int)((32 - $x % 32) / 8)));
}
}
return true;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 58.146.6.197
----
Manual Page -- http://php.net/manual/en/ref.image.php
Edit -- https://master.php.net/note/edit/120139
Del: integrated -- https://master.php.net/note/delete/120139/integrated
Del: useless -- https://master.php.net/note/delete/120139/useless
Del: bad code -- https://master.php.net/note/delete/120139/bad+code
Del: spam -- https://master.php.net/note/delete/120139/spam
Del: non-english -- https://master.php.net/note/delete/120139/non-english
Del: in docs -- https://master.php.net/note/delete/120139/in+docs
Del: other reasons-- https://master.php.net/note/delete/120139
Reject -- https://master.php.net/note/reject/120139
Search -- https://master.php.net/manage/user-notes.php