Is there a maxium amount of "Draws" in PHP GD - php

As the title says is there a max amount of objects you can draw using the GD library in PHP. Given the code below i can only get about 140 squares to appear. The line and square at the bottom only show if you don't max out the grid first. Anyone know of another way to get this done?
<?php
header('Content-type: image/png');
$png_image = imagecreate(3000, 3000);
imagecolorallocate($png_image, 15, 142, 210);
$gridx = 100;
$gridy = 100;
$count = 0;
$currentx = 0;
$currenty = 0;
function makeRec($xpos, $ypos, $c1, $c2, $c3) {
global $png_image, $currentx, $currenty;
imagefilledrectangle($png_image, $xpos, $ypos, $xpos+10, $ypos+10, imagecolorallocate($png_image, $c1, $c2, $c3));
$currentx = $xpos+11;
}
function makexLine($xpos, $ypos) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos, $ypos+10, imagecolorallocate($png_image, 0, 0, 0));
$currentx = $xpos+1;
}
function makeyLine($xpos, $ypos) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos+100, $ypos, imagecolorallocate($png_image, 0, 0, 0));
$currenty = $ypos+1;
}
for($y = 0; $y < $gridy; $y++) {
makeyline($currentx, $currenty);
for($x = 0; $x < $gridx; $x++) {
makexLine($currentx, $currenty);
if($count == 0) {
makeRec($currentx, $currenty, 255, 1, 255);
$count++;
}
else {
makeRec($currentx, $currenty, 255, 255, 255);
$count = 0;
}
}
makexLine($currentx, $currenty);
$currentx = 0;
$currenty = $currenty + 11;
}
makeyline(0, 200);
makeRec(200, 200, 255, 1, 255);
imagepng($png_image);
imagedestroy($png_image);
?>

Running a test script on my system that draws 1 million squares with imagefilledrectangle() in a chessboard pattern works fine (and surprisingly quickly). If there is a draw limit it's at least that high.
Your problem seems to be too many calls to imagecolorallocate(). Below I've modified your code to call imagecolorallocate() only once per colour:
header('Content-type: image/png');
$png_image = imagecreate(3000, 3000);
imagecolorallocate($png_image, 15, 142, 210);
$magenta = imagecolorallocate($png_image, 255, 1, 255);
$black = imagecolorallocate($png_image, 0, 0, 0);
$white = imagecolorallocate($png_image, 255, 255, 255);
$gridx = 100;
$gridy = 100;
$count = 0;
$currentx = 0;
$currenty = 0;
function makeRec($xpos, $ypos, $colour) {
global $png_image, $currentx, $currenty;
imagefilledrectangle($png_image, $xpos, $ypos, $xpos+10, $ypos+10, $colour);
$currentx = $xpos+11;
}
function makexLine($xpos, $ypos, $colour) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos, $ypos+10, $colour);
$currentx = $xpos+1;
}
function makeyLine($xpos, $ypos, $colour) {
global $png_image, $currentx, $currenty;
imageline($png_image, $xpos, $ypos, $xpos+100, $ypos, $colour);
$currenty = $ypos+1;
}
for($y = 0; $y < $gridy; $y++) {
makeyline($currentx, $currenty, $black);
for($x = 0; $x < $gridx; $x++) {
makexLine($currentx, $currenty, $black);
if($count == 0) {
makeRec($currentx, $currenty, $magenta);
$count++;
}
else {
makeRec($currentx, $currenty, $white);
$count = 0;
}
}
makexLine($currentx, $currenty, $black);
$currentx = 0;
$currenty = $currenty + 11;
}
makeyline(0, 200, $black);
makeRec(200, 200, $magenta);
imagepng($png_image);
imagedestroy($png_image);

Related

Try a simple captcha in CI4

