How to convert a byteArray to String in PHP? - php

I have a WebSocket encoder:
print_r(frameEncode("How can I convert a byteArray to String in PHP?"));
function frameEncode($message) {
$messageBytes = array();
$messageLength = strlen($message);
$messageBytes[0] = 129;
if ($messageLength < 126) {
$messageBytes[1] = $messageLength;
} else if ($messageLength <= 65535) {
$messageBytes[1] = 126;
$messageBytes[2] = ($messageLength >> 8) & 255;
$messageBytes[3] = $messageLength & 255;
} else {
$messageBytes[1] = 127;
$messageBytes[2] = ($messageLength >> 56) & 255;
$messageBytes[3] = ($messageLength >> 48) & 255;
$messageBytes[4] = ($messageLength >> 40) & 255;
$messageBytes[5] = ($messageLength >> 32) & 255;
$messageBytes[6] = ($messageLength >> 24) & 255;
$messageBytes[7] = ($messageLength >> 16) & 255;
$messageBytes[8] = ($messageLength >> 8) & 255;
$messageBytes[9] = $messageLength & 255;
}
return pack("C*", $messageBytes) . $message;
}
How can I convert the $messageBytes to a string at the end of the function? The array values are simply getting ignored by pack().

You could use call_user_func_array like this:
call_user_func_array('pack', array_merge(array('C*'), $messageBytes)))

return pack("C*", implode(' ', $messageBytes)) . $message;

Related

How to pixelar image in php

What I'm trying to do is to pixelate an image of a url, add another image above it and last save that image.
I have obtained this internet code to pixelate images but I think it does not work correctly. It does not show me the pixelated image nor does it save it. Just the black screen remains.
$pixel = 15;
$getImagen = 'https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg';
$imagen = imagecreatefromjpeg($getImagen);
if(!$imagen) exit('ERROR');
list($ancho,$alto)=getimagesize($getImagen);
$superficieTotal = $ancho*$alto;
//
$superficieRecorrida = 0;
$auxX=0;
$auxY=0;
while($superficieRecorrida <= $superficieTotal){
$posX=0;$posY=0;$data = array();
while($posX <= $pixel and (($auxX + $posX) < $ancho)){
$posY=0;
while($posY <= $pixel and (($auxY + $posY) < $alto)){
$rgb = imagecolorat($imagen, ($auxX + $posX), ($auxY + $posY));
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$data[] = array($r,$g,$b);
$posY++;
}
$posX++;
}
// Busco promedio
$r = 0; $g = 0; $b = 0;
foreach($data as $d){
$r+= $d[0];
$g+= $d[1];
$b+= $d[2];
}
$totalArray = count($data);
if($totalArray == 0) $totalArray = 1;
$r = $r/$totalArray;
$g = $g/$totalArray;
$b = $b/$totalArray;
$colorPromedio = imagecolorallocate($imagen, $r, $g, $b);
imagefilledrectangle($imagen, $auxX, $auxY, ($auxX + $pixel), ($auxY + $pixel), $colorPromedio);
//
$auxX+= $pixel;
if($auxX >= $ancho){
$auxX = 0;
$auxY+= ($pixel+1);
}
$superficieRecorrida+= $pixel*$pixel;
}
//
Header("Content-type: image/jpeg");
imagejpeg($imagen);
imagedestroy($imagen);
Thanks for your future answers. a greeting

Using PHP-GPG with Wordpress

