I want to generate a number between 1 and 100, but I want it to keep regenerating that number until it equals 50, once it equals 50, then echo it out. How would I do this?
My Function:
function create() {
$production_line = mt_rand(0, 3);
$random1 = mt_rand(0, 9);
$random2 = mt_rand(0, 9);
$random3 = mt_rand(0, 9);
$random4 = mt_rand(0, 9);
$random5 = mt_rand(0, 9);
$random6 = mt_rand(0, 9);
$production_year = mt_rand(3, 4);
$week1 = 4;
$week2 = 8;
$factory1 = 4;
$factory2 = 8;
if ($production_line + $random1 + $random2 + $random3 + $random4 + $random5 + $random6 + $production_year + $week1 + $week2 + $factory1 + $factory2 == 55) {
return $production_line.$random1.$random2.$random3.$random4.$random5.$random6.$production_year.$week1.$week2.$factory1.$factory2;
}
}
Use a simple loop:
$i = 0;
while ($rand = mt_rand(0,100)) {
$i++;
if ($rand == 50) {
// found 50, so break out of the loop
break;
}
}
echo "It took $i iterations to find 50";
But that's a bit pointless, right? If you're just going to output 50 all the time, then why do you need to generate a random number? Just echo 50 instead. Also note that this could be a slow operation if the larger limit is a bigger number.
Generate and test all the random numbers inside a while loop.
function create() {
$week1 = 4;
$week2 = 8;
$factory1 = 4;
$factory2 = 8;
while (true) {
$production_line = mt_rand(0, 3);
$random1 = mt_rand(0, 9);
$random2 = mt_rand(0, 9);
$random3 = mt_rand(0, 9);
$random4 = mt_rand(0, 9);
$random5 = mt_rand(0, 9);
$random6 = mt_rand(0, 9);
$production_year = mt_rand(3, 4);
if ($production_line + $random1 + $random2 + $random3 + $random4 + $random5 + $random6 + $production_year + $week1 + $week2 + $factory1 + $factory2 == 55) {
return $production_line.$random1.$random2.$random3.$random4.$random5.$random6.$production_year.$week1.$week2.$factory1.$factory2;
}
}
}
I've rearranged bits of the code to make it more cohesive:
function create() {
do {
$arr = [
mt_rand(0, 3), // line
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(0, 9),
mt_rand(3, 4), // year
4, // weeks
8,
4, // factories
8,
];
} while (array_sum($arr) != 55);
return join('', $arr);
}
$number = 0;
$try = 0;
while ($number != 50) {
$try++;
$number = rand(1,100);
}
echo "found $number after $try cycles";
Related
I have a PHP pie chart which uses data from Mysql to show the chart. However if one data is missing the whole chart turns to one color. For example for grading if the input is A, B, D and F(pay attention C grade input is missing) then the whole pie chart is in one color like Orange or red.
Can you please help me with this? Thanks
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('0000ff', '006600', 'ffff00','DD7C1D', 'FF3300', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = array_filter(explode('*',$data));
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
How can I fix this undefined offset error.
I get: Undefined offset 0 - 44, on line 234.
Line 234 looks like this:
$mysql['invo_mysql_where'] .= ( $x > 0 ? " OR " : '' )."`id` = {$mysql['inactive'][$x]}";
The entire part of the code, looks like this:
shuffle($mysql['inactive']);
for($x = 0, $l = $mysql['inactive_amount']; $x < $l; $x++){
$mysql['invo_mysql_where'] .= ( $x > 0 ? " OR " : '' )."`id` = {$mysql['inactive'][$x]}";
}
$mysql_inactive:
$mysql['inactive'] = array();
if($ile == 10){
$mysql['inactive_amount'] = mt_rand(0, 1);
} else if($ile == 20){
$mysql['inactive_amount'] = mt_rand(0, 2);
} else if($ile == 30){
$mysql['inactive_amount'] = mt_rand(0, 3);
} else if($ile == 40){
$mysql['inactive_amount'] = mt_rand(0, 4);
} else if($ile == 50){
$mysql['inactive_amount'] = mt_rand(0, 4);
} else if($ile == 60){
$mysql['inactive_amount'] = mt_rand(1, 3);
} else if($ile == 70){
$mysql['inactive_amount'] = mt_rand(2, 3);
} else if($ile == 80){
$mysql['inactive_amount'] = mt_rand(3, 4);
} else if($ile == 90){
$mysql['inactive_amount'] = mt_rand(4, 4);
} else if($ile == 100){
$mysql['inactive_amount'] = mt_rand(5, 5);
} else if($ile == 200){
$mysql['inactive_amount'] = mt_rand(6, 5);
} else {
$mysql['inactive_amount'] = mt_rand(20, 50);
}
$mysql['invo_mysql_where'] = '';
The problem is that you define $mysql['inactive'] as an empty array here:
$mysql['inactive'] = array();
Then never add elements to it. Later, you try to use it as a populated array here:
$mysql['inactive'][$x]
The error occurs because no indexes are defined for the $mysql['inactive'] array. In order to fix this, you'll need to populate it with some data.
I work with python 2.7
I I've a string in DB that represente a image in Hexa. Exemple
sdata = "789C9D953D56C52010856363696D49E90AAC73ECDD4396C25228B210CE711B2CC2CAC622CECC9D0C0321313A27E411123EEEFCC07B7BFF7A9CC45EA9BD507BD6F620F769CAF4FEE3096DB76DDACEAEE9865D4CF79C6DAB34F46D441F7F23F88F6F728E6AD794724EDD5CBB9B790EF53FBF1595D9524C517E93CDEA3A433D984E83440327B318B633BF867A4C12734A5654CE26F24F29AB28704A067685363C665B0582D30ADF0F39A2717F3979C9412A6108A1D731C6992C04BD96252ECB9A2AC4A60F2B07904AA8166C84B51545D172C3C8D02B4CA3D51D841F7584B5CD2E17E2698A5DDE991302AD6240189666558242122D68F1C0F19F99475104D0F7C6216D5A6665AFAED62F8A27730A57E3BC4858669D25716B387BA04E39B41059BCC7E99CEAF4B05F971C75AAB0181AE938111CA9DB9A71C9B5443EA000D4231183A4F8ECEF79E7E5B40E2DEF647BDEA9AB6250EA59F70B6AC90E9FAABFB7D040E43C010107D4F1086A4ADA6D8DA66C8AEDD9C10E3514196A0F060220B59825C843883F5D71A67586809FEDF17FFCD75C4CFC012B43550B"
Now, i want to create the image using this string. I juste know that the image is 24x24 BMP (or PNG?) image.
I know the php code to get it, but not the python.
php :
function imagecreatefrombmpstring($im) {
$header = unpack("vtype/Vsize/v2reserved/Voffset", substr($im, 0, 14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", substr($im, 14, 40));
extract($info);
extract($header);
if($type != 0x4D42)
return false;
$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$imres=imagecreatetruecolor($width, $height);
imagealphablending($imres, false);
imagesavealpha($imres, true);
$pal=array();
if($palette_size) {
$palette = substr($im, 54, $palette_size);
$gd_palette = "";
$j = 0; $n = 0;
while($j < $palette_size) {
$b = ord($palette{$j++});
$g = ord($palette{$j++});
$r = ord($palette{$j++});
$a = ord($palette{$j++});
if ( ($r == 255) && ($g == 0) && ($b == 255))
$a = 127; // alpha = 255 on 0xFF00FF
$pal[$n++] = imagecolorallocatealpha($imres, $r, $g, $b, $a);
}
}
$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03): 0;
for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
$scan_line = substr($im, $offset + (($scan_line_size + $scan_line_align) * $l), $scan_line_size);
if($bits == 24) {
$j = 0; $n = 0;
while($j < $scan_line_size) {
$b = ord($scan_line{$j++});
$g = ord($scan_line{$j++});
$r = ord($scan_line{$j++});
$a = 0;
if ( ($r == 255) && ($g == 0) && ($b == 255))
$a = 127; // alpha = 255 on 0xFF00FF
$col=imagecolorallocatealpha($imres, $r, $g, $b, $a);
imagesetpixel($imres, $n++, $i, $col);
}
}
else if($bits == 8) {
$j = 0;
while($j < $scan_line_size) {
$col = $pal[ord($scan_line{$j++})];
imagesetpixel($imres, $j-1, $i, $col);
}
}
else if($bits == 4) {
$j = 0; $n = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = $byte >> 4;
$p2 = $byte & 0x0F;
imagesetpixel($imres, $n++, $i, $pal[$p1]);
imagesetpixel($imres, $n++, $i, $pal[$p2]);
}
}
else if($bits == 1) {
$j = 0; $n = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = (int) (($byte & 0x80) != 0);
$p2 = (int) (($byte & 0x40) != 0);
$p3 = (int) (($byte & 0x20) != 0);
$p4 = (int) (($byte & 0x10) != 0);
$p5 = (int) (($byte & 0x08) != 0);
$p6 = (int) (($byte & 0x04) != 0);
$p7 = (int) (($byte & 0x02) != 0);
$p8 = (int) (($byte & 0x01) != 0);
imagesetpixel($imres, $n++, $i, $pal[$p1]);
imagesetpixel($imres, $n++, $i, $pal[$p2]);
imagesetpixel($imres, $n++, $i, $pal[$p3]);
imagesetpixel($imres, $n++, $i, $pal[$p4]);
imagesetpixel($imres, $n++, $i, $pal[$p5]);
imagesetpixel($imres, $n++, $i, $pal[$p6]);
imagesetpixel($imres, $n++, $i, $pal[$p7]);
imagesetpixel($imres, $n++, $i, $pal[$p8]);
}
}
}
return $imres;
}
$data = #gzuncompress(pack('H*', $sdata));
$image = imagecreatefrombmpstring($data);
I find this code on http://code.google.com/p/fluxcp/source/browse/branches/fluxcp-1.0/modules/guild/emblem.php?r=696
Can you help me.
EDIT::
A solution
from binascii import unhexlify
import zlib
import Image
from cStringIO import StringIO
data = StringIO(zlib.decompress(unhexlify(sdata)))
i = Image.open(data)
i.show()
The solution (in collaboration with OP) is:
import PIL
from binascii import unhexlify
import zlib
from cStringIO import StringIO
sdata = "789C9D953D56C52010856363696D49E90AAC73ECDD4396C25228B210CE711B2CC2CAC622CECC9D0C0321313A27E411123EEEFCC07B7BFF7A9CC45EA9BD507BD6F620F769CAF4FEE3096DB76DDACEAEE9865D4CF79C6DAB34F46D441F7F23F88F6F728E6AD794724EDD5CBB9B790EF53FBF1595D9524C517E93CDEA3A433D984E83440327B318B633BF867A4C12734A5654CE26F24F29AB28704A067685363C665B0582D30ADF0F39A2717F3979C9412A6108A1D731C6992C04BD96252ECB9A2AC4A60F2B07904AA8166C84B51545D172C3C8D02B4CA3D51D841F7584B5CD2E17E2698A5DDE991302AD6240189666558242122D68F1C0F19F99475104D0F7C6216D5A6665AFAED62F8A27730A57E3BC4858669D25716B387BA04E39B41059BCC7E99CEAF4B05F971C75AAB0181AE938111CA9DB9A71C9B5443EA000D4231183A4F8ECEF79E7E5B40E2DEF647BDEA9AB6250EA59F70B6AC90E9FAABFB7D040E43C010107D4F1086A4ADA6D8DA66C8AEDD9C10E3514196A0F060220B59825C843883F5D71A67586809FEDF17FFCD75C4CFC012B43550B"
fh = StringIO(zlib.decompress(unhexlify(sdata)))
image = PIL.Image.open(fh)
PIL is the Python Imaging Library, and using StringIO, I pretend to have a file-like object.
i have this script for pixelize my images the script is working but i want more smooth edges:
$imgfile = 'batman.jpg';
$image = ImageCreateFromJPEG($imgfile);
$imagex = imagesx($image);
$imagey = imagesy($image);
$pixelate_amount = 10;
$tmpImage = ImageCreateTrueColor($imagex, $imagey);
imagecopyresized($tmpImage, $image, 0, 0, 0, 0, round($imagex / $pixelate_amount), round($imagey / $pixelate_amount), $imagex, $imagey);
$pixelated = ImageCreateTrueColor($imagex, $imagey);
imagecopyresized($pixelated, $tmpImage, 0, 0, 0, 0, $imagex, $imagey, round($imagex / $pixelate_amount), round($imagey / $pixelate_amount));
header("Content-Type: image/jpeg");
imageJPEG($pixelated, "", 100);
I have:
this produce:
is there anything i miss?
Here's what you need (script I currently use). This script is based from the script at http://www.talkphp.com/19670-post1.html:
function convertToPixel($im, $size) {
$size = (int)$size;
$sizeX = imagesx($im);
$sizeY = imagesy($im);
if($sizeX < 3 && $sizeX < 3) { // or you can choose any size you want
return;
}
for($i = 0;$i < $sizeX; $i += $size) {
for($j = 0;$j < $sizeY; $j += $size) {
$colors = Array('alpha' => 0, 'red' => 0, 'green' => 0, 'blue' => 0, 'total' => 0);
for($k = 0; $k < $size; ++$k) {
for($l = 0; $l < $size; ++$l) {
if($i + $k >= $sizeX || $j + $l >= $sizeY) {
continue;
}
$color = imagecolorat($im, $i + $k, $j + $l);
imagecolordeallocate($im, $color);
$colors['alpha'] += ($color >> 24) & 0xFF;
$colors['red'] += ($color >> 16) & 0xFF;
$colors['green'] += ($color >> 8) & 0xFF;
$colors['blue'] += $color & 0xFF;
++$colors['total'];
}
}
$color = imagecolorallocatealpha($im, $colors['red'] / $colors['total'], $colors['green'] / $colors['total'], $colors['blue'] / $colors['total'], $colors['alpha'] / $colors['total']);
imagefilledrectangle($im, $i, $j, ($i + $size - 1), ($j + $size - 1), $color);
}
}
}
header('Content-type: image/jpg');
$im = imagecreatefromjpeg($imgfile);
convertToPixel($im, 15);
imagejpeg($im, '', 100);
This will produce:
You can also change the value passed in convertToPixel to modify the pixel size.
)
Use imagecopyresampled() instead of imagecopyresized().
http://php.net/manual/en/function.imagecopyresampled.php
Im trying to format the output of numbers in php. I have an amount of posts that show up, and next to each user is the total of posts. But it shows that actual amount, i want it to show it in a shorter format, actually, just like they do here at SO with reputation
any ideas?
<?
$numbers = array(100,1000,15141,3421);
function format_number($number) {
if($number >= 1000) {
return $number/1000 . "k"; // NB: you will want to round this
}
else {
return $number;
}
}
foreach($numbers as $number) {
echo $number . " : " . format_number($number);
echo "\n";
}
function count_format($n, $point='.', $sep=',') {
if ($n < 0) {
return 0;
}
if ($n < 10000) {
return number_format($n, 0, $point, $sep);
}
$d = $n < 1000000 ? 1000 : 1000000;
$f = round($n / $d, 1);
return number_format($f, $f - intval($f) ? 1 : 0, $point, $sep) . ($d == 1000 ? 'k' : 'M');
}
Use This
Shorten long numbers to K/M/B?
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
// 0.9k-850k
$n_format = number_format($n / 1000, $precision);
$suffix = 'K';
} else if ($n < 900000000) {
// 0.9m-850m
$n_format = number_format($n / 1000000, $precision);
$suffix = 'M';
} else if ($n < 900000000000) {
// 0.9b-850b
$n_format = number_format($n / 1000000000, $precision);
$suffix = 'B';
} else {
// 0.9t+
$n_format = number_format($n / 1000000000000, $precision);
$suffix = 'T';
}