I want to implement a simple captcha in ci4, I try to search a sample code in google and then copas it. But when run the code get result like this. Please help what wrong in my code ?
My controller is :
private function captcha()
{
$i = 0;
$imgHeight = 80;
$imgWidth = 250;
$randTotal = 7;
$randomDots = 50;
$randomLines = 25;
$font = realpath('./fonts/monofont.ttf');
$random = '';
$captTextColor = "0x142864";
$noiseColor = "0x142864";
$fontSize = $imgHeight * 0.65;
$random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0,
$randTotal);
$image = #imagecreate($imgWidth, $imgHeight);
$arrTextColor = $this->hexToRGB($captTextColor);
$arrNoiseColor = $this->hexToRGB($noiseColor);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$captTextColor = imagecolorallocate($image, $arrTextColor['red'], $arrTextColor['green'], $arrTextColor['blue']);
$imgNoiseColor = imagecolorallocate($image, $arrNoiseColor['red'], $arrNoiseColor['green'], $arrNoiseColor['blue']);
// Cetak titik acak di latar belakang gambar
for ($i = 0; $i < $randomDots; $i++) {
imagefilledellipse($image, mt_rand(0, $imgWidth), mt_rand(0, $imgHeight), 2, 3, $imgNoiseColor);
}
// Cetak garis acak di latar belakang gambar
for ($i = 0; $i < $randomLines; $i++) {
imageline(
$image,
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
mt_rand(0, $imgWidth),
mt_rand(0, $imgHeight),
$imgNoiseColor
);
}
// Cetak kotak teks dan tambahkan 6 kode huruf captcha
$textBox = imagettfbbox($fontSize, 0, $font, $random);
$x = ($imgWidth - $textBox[4]) / 2;
$y = ($imgHeight - $textBox[5]) / 2;
$_SESSION['captcha'] = $random;
// imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
imagettftext($image, $fontSize, 0, $x, $y, $captTextColor, $font, $random);
header('Content-Type: image/jpeg');
imagejpeg($image, null, null);
imagedestroy($image);
}
Thank you
Thank you.
Try adding the exit; command after the imagedestroy($image); line

Watermarking an existing PDF in Drupal

I'm working on a project in DRUPAL and am trying to programatically add an image to an existing pdf - an "Approved" image for invoices. I've Frankensteined the code from the StackExchange post - How can I add a watermark to an existing PDF file using PHP? - and have managed to get it working outside of Drupal (7.0) but once I port the code over, I get nothing, no errors, no warnings, nadda .... Just wondering if someone out there can help me on this issue, it been driving me nuts for a bit now .... Sorry about the sloppy code....
<?php
global $user;
ob_start();
$filename ="approved_".$user->name.".png";
$file = "2pages.pdf";
$op = 100;
////////////////////////////////////////////////////////////////////////////
require('fpdf.php');
require('fpdi.php');
$text = "Text message for watermark goes here";
$name = uniqid();
$font_size = 5;
$ts=explode("\n",$text);
$width=0;
foreach ($ts as $k=>$string) {
$width=max($width,strlen($string));
}
$width = imagefontwidth($font_size)*$width;
$height = imagefontheight($font_size)*count($ts);
$el=imagefontheight($font_size);
$em=imagefontwidth($font_size);
$img = imagecreatetruecolor($width,$height);
// Background color
$bg = imagecolorallocate($img, 255, 255, 255);
imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
// Font color
$color = imagecolorallocate($img, 0, 0, 0);
foreach ($ts as $k=>$string) {
$len = strlen($string);
$ypos = 0;
for($i=0;$i<$len;$i++){
$xpos = $i * $em;
$ypos = $k * $el;
imagechar($img, $font_size, $xpos, $ypos, $string, $color);
$string = substr($string, 1);
}
}
imagecolortransparent($img, $bg);
$blank = imagecreatetruecolor($width, $height);
$tbg = imagecolorallocate($blank, 255, 255, 255);
imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg);
imagecolortransparent($blank, $tbg);
if ( ($op < 0) OR ($op >100) ){
$op = 100;
}
imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op);
$pdf = new FPDI();
//********************************************************************
if (file_exists($file)){
$pagecount = $pdf->setSourceFile($file);
} else {
return FALSE;
}
/////////////////////////////////////////
for($i=1; $i <= $pagecount; $i++) {
$tpl = $pdf->importPage($i);
$pdf->addPage();
$pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
//Put the watermark
$pdf->Image($filename, 0, 0, 40, 25, 'png');}
/////////////////////////////////////////
return $pdf->Output('test10.pdf', 'F');
////////////////////////////////////////////////////////////////////////////
ob_end_flush();
?>
Of note, I am also open you anyone who may have an alternate method to achieve the same end result
thanks in advance!
So after taking some advise from Marc B, I went through the code and isolated the issue. For those looking for the answer to this question, here's the solution I came up with ... Hope it helps!
<?php
ob_start();
function PlaceWatermark($file, $text, $xxx, $yyy, $op, $outdir) {
global $user;
require('fpdf.php');
require('fpdi.php');
$myfilename = 'approved_'.$user->name.'.png';
// Created Watermark Image
$pdf = new FPDI();
//if (file_exists("./".$file)){
$pagecount = $pdf->setSourceFile($file);
//} else {
// return FALSE;
//}
/////////////////////////////////////////
for($i=1; $i <= $pagecount; $i++) {
$tpl = $pdf->importPage($i);
$pdf->addPage();
$pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
//Put the watermark
if (file_exists($myfilename)) {
$pdf->Image($myfilename, 0, 0, 40, 25, 'png');
} else {
$pdf->Image('approved.png', 0, 0, 40, 25, 'png');
}
}
/////////////////////////////////////////
return $pdf->Output('test_output.pdf', 'F');
}
PlaceWatermark("Original.pdf", "", 30, 120, 100,TRUE);
ob_end_flush();
?>