I'm working on an implementation, which should send every outgoing mail from my Wordpress installation gpg encrypted.
I built my small plugin with tutorial from Tim Nash, and I used the php-gpg lib from Jason Hinkle. When I send an email from Wordpress 4.3, I get an pgp-encrypted mail, but I can't open it, because WP / my plugin uses the wrong public key. I checked it out, and pasted the right key in my user wp-profile, but ... nothing: wrong key. Have you any ideas?
<?php
/** #package php-gpg::GPG */
/** seed rand */
list($gpg_usec, $gpg_sec) = explode(' ', microtime());
srand((float) $gpg_sec + ((float) $gpg_usec * 100000));
/**
* #package php-gpg::GPG
*/
class GPG_Utility
{
static function starts_with($haystack, $needle)
{
return $needle === "" || strpos($haystack, $needle) === 0;
}
static function B0($x) {
return ($x & 0xff);
}
static function B1($x) {
return (($x >> 0x8) & 0xff);
}
static function B2($x) {
return (($x >> 0x10) & 0xff);
}
static function B3($x) {
return (($x >> 0x18) & 0xff);
}
static function zshift($x, $s) {
$res = $x >> $s;
$pad = 0;
for ($i = 0; $i < 32 - $s; $i++) $pad += (1 << $i);
return $res & $pad;
}
static function pack_octets($octets)
{
$i = 0;
$j = 0;
$len = count($octets);
$b = array_fill(0, $len / 4, 0);
if (!$octets || $len % 4) return;
for ($i = 0, $j = 0; $j < $len; $j += 4) {
$b[$i++] = $octets[$j] | ($octets[$j + 1] << 0x8) | ($octets[$j + 2] << 0x10) | ($octets[$j + 3] << 0x18);
}
return $b;
}
static function unpack_octets($packed)
{
$j = 0;
$i = 0;
$l = count($packed);
$r = array_fill(0, $l * 4, 0);
for ($j = 0; $j < $l; $j++) {
$r[$i++] = GPG_Utility::B0($packed[$j]);
$r[$i++] = GPG_Utility::B1($packed[$j]);
$r[$i++] = GPG_Utility::B2($packed[$j]);
$r[$i++] = GPG_Utility::B3($packed[$j]);
}
return $r;
}
static function hex2bin($h)
{
if(strlen($h) % 2) $h += "0";
$r = "";
for($i = 0; $i < strlen($h); $i += 2) {
$r .= chr(intval($h[$i], 16) * 16 + intval($h[$i + 1], 16));
}
return $r;
}
static function crc24($data)
{
$crc = 0xb704ce;
for($n = 0; $n < strlen($data); $n++) {
$crc ^= (ord($data[$n]) & 0xff) << 0x10;
for($i = 0; $i < 8; $i++) {
$crc <<= 1;
if($crc & 0x1000000) $crc ^= 0x1864cfb;
}
}
return
chr(($crc >> 0x10) & 0xff) .
chr(($crc >> 0x8) & 0xff) .
chr($crc & 0xff);
}
static function s_random($len, $textmode)
{
$r = "";
for($i = 0; $i < $len;)
{
$t = rand(0, 0xff);
if($t == 0 && $textmode) continue;
$i++;
$r .= chr($t);
}
return $r;
}
static function c_random() {
return round(rand(0, 0xff));
}
}
?>

Php imagebmp for monochrome bitmap

How to implement imagebmp() function for monochrome BMP?
I found many implementation for 24bit bitmap but nothing about 1bit bitmap.
This is a working implemenation.
I used a bindec function, I can be optimized using shift operators, but I found very easy the string padding function.
function imagebmp(&$img, $filename = false)
{
$wid = imagesx($img);
$hei = imagesy($img);
$wid_pad = str_pad('', (4-ceil($wid/8) % 4) %4, "\0");
$size = 62 + ( ceil($wid/8) + strlen($wid_pad)) * $hei;
//prepare & save header
$header['identifier'] = 'BM';
$header['file_size'] = dword($size);
$header['reserved'] = dword(0);
$header['bitmap_data'] = dword(62);
$header['header_size'] = dword(40);
$header['width'] = dword($wid);
$header['height'] = dword($hei);
$header['planes'] = word(1);
$header['bits_per_pixel'] = word(1);
$header['compression'] = dword(0);
$header['data_size'] = dword(0);
$header['h_resolution'] = dword(0);
$header['v_resolution'] = dword(0);
$header['colors'] = dword(0);
$header['important_colors'] = dword(0);
$header['white'] = chr(255).chr(255).chr(255).chr(0);
$header['black'] = chr(0).chr(0).chr(0).chr(0);
if ($filename)
{
$f = fopen($filename, "wb");
foreach ($header AS $h)
{
fwrite($f, $h);
}
//save pixels
$str="";
for ($y=$hei-1; $y>=0; $y--)
{
$str="";
for ($x=0; $x<$wid; $x++)
{
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$gs = (($r*0.299)+($g*0.587)+($b*0.114));
if($gs>150) $color=0;
else $color=1;
$str=$str.$color;
if($x==$wid-1){
$str=str_pad($str, 8, "0");
}
if(strlen($str)==8){
fwrite($f, chr((int)bindec($str)));
$str="";
}
}
fwrite($f, $wid_pad);
}
fclose($f);
}
else
{
foreach ($header AS $h)
{
echo $h;
}
//save pixels
for ($y=$hei-1; $y>=0; $y--)
{
for ($x=0; $x<$wid; $x++)
{
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$gs = (($r*0.299)+($g*0.587)+($b*0.114));
if($gs>150) $color=0;
else $color=1;
$str=$str.$color;
if($x==$wid-1){
$str=str_pad($str, 8, "0");
}
if(strlen($str)==8){
echo chr((int)bindec($str));
$str="";
}
}
echo $wid_pad;
}
}
}
function dword($n)
{
return pack("V", $n);
}
function word($n)
{
return pack("v", $n);
}

