PHP – Examples

intval()

pass a second parameter that specifies the base for the conversion <?php echo intval(‘123’, 10) . “\n<br>”;  //123 echo intval(‘101010’, 2) . “\n<br>”; // 42 echo intval(‘123’, 8) . “\n<br>”;  // 83 echo intval(‘123’, 16) . “\n<br>”;  // 291 echo… (READ MORE)

decoct() and octdec()

<?php $orig_dec = 789; $dec2oct = decoct($orig_dec); $oct2dec = octdec($dec2oct); echo “original decimal number: $orig_dec <br>”; // 789 echo “octal number: $dec2oct <br>”; //1425 echo “back to decimal: $oct2dec <br>”; // 789 ?>

Fill Rectangle

<html> <body> <?php $pic=ImageCreate(600,600); $col1=ImageColorAllocate($pic,200,200,200); $col2=ImageColorAllocate($pic,0,0,255); ImageFilledRectangle($pic,1,1,100,100,$col2); ImagePNG($pic,”pic.png”); ImageDestroy($pic); ?> <img src=”pic.png” border=0> </body> </html>

Set Font size

<?php $textImage = imagecreate(200,100); $white = imagecolorallocate($textImage, 255, 255, 255); $black = imagecolorallocate($textImage, 0, 0, 0); $yOffset = 0; for ($i = 1; $i <=5; $i++) { imagestring($textImage, $i, 5, $yOffset, “www.java2s.com $i”, $black); $yOffset += imagefontheight($i); } header(“Content-type: image/png”); imagepng($textImage); imagedestroy($textImage); ?>

Draw char with color

<html> <body> <?php $img=ImageCreate(200,200); $bgcolor=ImageColorAllocate($img,200,200,200); $red=ImagecolorAllocate($img,255,0,0); $green=ImagecolorAllocate($img,0,255,0); $blue=ImagecolorAllocate($img,0,0,255); $grey=ImagecolorAllocate($img,50,50,50); $black=ImagecolorAllocate($img,0,0,0); ImageChar($img,0,20,20,”S”,$red); ImageChar($img,1,20,40,”S”,$green); ImageChar($img,2,20,60,”S”,$blue); ImageChar($img,3,20,80,”S”,$grey); ImageChar($img,4,20,100,”S”,$black); ImageChar($img,5,20,120,”S”,$red); ImageCharUp($img,0, 20,160,”S”,$red); ImageCharUp($img,1, 40,160,”S”,$green); ImageCharUp($img,2, 60,160,”S”,$blue); ImageCharUp($img,3, 80,160,”S”,$grey); ImageCharUp($img,4,100,160,”S”,$black); ImageCharUp($img,5,120,160,”S”,$red); /* RGB Red = 0..255 , Green = 0..255 , Blue = 0..255. */ ImageSetPixel($img,50,50,$pixelcolor); ImagePNG($img,”pic.png”); ImageDestroy($img); ?> <img src=”pic.png” border=0> </body> </html>

Draw Filled Arc

<?php $myImage = ImageCreate(300,300); $white = ImageColorAllocate ($myImage, 255, 255, 255); $red  = ImageColorAllocate ($myImage, 255, 0, 0); $green = ImageColorAllocate ($myImage, 0, 255, 0); $blue = ImageColorAllocate ($myImage, 0, 0, 255); ImageFilledArc($myImage, 100, 100, 200, 150, 0, 90, $red, IMG_ARC_PIE); ImageFilledArc($myImage, 100, 100, 200, 150, 90, 180 , $green, IMG_ARC_PIE); ImageFilledArc($myImage, 100, 100,… (READ MORE)