imagecopyresampled after rotating the image

I am trying to copy image (to $img) after rotating image (many images $im) but I get weird behavior. Once I un-comment the line //$img = I only get the rotated image on my final output. Can I rotate the inner $im and copy it to final image$img?
<?php
$height = 80;
$width = 300;
$img = imagecreate($width, $height);
$c = imagecolorallocate ($img , 135, 135, 135);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
for($i=0; $i<=5; $i++){
$im = imagecreatetruecolor(35, 35);
$gry = imagecolorallocate($im, 135, 135, 135);
$wht = imagecolorallocate($im, 255, 255, 255);
$j = mt_rand(0, 1);
$ch = mt_rand(0,1)?chr(rand(65, 90)):chr(rand(97, 122));
if($j == 0){
imagefill($im, 0, 0, $wht);
imagefttext($im, 20, 0, 3, 21, $gry, 'AHGBold.ttf', $ch);
//$img = imagerotate($im, mt_rand(0,10)-5, $wht);
}else{
imagefill($im, 0, 0, $gry);
imagefttext($im, 20, 0, 3, 21, $wht, 'AHGBold.ttf', $ch);
//$img = imagerotate($im, mt_rand(0,10)-5, $gry);
}
imagecopyresampled($img, $im, 5 + $i*42, $height/2 - 12, 0, 0, 40, 40, 25, 25);
}
header('Content-type: image/png');
imagepng($img);
change
$img = imagerotate($im, mt_rand(0,10)-5, $wht);
and
$img = imagerotate($im, mt_rand(0,10)-5, $gry);
to
$im = imagerotate($im, mt_rand(0,10)-5, $wht);
and
$im = imagerotate($im, mt_rand(0,10)-5, $gry);
in cases that imagerotes does not work you can use the following function to rotate an image:
function imagerotateEquivalent(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0)
{
$srcw = imagesx($srcImg);
$srch = imagesy($srcImg);
if($angle == 0) return $srcImg;
// Convert the angle to radians
$theta = deg2rad ($angle);
// Calculate the width of the destination image.
$temp = array ( rotateX(0, 0, 0-$theta),
rotateX($srcw, 0, 0-$theta),
rotateX(0, $srch, 0-$theta),
rotateX($srcw, $srch, 0-$theta)
);
$minX = floor(min($temp));
$maxX = ceil(max($temp));
$width = $maxX - $minX;
// Calculate the height of the destination image.
$temp = array ( rotateY(0, 0, 0-$theta),
rotateY($srcw, 0, 0-$theta),
rotateY(0, $srch, 0-$theta),
rotateY($srcw, $srch, 0-$theta)
);
$minY = floor(min($temp));
$maxY = ceil(max($temp));
$height = $maxY - $minY;
$destimg = imagecreatetruecolor($width, $height);
imagefill($destimg, 0, 0, imagecolorallocate($destimg, 0,255, 0));
// sets all pixels in the new image
for($x=$minX;$x<$maxX;$x++) {
for($y=$minY;$y<$maxY;$y++)
{
// fetch corresponding pixel from the source image
$srcX = round(rotateX($x, $y, $theta));
$srcY = round(rotateY($x, $y, $theta));
if($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch)
{
$color = imagecolorat($srcImg, $srcX, $srcY );
}
else
{
$color = $bgcolor;
}
imagesetpixel($destimg, $x-$minX, $y-$minY, $color);
}
}
return $destimg;
}
function rotateX($x, $y, $theta){
return $x * cos($theta) - $y * sin($theta);
}
function rotateY($x, $y, $theta){
return $x * sin($theta) + $y * cos($theta);
}
I got the above code from a note in php.net

