Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how to convert below panaroma image to cube iamges in php?
I used imagick, js code and ... but do not result.
main image
please help me
thanks
After much effort ...
I got the right result
my final code:
# get x,y,z coords from out image pixels coords
# i,j are pixel coords
# faceIdx is face number
# faceSize is edge length
function outImgToXYZ($i, $j, $faceIdx, $faceSize)
{
$a = 2.0 * (float) $i / $faceSize;
$b = 2.0 * (float) $j / $faceSize;
switch ($faceIdx)
{
case 0: return [-1.0, 1.0 - $a, 3.0 - $b]; break; # back
case 1: return [$a - 3.0, -1.0, 3.0 - $b]; break; # left
case 2: return [1.0, $a - 5.0, 3.0 - $b]; break; # front
case 3: return [7.0 - $a, 1.0, 3.0 - $b]; break; # right
case 4: return [$b - 1.0, $a - 5.0, 1.0]; break; # top
case 5: return [5.0 - $b, $a - 5.0, -1.0]; break; # bottom
}
return [0, 0, 0];
}
function clip($x, $min, $max)
{
if ($x > $max) return $max;
if ($x < $min) return $min;
return $x;
}
# convert using an inverse transformation
function convertFace($imgIn, $imgInWidth, $imgInHeight, $imgOut, $imgOutWidth, $imgOutHeight)
{
$pi = pi();
$edge = $imgInWidth / 4;
$face = 0;
$face2 = 0;
$rng = null;
for($xOut = 0; $xOut <= $imgOutWidth; $xOut++)
{
$face = floor($xOut / $edge); // 0 - back, 1 - left 2 - front, 3 - right
if ($face === 2) $rng = range(0, $edge * 3, 1);
else $rng = range($edge, $edge * 2, 1);
for($yOut =0; $yOut <= $imgOutHeight; $yOut++)
{
if ($yOut < $edge) $face2 = 4; // top
else if ($yOut >= 2 * $edge) $face2 = 5; // bottom
else $face2 = $face;
list($x, $y, $z) = outImgToXYZ($xOut, $yOut, $face2, $edge);
$theta = atan2($y, $x);
$r = hypot($x, $y);
$phi = atan2($z, $r);
# source img coords
$uf = 2.0 * $edge * ($theta + $pi) / $pi;
$vf = 2.0 * $edge * ($pi / 2 - $phi) / $pi;
# Use bilinear interpolation between the four surrounding pixels
$ui = floor($uf); # coord of pixel to bottom left
$vi = floor($vf);
$u2 = $ui + 1; # coords of pixel to top right
$v2 = $vi + 1;
$mu = $uf - $ui; # fraction of way across pixel
$nu = $vf - $vi;
# Pixel values of four corners
$a = imagecolorsforindex($imgIn, imagecolorat($imgIn, $ui % $imgInWidth, clip($vi, 0, $imgInHeight - 1)));
$b = imagecolorsforindex($imgIn, imagecolorat($imgIn, $u2 % $imgInWidth, clip($vi, 0, $imgInHeight - 1)));
$c = imagecolorsforindex($imgIn, imagecolorat($imgIn, $ui % $imgInWidth, clip($v2, 0, $imgInHeight - 1)));
$d = imagecolorsforindex($imgIn, imagecolorat($imgIn, $u2 % $imgInWidth, clip($v2, 0, $imgInHeight - 1)));
# interpolate
list($r, $g, $b) =
[
$a["red"] * (1 - $mu) * (1 - $nu) + $b["red"] * ($mu) * (1 - $nu) + $c["red"] * (1 - $mu) * $nu + $d["red"] * $mu * $nu,
$a["green"] * (1 - $mu) * (1 - $nu) + $b["green"] * ($mu) * (1 - $nu) + $c["green"] * (1 - $mu) * $nu + $d["green"] * $mu * $nu,
$a["blue"] * (1 - $mu) * (1 - $nu) + $b["blue"] * ($mu) * (1 - $nu) + $c["blue"] * (1 - $mu) * $nu + $d["blue"] * $mu * $nu
];
imagesetpixel($imgOut, $xOut, $yOut, imagecolorallocate($imgOut, (int) round($r), (int) round($g), (int) round($b)));
}
}
return $imgOut;
}
// start convert
$imgInPath = "./main.jpg";
$imgIn = imagecreatefromjpeg($imgInPath);
$imgInSize = getimagesize($imgInPath);
$imgInWidth = $imgInSize[0];
$imgInHeight = $imgInSize[1];
$imgOutWidthHeight = $imgInWidth / 4;
$imgOutFullWidth = $imgInWidth;
$imgOutFullHeight = $imgInWidth * 3 / 4;
$imgFormat = "jpg";
$imgOut = imagecreate($imgOutFullWidth, $imgOutFullHeight);
imagefill($imgOut, 0, 0, imagecolorallocate($imgOut, 0, 0, 0));
imagejpeg($imgOut, "./temp.jpg", 100);
$imgOut = imagecreatefromjpeg("./temp.jpg");
imagefill($imgOut, 0, 0, imagecolorallocate($imgOut, 0, 0, 0));
$imgOut = convertFace($imgIn, $imgInWidth, $imgInHeight, $imgOut, $imgOutFullWidth, $imgOutFullHeight);
imagejpeg($imgOut, "./temp.jpg", 100);
imagedestroy($imgOut);
$faceNames = [['', '', 'top', ''], ['left', 'front', 'right', 'back'], ['', '', 'bottom', '']];
for($i = 0; $i < 3; $i++)
{
for($j = 0; $j < 4; $j++)
{
$faceName = $faceNames[$i][$j];
if($faceName != "")
{
$im = imagecreatefromjpeg("temp.jpg");
$im2 = imagecrop($im, ["x" => ($j * $imgOutWidthHeight), "y" => ($i * $imgOutWidthHeight), "width" => $imgOutWidthHeight, "height" => $imgOutWidthHeight]);
if($im2 !== FALSE)
{
imagejpeg($im2, "main_$faceName.jpg", 100);
imagedestroy($im2);
}
imagedestroy($im);
}
}
}
unlink("./temp.jpg");
I am currently trying to make an animated Mandelbrot set visualisation. But right now it won't even render a single frame correctly.
I don't know where I made the mistake. I guess there is an error in the math.Will you please have a look at it?
Here is a how it looks right now:
Here is my mandelbrot function:
function mandelbrot($a, $b, $limit) {
$a_orig = $a;
$b_orig = $b;
$count = 0;
while(($count < $limit) && (sqrt(($a * $a) + ($b * $b)) <= 2)) {
$a = ($a * $a) - ($b * $b) + $a_orig;
$b = (2 * $a * $b) + $b_orig;
$count++;
}
return $count;
}
And here is the entire code:
<?php
function HSVtoRGB(array $hsv) {
list($H,$S,$V) = $hsv;
//1
$H *= 6;
//2
$I = floor($H);
$F = $H - $I;
//3
$M = $V * (1 - $S);
$N = $V * (1 - $S * $F);
$K = $V * (1 - $S * (1 - $F));
//4
switch ($I) {
case 0:
list($R,$G,$B) = array($V,$K,$M);
break;
case 1:
list($R,$G,$B) = array($N,$V,$M);
break;
case 2:
list($R,$G,$B) = array($M,$V,$K);
break;
case 3:
list($R,$G,$B) = array($M,$N,$V);
break;
case 4:
list($R,$G,$B) = array($K,$M,$V);
break;
case 5:
case 6: //for when $H=1 is given
list($R,$G,$B) = array($V,$M,$N);
break;
}
return array($R, $G, $B);
}
function mandelbrot($a, $b, $limit) {
$a_orig = $a;
$b_orig = $b;
$count = 0;
while(($count < $limit) && (sqrt(($a * $a) + ($b * $b)) <= 2)) {
$a = ($a * $a) - ($b * $b) + $a_orig;
$b = (2 * $a * $b) + $b_orig;
$count++;
}
return $count;
}
ini_set("max_execution_time", 0);
header ("Content-Type: image/gif");
$num_frames = 60;
$size = 1024;
$points = array($size);
$image = imagecreate($size, $size);
for($j = 0; $j <= $num_frames; $j++) {
$tmp_color = HSVtoRGB(array(($j + 1) / ($num_frames + 1), 1, 1));
$color[$j] = imagecolorallocate($image, $tmp_color[0] * 255, $tmp_color[1] * 255, $tmp_color[2] * 255);
}
for($x = 0; $x < $size; $x++) {
for($y = 0; $y < $size; $y++) {
imagesetpixel($image, $x, $y, $color[mandelbrot(-2 + ($x * 2.7 / ($size - 1)), -1.35 + ($y * 2.7 / ($size - 1)), $num_frames)]);
}
}
imagegif($image);
imagedestroy($image);
?>
Your complex number square is wrong. You are overwriting the old value of a where it is needed again in the computation of b. So save it in a temporary variable.
Also, the bailout value of 60 iterations is rather small, 200 would be more appropriate for this scale, for more detailed images it should be reasonably rapidly increase.
Use a*a+b*b < 4 instead of the unnecessary square root. One could re-use the values of a*a and b*b which would also solve the problem of the temporary variable.
norm=10
while ... and norm < 4
a2=a*a
b2=b*b
norm=a2+b2
b=2*a*b+b_orig
a=a2-b2+a_orig
end
I can normally catch these easy, but......
function linear_regression($x, $y) {
// calculate number points
$n = count($x);
// ensure both arrays of points are the same size
if ($n != count($y)) {
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
}
// calculate sums
$x_sum = array_sum($x);
$y_sum = array_sum($y);
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $n; $i++) {
$xy_sum+=($x[$i]*$y[$i]);
$xx_sum+=($x[$i]*$x[$i]);
}
// calculate slope
//$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
$divisor = (($n * $xx_sum) – ($x_sum * $x_sum));
if ($divisor == 0){
$m = 0;
} else {
$m = (($n * $xy_sum) – ($x_sum * $y_sum)) / $divisor;
}
// calculate intercept
$b = ($y_sum - ($m * $x_sum)) / $n;
// return result
return array("m"=>$m, "b"=>$b);
}
var_dump( linear_regression(array(1, 2, 3, 4, 4), array(1.5, 1.6, 2.1, 3.0, 6)) );
The error is happening here $divisor = (($n * $xx_sum) – ($x_sum * $x_sum));
Any ideas why?
The minus sign is a fancy unicode dash (I think the em dash) instead of the regular ascii - character.
Don't ask me how I discovered that, but your "-" is not a real "-", is another character, but have the same apparence
Ok you didn't asked me, but I have discovered at http://writecodeonline.com/php/ , it doesn't recognize yours "-" character
Now working code with real "-", you can copy an paste, and see:
function linear_regression($x, $y) {
// calculate number points
$n = count($x);
// ensure both arrays of points are the same size
if ($n != count($y)) {
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
}
// calculate sums
$x_sum = array_sum($x);
$y_sum = array_sum($y);
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $n; $i++) {
$xy_sum+=($x[$i]*$y[$i]);
$xx_sum+=($x[$i]*$x[$i]);
}
// calculate slope
//$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
$divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
if ($divisor == 0){
$m = 0;
} else {
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
}
// calculate intercept
$b = ($y_sum - ($m * $x_sum)) / $n;
// return result
return array("m"=>$m, "b"=>$b);
}
var_dump( linear_regression(array(1, 2, 3, 4, 4), array(1.5, 1.6, 2.1, 3.0, 6)) );
I retyped the offending line like this:
$divisor = (($n*$xx_sum)-($x_sum * $x_sum));
The error disappeared. After a bit of poking the error seems to be related to the minus sign in the middle. deleting it and retyping it seems to fix the problem.
See this fiddle:
Adam is right, but also, you didn’t close the for loop. Here try this.
Also, the minus sign was the wrong sign here:
$divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
As well as here:
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
My cleaned up version of your function with the closed for loop in place as well here.
function linear_regression($x, $y) {
// calculate number points
$n = count($x);
// ensure both arrays of points are the same size
if ($n != count($y)) {
trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
}
// calculate sums
$x_sum = array_sum($x);
$y_sum = array_sum($y);
$xx_sum = 0;
$xy_sum = 0;
for($i = 0; $i < $n; $i++) {
$xy_sum+=($x[$i]*$y[$i]);
$xx_sum+=($x[$i]*$x[$i]);
// calculate slope
//$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
$divisor = (($n * $xx_sum) - ($x_sum * $x_sum));
if ($divisor == 0) {
$m = 0;
}
else {
$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / $divisor;
}
// calculate intercept
$b = ($y_sum - ($m * $x_sum)) / $n;
}
// return result
return array("m"=>$m, "b"=>$b);
} // linear_regression
Hello fellow earthlings. A quesion about RGB color and its usefulness in a simple tiny php code:
Imagine I have variable $colorA containning a valid six char color. say B1B100, a greenish natural color. Now If I would like to make a new color from that, which is, say, ten steps lighter thatn that original color, roughly.
$colorA = B1B100 // original color
php code with little color engine lightening stuff up goes here
$colorB = ?????? // original color lightened up
Is there a php ready function that KNOWS rgb colors something like
php function RGB ( input color, what to do, output color)
Where what to do could be +/- 255 values of brightness etc etc.
Is something like this already possible or am I day dreaming?
rgb-hsl($colorA, +10, $colorB);
If this does not exist, what would be the shortest code for doing this? Suggestions, code or ideas are all answers to me. Thanks.
This SO question has a full-blown PHP script that can convert a RGB to a HSL colour, and increase its H component of a HSL colour - it should be trivial to change to increase L instead.
In general if you want a lighter shade of a particular colour, the most accurate process is to convert from RGB to HSL (or HSV), change the 'L' (or 'V') value which represents lightness, and then convert back to RGB.
This will preserve the "hue", which represents where the colour sits on the spectrum, but change the "tint" (if lightening) or "shade" (if darkening) of that colour.
See http://en.wikipedia.org/wiki/HSL_and_HSV for more information.
On this website: http://www.sitepoint.com/forums/showthread.php?t=586223 they are talking about this code which is originally made by opensource Drupal. Seems to work fine in PHP!?
Now, how do I now indermingle myself with this code and change the lightness of an HSL value, before its outputted as RGB again?
<?php
### RGB >> HSL
function _color_rgb2hsl($rgb) {
$r = $rgb[0]; $g = $rgb[1]; $b = $rgb[2];
$min = min($r, min($g, $b)); $max = max($r, max($g, $b));
$delta = $max - $min; $l = ($min + $max) / 2; $s = 0;
if ($l > 0 && $l < 1) {
$s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
}
$h = 0;
if ($delta > 0) {
if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
$h /= 6;
} return array($h, $s, $l);
}
### HSL >> RGB
function _color_hsl2rgb($hsl) {
$h = $hsl[0]; $s = $hsl[1]; $l = $hsl[2];
$m2 = ($l <= 0.5) ? $l * ($s + 1) : $l + $s - $l*$s;
$m1 = $l * 2 - $m2;
return array(_color_hue2rgb($m1, $m2, $h + 0.33333),
_color_hue2rgb($m1, $m2, $h),
_color_hue2rgb($m1, $m2, $h - 0.33333));
}
### Helper function for _color_hsl2rgb().
function _color_hue2rgb($m1, $m2, $h) {
$h = ($h < 0) ? $h + 1 : (($h > 1) ? $h - 1 : $h);
if ($h * 6 < 1) return $m1 + ($m2 - $m1) * $h * 6;
if ($h * 2 < 1) return $m2;
if ($h * 3 < 2) return $m1 + ($m2 - $m1) * (0.66666 - $h) * 6;
return $m1;
}
### Convert a hex color into an RGB triplet.
function _color_unpack($hex, $normalize = false) {
if (strlen($hex) == 4) {
$hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
} $c = hexdec($hex);
for ($i = 16; $i >= 0; $i -= 8) {
$out[] = (($c >> $i) & 0xFF) / ($normalize ? 255 : 1);
} return $out;
}
### Convert an RGB triplet to a hex color.
function _color_pack($rgb, $normalize = false) {
foreach ($rgb as $k => $v) {
$out |= (($v * ($normalize ? 255 : 1)) << (16 - $k * 8));
}return '#'. str_pad(dechex($out), 6, 0, STR_PAD_LEFT);
}
/* $testrgb = array(0.2,0.75,0.4); //RGB to start with
print_r($testrgb); */
print "Hex: ";
$testhex = "#b7b700";
print $testhex;
$testhex2rgb = _color_unpack($testhex,true);
print "<br />RGB: ";
var_dump($testhex2rgb);
print "<br />HSL color module: ";
$testrgb2hsl = _color_rgb2hsl($testhex2rgb); //Converteren naar HSL
var_dump($testrgb2hsl);
print "<br />RGB: ";
$testhsl2rgb = _color_hsl2rgb($testrgb2hsl); // En weer terug naar RGB
var_dump($testhsl2rgb);
print "<br />Hex: ";
$testrgb2hex = _color_pack($testhsl2rgb,true);
var_dump($testrgb2hex);
?>
PHP does have a couple image manipulation libraries. Either GD or Imagemagick
EDIT: I jumped the gun, these libraries do not have direct PHP color manipulation functions - I honestly assumed they did of a sort after seeing a lot of the things they can do with images via PHP. They do accomplish a lot of cool things. Here's one guy's example.
I am working on a Facebook App that needs to be able to average three numbers. But, it always return 0 as the answer. Here is my code:
$y = 100;
$n = 250;
$m = 300;
$number = ($y + $n + $m / 3);
echo 'Index: '.$number;
It always displays Index: 0
Any ideas?
$y = 100;
$n = 250;
$m = 300;
$number = ($y + $n + $m) / 3;
echo 'Index: '.$number;
Also - you missed ; in the end of the first 3 lines
Your parentheses are grouped wrongly. You should be doing:
$number = ($y + $n + $m) / 3;
Two problems:
You are missing ; at the end of these lines:
$y = 100
$n = 250
$m = 300
And to / has higher precedence than + so you need to do:
$number = ($y + $n + $m) / 3;