PHP - JPEG Image to RGB value Array

I am wanting to get an array of RGB values from an image. E.g. (2 X 2 pix example.)
[[[R, G, B], [R, G, B]], [[R, G, B], [R, G, B]]]
The code I have now:
<?php
// open an image
$image = imagecreatefromjpeg('image.jpg'); // imagecreatefromjpeg/png/
// get image dimension, define colour array
$width = imagesx($image);
$height = imagesy($image);
$colors = [];
for ($y = 0; $y < $height; $y++)
{
for ($x = 0; $x < $width; $x++)
{
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
}
}
print_r($colors);
?>
The above is not working.
My image is now just a 2 X 2 pix jpeg which should output:
[[[0, 255, 0], [255, 0, 0]], [[0, 0, 255], [255, 255, 255]]]
Any help greatly appreciated!
OK, nailed it. Thanks to all.
<?php
$image = imagecreatefromjpeg('image.jpg'); // imagecreatefromjpeg/png/
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++) {
$y_array = array() ;
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$x_array = array($r, $g, $b) ;
$y_array[] = $x_array ;
}
$colors[] = $y_array ;
}
print_r($colors);
?>
Comments correct, added $r, $g, $b. Restructured #jari answer and now getting a good output.
Cheers!
function getArrayOfPixelsFromFile($source) {
$image = imagecreatefromjpeg($source); // imagecreatefromjpeg/png/
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++) {
$y_array = array();
for ($x = 0; $x < $width; $x++) {
//Seleciona a cor localizada em ($x, $y)
$rgb = imagecolorat($image, $x, $y);
//echo $rgb." = ".decbin($rgb),"<br>";
//Seleciona os primeiros dois bytes que representam vermelho
$r = ($rgb >> 16) & 0xFF;
//Seleciona os dois bytes do meio que representam o verde
$g = ($rgb >> 8) & 0xFF;
//Seleciona os dois Ășltimos bytes que representam o azul
$b = $rgb & 0xFF;
$x_array = array($r, $g, $b);
$y_array[] = $x_array;
}
$colors[] = $y_array;
}
return $colors;
}
I fixed your code by creating subarrays and adding elements to it as it should be.
for ($y = 0; $y < $height; $y++)
{
$height_arr = array() ;
for ($x = 0; $x < $width; $x++)
{
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$width_arr = array($r, $g, $b) ;
$height_array[] = $width_arr ;
}
$colors[] = $height_arr ;
}
How about this?
[...]
$colors = [];
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$colors[$y][$x] = array($r,$g,$b); // or, $colors[$x][$y] = array($r,$g,$b);
}
}
print_r($colors);

Find White Pixel Lines

I want to replace all white pixel from a completely white column with other color and if I have a black pixel on column don't change anything, but I don't know where is the problem...
Here is the code:
function findLines() {
$blank = 1;
$im_1 = imageCreateFromPng('resize_1.png');
for($i=0; $i<1090; $i++) {
if($blank == 1) {
for($j=0; $j<240; $j++) {
$rgb = imageColorAt($im_1, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$c = $r.$g.$b;
if($c === "0 0 0") {
$blank = 0;
}
$color = imageColorAllocate($im_1, 0, 255, 255);
imageSetPixel($im_1, $i, $j, $color);
}
}
if ($blank == 0) {
for($j=0; $j<240; $j++) {
$rgb = imageColorAt($im_1, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$c = $r . " " . $g . " " . $b;
if($c === "255 255 255") {
$blank = 1;
}
}
} else {
$blank = 0;
}
}
header("Content-Type: image/png");
imagepng($im_1);
}
This is the right one. Thanks anyway!
function findLines() {
$im_1 = imageCreateFromPng('resize_1.png');
for($i=0; $i<1090; $i++) {
$blank = 1;
for($j=0; $j<240; $j++) {
$rgb = imageColorAt($im_1, $i, $j);
if($rgb == 0) {
$blank = 0;
}
}
if ($blank == 1) {
for($j=0; $j<240; $j++) {
$color = imageColorAllocate($im_1, 155, 155, 155);
imageSetPixel($im_1, $i, $j, $color);
}
} else {
$blank = 0;
}
}
header("Content-Type: image/png");
imagepng($im_1);
}

Categories