Php captcha session variable not found

I have this captcha.php file that I load on the page I want the captcha to appear on using jQuery load.
In the captcha.php file I start a new session and I set a variable (theCaptchaCode) which contains the code you see on the image, but when I try to get the session variable with $theCaptchaCode = $_SESSION['theCaptchaCode']. It says
undefined index: theCaptchaCode.
Captcha.php code:
<?php
//Killing previous session
session_start();
session_unset();
session_destroy();
?>
<?php
session_start();
$theCaptchaCode = "";
$thisCaptchaImg = imagecreatetruecolor(200, 50);
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0);
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46);
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255);
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor);
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';
$length = strlen($characters);
for ($l = 0; $l < 4; $l++)
{
imageline($thisCaptchaImg, 0, rand() % 50, 200, rand() % 50, $color);
}
for ($d = 0; $d < 1500; $d++)
{
imagesetpixel($thisCaptchaImg, rand() % 200, rand() % 50, $dotColor);
}
for ($c = 0; $c < 7; $c++)
{
$selCharacter = $characters[rand(0, $length - 1)];
imagestring($thisCaptchaImg, 5, 5 + ($c * 30), 20, $selCharacter, $color);
$theCaptchaCode .= $selCharacter;
}
imagepng($thisCaptchaImg, 'thisCaptchaImage.png');
$_SESSION['theCaptchaCode'] = $theCaptchaCode;
session_destroy();
?>
At the end of your code remove the session_destroy() method.
Edited:
Use the below code:
<?php session_start();
$theCaptchaCode = "";
$thisCaptchaImg = imagecreatetruecolor(200, 50);
$color = imagecolorallocate($thisCaptchaImg, 0, 0, 0);
$dotColor = imagecolorallocate($thisCaptchaImg, 46, 46, 46);
$backgroundColor = imagecolorallocate($thisCaptchaImg, 255, 255, 255);
imagefilledrectangle($thisCaptchaImg, 0, 0, 200, 70, $backgroundColor);
$characters = '123456789QWERTYUIOPASDFGHJKLZXCVBNM';
$length = strlen($characters);
for($l=0;$l < 4;$l++)
{
imageline($thisCaptchaImg, 0, rand()%50, 200, rand()%50, $color);
}
for($d=0;$d < 1500;$d++)
{
imagesetpixel($thisCaptchaImg, rand()%200, rand()%50, $dotColor);
}
for($c=0;$c < 7;$c++)
{
$selCharacter = $characters[rand(0, $length-1)];
imagestring($thisCaptchaImg, 5, 5+($c*30), 20, $selCharacter, $color);
$theCaptchaCode .= $selCharacter;
}
imagepng($thisCaptchaImg, 'thisCaptchaImage.png');
$_SESSION['theCaptchaCode'] = $theCaptchaCode;
?>

Trying to add text into an image

