I am creating image with php function imagesetpixel() there I could set starting x position and y position, but if I have it in code:
for ($i=1;$i<50;$i++)
{
for ($a=1;$a<50;$a++)
{
imagesetpixel($img, $i, $a, $color);
}
}
this create every field 1x1px but I want it maybe 5x5px. is it possible to make something like it?
see below url:-
http://phptutorial.info/?imagesetpixel
http://php.net/manual/en/function.imagesetpixel.php
example:-
<?php
$x = 200;
$y = 200;
$gd = imagecreatetruecolor($x, $y);
$corners[0] = array('x' => 100, 'y' => 10);
$corners[1] = array('x' => 0, 'y' => 190);
$corners[2] = array('x' => 200, 'y' => 190);
$red = imagecolorallocate($gd, 255, 0, 0);
for ($i = 0; $i < 100000; $i++) {
imagesetpixel($gd, round($x),round($y), $red);
$a = rand(0, 2);
$x = ($x + $corners[$a]['x']) / 2;
$y = ($y + $corners[$a]['y']) / 2;
}
header('Content-Type: image/png');
imagepng($gd);
?>
The size of your image you have to pass as arguments to the - for instance [imagecreate][1] function.
Example:
$width = 5;
$height = 5;
$img = imagecreate($width, $height);
Related
How can I calculate the variation of an image in Imagick? I already converted the image in greyscale, but through native functions I can only get Mean and Standard Deviation.
My approach is generating two noise images and calculate the Euclidian difference between both RGB pixel color values.
This will certainly look better with real images for example of a webcam.
$getRandomNoiseImage = function ($x, $y, $steps): Imagick {
$draw = new ImagickDraw();
$draw->setResolution($x, $y);
$draw->setStrokeWidth(1);
$pixel = new ImagickPixel();
for ($i = 0; $i < $x; $i += $steps) {
for ($j = 0; $j < $y; $j += $steps) {
$color = join(',', [rand(0, 255), rand(0, 255), rand(0, 255)]);
$pixel->setColor("rgb($color)");
$draw->setFillColor($pixel);
$draw->setStrokeColor($pixel);
$draw->rectangle($i, $j, $i + $steps, $j + $steps);
}
}
$im = new Imagick();
$im->newImage($x, $y, new ImagickPixel('black'));
$im->drawImage($draw);
return $im;
};
$width = 64;
$height = 64;
$im1 = $getRandomNoiseImage($width, $height, 16);
$im2 = $getRandomNoiseImage($width, $height, 16);
$draw = new ImagickDraw();
$draw->setResolution($width, $height);
$draw->setStrokeWidth(1);
$pixel = new ImagickPixel();
for ($i = 0; $i < $width; $i++) {
for ($j = 0; $j < $height; $j++) {
$c1 = $im1->getImagePixelColor($i, $j)->getColor(0);
$c2 = $im2->getImagePixelColor($i, $j)->getColor(0);
$color = [
sqrt(abs($c2['r']**2 - $c1['r']**2)),
sqrt(abs($c2['g']**2 - $c1['g']**2)),
sqrt(abs($c2['b']**2 - $c1['b']**2)),
];
$pixel->setColor(sprintf('rgb(%d,%d,%d)', ...$color));
$draw->setFillColor($pixel);
$draw->setStrokeColor($pixel);
$draw->rectangle($i, $j, $i + 1, $j + 1);
}
}
$im3 = new Imagick();
$im3->newImage($width, $height, new ImagickPixel('black'));
$im3->drawImage($draw);
$im1->writeImage('image1.png');
$im2->writeImage('image2.png');
$im3->writeImage('image3.png');
I have a script that crops and rotates an image.
But i end up with a black bar in my image:
http://prntscr.com/r9l1w5 (screenshot)
I found a script on the internet, that is able to remove black bars on images. But i am unable to make it work together on 1 page.
(view-image.php) My script:
<?php
$filenamegetter = $_GET['imgid'];
$degree = 0;
// File and rotation
$filename = 'img/' . $filenamegetter . '';
$degrees = $degree;
$percent = 0.30;
// Content type
header('Content-type: image/jpeg');
// Get new sizes
list($width, $height) = getimagesize($filename);
if ($width > "1000") {
$newwidth = $width * $percent;
$newheight = $height * $percent;
} else {
$newwidth = $width;
$newheight = $height;
}
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
imagecopyresized($thumb, $rotate, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb);
// Free the memory
imagedestroy($source);
imagedestroy($thumb);
?>
The anti black bar script i wish it works together with:
<?php
$image_path = "image.jpg";
$jpg = imagecreatefromjpeg($image_path);
$black = array("red" => 0, "green" => 0, "blue" => 0, "alpha" => 0);
$removeLeft = 0;
for($x = 0; $x < imagesx($jpg); $x++) {
for($y = 0; $y < imagesy($jpg); $y++) {
if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
break 2;
}
}
$removeLeft += 1;
}
$removeRight = 0;
for($x = imagesx($jpg)-1; $x > 0; $x--) {
for($y = 0; $y < imagesy($jpg); $y++) {
if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
break 2;
}
}
$removeRight += 1;
}
$removeTop = 0;
for($y = 0; $y < imagesy($jpg); $y++) {
for($x = 0; $x < imagesx($jpg); $x++) {
if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
break 2;
}
}
$removeTop += 1;
}
$removeBottom = 0;
for($y = imagesy($jpg)-1; $y > 0; $y--) {
for($x = 0; $x < imagesx($jpg); $x++) {
if(imagecolorsforindex($jpg, imagecolorat($jpg, $x, $y)) != $black){
break 2;
}
}
$removeBottom += 1;
}
$cropped = imagecreatetruecolor(imagesx($jpg) - ($removeLeft + $removeRight), imagesy($jpg) - ($removeTop + $removeBottom));
imagecopy($cropped, $jpg, 0, 0, $removeLeft, $removeTop, imagesx($cropped), imagesy($cropped));
header("Content-type: image/jpeg");
imagejpeg($cropped); //change to `imagejpeg($cropped, $image_path);` to save
imagedestroy($cropped);
imagedestroy($jpg);
I'd like to know how i can implent the anti black bar script in my script. I tried multiple ways but i end up with a empty page.
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
There are a lot of factors but for me the problem was the new size after rotating the image.
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);
so with this code it produces an X
$pdf->SetDrawColor(0, 0, 0);
$pdf->SetLineWidth(2.0);
$pdf->Line($x, $y, $x + $w, $y + $h);
$pdf->Line($x, $y + $h, $x + $w, $y);
How can I change the two parts that say $pdf->Line(); so I can make it produce a check mark instead?
here this is if it helps aswell
if (isset($json->pages[$i]) && isset($json->pages[$i]->areas)) {
for ($j = 0; $j < count($json->pages[$i]->areas); $j++) {
$area = $json->pages[$i]->areas[$j];
$x = $area->x;
$y = $area->y;
$w = $area->width;
$h = $area->height;
I was hoping to get in touch with someone on a situation that I cannot find the solution to anywhere.
I am trying to create a captcha on my website using php and although I was able to create an image and create the random captcha text.
I am unable to over lay the two. Here is my code:
<?PHP
session_start();
function generateRandomString($length = 10) {
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
}
function security_image(){
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
//$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
// $image = #imagecreate($width, $height) or die('GD not installed');
global $image;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
//$textbox = imagettfbbox($font_size, 0, $font, $code);
//$textbox = imagettfbbox($font_size, 0, $randomString);
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
// imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
imagettftext($image, $font_size, 0, $x, $y, $text_color , $word);
$images = glob("*.png");
foreach ($images as $image_to_delete) {
#unlink($image_to_delete);
}
imagepng($image, "image" . $_SESSION['count'] . ".png");
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();
?>
I have no idea what I’m doing wrong. I’ve spent over 10 hours on this “display text” issue. I don’t understand and I am desperate for help. I even downloaded working captcha version from other resources that break once I upload it to my server. I have no idea whats going on. At first I thought there was something wrong with my server but the fact that I can even create the pixels, lines image means that it is at least working.
Please help!!!!
UPDATE---------------------------------------------
Thank you for your suggestions. Here is the edited code. I'm still getting the same issue.
<?PHP
session_start();
function security_image(){
global $image;
// $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code();
$font = 'content/fonts/comic.ttf';
$width = '110';
$height = '20';
$font_size = $height * 0.75;
$image = imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,200,50,$background_color);
$line_color = imagecolorallocate($image, 64,64,64);
for($i=0;$i<10;$i++) {
imageline($image,0,rand()%50,200,rand()%50,$line_color);
}
$pixel_color = imagecolorallocate($image, 0,0,255);
for($i=0;$i<1000;$i++) {
imagesetpixel($image,rand()%200,rand()%50,$pixel_color);
}
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$len = strlen($letters);
$letter = $letters[rand(0, $len - 1)];
$text_color = imagecolorallocate($image, 0, 0, 0);
$word = "";
for ($i = 0; $i < 6; $i++) {
$letter = $letters[rand(0, $len - 1)];
imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
$word .= $letter;
}
$_SESSION['captcha_string'] = $word;
/*texbox unitinitialized (removed for the sake of just showing the word size doesnt matter)
$x = ($width - $textbox[4]) / 2;
$y = ($height - $textbox[5]) / 2;
*/
$x = ($width) / 2;
$y = ($height) / 2;
imagettftext($image,$font, 4, $x, $y, $word);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
security_image();?>
i made some suggested changes but the code seems to still do the same thing. Just display the lines and pixels as expected but the text still is missing... ?
There are some several "errors" in your functions, let's fix them:
In generateRandomString()
generateRandomString($length = 10)
$lenght is not used its scope.
$text_color = imagecolorallocate($image, 0, 0, 0);
$image is uninitialized
In security_image() function:
$textbox is uninitialized
$text_color and $word is uninitialized.
And Wrong parameter count for imagettftext() You add 7 parameters, and forget the font file parameter.
found the problem. using this:
http://php.net/manual/en/function.imagettftext.php
i was able to see that the font location was incorrect. using the example on that page and editing it to my needs worked.
I recently read about graphic design extensions in PHP and I am trying to create a square with different colors. Example.
Here is my program:
<?php
$color = array(0 => array('35', '3B', '1A'),
1 => array('7E', 'A6', '29'),
2 => array('D9', 'C9', '9A'),
3 => array('D9', '30', '30'),
4 => array('73', '07', '10'),
5 => array('D9', '62', 'C6')
);
$image = imagecreate(200,200);
$maxsize = 200;
$currentcolor = 0;
$yellow = imagecolorallocate($image, 0xFF, 0xFF, 0x00);
for($i = 0; $i <= 200; $i += 10) {
if($currentcolor == 6) {
$currentcolor = 0;
}
$red = "0x".$color[$currentcolor][0];
$green = "0x".$color[$currentcolor][1];
$blue = "0x".$color[$currentcolor][2];
$red = (int)$red;
$green = (int)$green;
$blue = (int)$blue;
$rescolor = imagecolorallocate($image, $red, $green, $blue);
imagefilledrectangle($image, $i, $i, $maxsize -= 10, $maxsize -= 10, $rescolor);
$currentcolor++;
}
header("Content-Type: image/png");
imagepng($image);
?>
However, this code produces only a black square. How can I make the square multicolored?
These two arguments are almost CERTAINLY not what you want:
imagefilledrectangle($image, $i, $i, $maxsize -= 10, $maxsize -= 10, $rescolor);'
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
This code is actually functioning as:
$maxsize = $maxsize - 10;
$foo = $maxsize;
$maxsize = $maxsize - 10;
$bar = $maxsize;
imagefilledrectangle($image, $i, $i, $foo, $bar, $rescolor);