I'm trying to add 2 line of text. One for the name of the person and the other for the name of the clan.
Could you help me of what I'm doing wrong? I don't get any error message or anything and the image is loading fine.
This is my code:
//Ribbons Image
$ribbons = array(
"ribbons/1STAID.png",
"ribbons/BMT.png",
"ribbons/BRA1A.png",
"ribbons/CCMD.png",
"ribbons/CCMDV2.png",
"ribbons/donator.png",
"ribbons/FTR.png",
"ribbons/GC.png",
"ribbons/GRA1A.png",
"ribbons/IOTP.png",
"ribbons/MEDIC.png",
"ribbons/PHA1A.png",
"ribbons/PILOT.png",
"ribbons/RCT1A.png",
"ribbons/RCT2A.png",
"ribbons/RCT3A.png",
"ribbons/RCT4A.png",
"ribbons/SRA1A.png",
"ribbons/SVR1A.png",
"ribbons/SVR2A.png",
"ribbons/SVR3A.png",
"ribbons/SVR4A.png",
"ribbons/SVR-ALTIS.png",
"ribbons/XOCMD.png",
"ribbons/XOCMDV2.png");
//Rank Image
$rank = imagecreatefrompng("rank/2LT.png");
//Background
$frame = imagecreatefrompng("sign3.png");
//imagecopymergy(output,image,x,y,0,0,w,h,100)
//Adding rank
imagecopymerge($frame, $rank, 30, 30, 0, 0, 10, 25, 100);
//Trying to add the text on the image
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Test...';
$font = 'arial.ttf';
imagettftext($im, 20, 0, 10, 20, $white, $font, $text);
//Adding the ribbon on the image
$z = 0;
$i = 0;
for ($y = 0; $y <= 120; $y += 20) {
$z++;
for ($x = 0; $x <= 150; $x += 50) {
if ($i <= 24) {
$rib = imagecreatefromjpeg($ribbons[$i]);
imagecopymerge($frame, $rib, $x + 375, $y + 50, 0, 0, 50, 20, 100);
}
$i++;
}
}
//Save the image + Display
imagepng($frame, 'generate/test.png');
imagepng($frame);
header('Content-Type: image/png');
Thanks for helping me!
Working code:
//Ribbons Image
$ribbons = array(
"ribbons/1STAID.png",
"ribbons/BMT.png",
"ribbons/BRA1A.png",
"ribbons/CCMD.png",
"ribbons/CCMDV2.png",
"ribbons/donator.png",
"ribbons/FTR.png",
"ribbons/GC.png",
"ribbons/GRA1A.png",
"ribbons/IOTP.png",
"ribbons/MEDIC.png",
"ribbons/PHA1A.png",
"ribbons/PILOT.png",
"ribbons/RCT1A.png",
"ribbons/RCT2A.png",
"ribbons/RCT3A.png",
"ribbons/RCT4A.png",
"ribbons/SRA1A.png",
"ribbons/SVR1A.png",
"ribbons/SVR2A.png",
"ribbons/SVR3A.png",
"ribbons/SVR4A.png",
"ribbons/SVR-ALTIS.png",
"ribbons/XOCMD.png",
"ribbons/XOCMDV2.png");
//Rank Image
$rank = imagecreatefrompng("rank/2LT.png");
//Background
$frame = imagecreatefrompng("sign3.png");
//imagecopymergy(output,image,x,y,0,0,w,h,100)
//Adding rank
imagecopymerge($frame, $rank, 30, 30, 0, 0, 10, 25, 100);
//Trying to add the text on the image
$im = imagecreatetruecolor(50, 30);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 0, 82, 255);
imagefilledrectangle($im, 4, 4, 50, 25, $white);
$text = 'Nato Military Corp';
$font = 'arial.ttf';
imagettftext($frame, 12, 0, 450, 220, $white, $font, $text);
//imagecopymerge($frame, $im, 10, 20, 0, 0, 10, 25, 100);
//Adding the ribbon on the image
$z = 0;
$i = 0;
for ($y = 0; $y <= 120; $y += 20) {
$z++;
for ($x = 0; $x <= 150; $x += 50) {
if ($i <= 24) {
$rib = imagecreatefromjpeg($ribbons[$i]);
imagecopymerge($frame, $rib, $x + 375, $y + 50, 0, 0, 50, 20, 100);
}
$i++;
}
}
//Save the image + Display
imagepng($frame, 'generate/test.png');
imagepng($frame);
header('Content-Type: image/png');

Categories