...

View Full Version : Get part of screen as image



chekura
02-25-2007, 01:15 PM
Hello all :) I'm new one here... Hope you'll help newbie programmer...

Is there any way in PHP to get part of browser screen as JPEG (Or any other type) image?

To concrete:

Using this code



Function GenHexagons($R, $CountX, $CountY, $Left, $Top){
$StepX = 1.5 * $R;
$stepY = Sqrt(3) * $R;

For($i = 0; $i < $CountX; $i++){
$X = $Left + Round( $StepX * $i );

For($j = 0; $j < $CountY; $j++){
$Y = $Top + Round( $stepY * ( $j + 0.5 * ($i % 2)));
// echo '<p><a href="'. $Y .'">Y = '.$Y.'</a></p>';
// echo '<p><a href="'. $X .'">X = '.$X.'</a></p>';
echo "<img src=\"hex.png\" style=\"position: absolute; top:".$Y."px; left:".$X."px; \" alt=\"\" />" ;
}
}

}

GenHexagons(21, 5, 5, 50, 0);


I get somthing like this:

http://www.geomedia.ge/screen123.PNG


PHP code makes so, that hexagon image is repeating horizontally and vertically and I get many images. But it would be great to combine them as one file image... Any idea? :confused:


Thanks :)

westmatrix99
02-25-2007, 01:57 PM
What is this for

GenHexagons(21, 5, 5, 50, 0);

Cheers

Sandrix
02-25-2007, 02:34 PM
westmatrix99

This are function arguments :)

GenHexagons(21, 5, 5, 50, 0); = GenHexagons($R, $CountX, $CountY, $Left, $Top)

where R = Hexagons side, Countx= Number of Hexagons to be displayed horizontally, CountY= Number of Hexagons to be displayed vertically, Left= Left Margin, Top = top margin.

You must call the funtction to make it work, otherwise it wont work.
cheers

westmatrix99
02-25-2007, 02:38 PM
Then make it

GenHexagons(21, 1, 1, 50, 0);

That should solve so many of them showing and only one should show.

Sandrix
02-25-2007, 03:28 PM
westmatrix99

Lol :)) I'm not sure that you understand the code that chekura provided .

coz if he does what you say then the function will show only one hexagon, but chekura wants to generate the hexagonal grid as one image, not just <img scr="hex.gif" /> lol... he didn't mention that he wants to display a hexagon, what he meant is that he wants to generate an image filled with hexagons.. in other words he wants to merge all those hexagons in to one and display them as one image. otherwise why whould he use this function lol? to display one image? :D

Sandrix
03-02-2007, 07:11 PM
so? :) any1?

I need this too

I've got an Idea but I don't know how to make it work

Ok here is the image watermark scritpt:


<?

class watermark {
public static function emboss($pic1, $patt) {

header("Content-type: image/png");
$source = imagecreatefromjpeg($pic1); // Source
$pattern = imagecreatefromjpeg($patt); // pattern

list ($width, $height) = getimagesize($pic1);
list ($widthpatt, $heightpatt) = getimagesize($patt);

$new_width = $width;
$new_height = $width / $widthpatt * $heightpatt;

if ($new_height > $height) {
$offset = intval(($new_height - $height) / 2);
}elseif ($new_height < $height) {
$offset = intval(($new_height - $height) / 2);
} else {
$offset = 0;
}

$image_p = imagecreatetruecolor($new_width, $new_height);
$pattern = imagecreatefromjpeg($patt);
imagecopyresampled($image_p, $pattern, 0, 0, 0, 0, $new_width, $new_height, $widthpatt, $heightpatt);

$im = imagecreatetruecolor($width, $height);
for ($y = 0; $y < $height; $y ++) {
for ($x = 0; $x < $width; $x ++) {
$colors = imagecolorsforindex($source, imagecolorat($source, $x, $y));
$pattern_color = imagecolorsforindex($image_p, imagecolorat($image_p, $x, ($y + $offset)));

//changes brightness depending on luminance
if (($y + $offset +1) > 0 && ($y + $offset) < ($new_height -1)) {
$brightness = abs(($pattern_color['red'] * 50 / 255) - 50);
if ($pattern_color['red'] < 150) {
$change = true;
} else {
$change = false;
$tally = 0;
}

if ($change && $tally < 2) {
$highlight = 1.8;
$tally ++;
} else {
$highlight = 1;
}
$brightness = $brightness * $highlight;
} else {
$brightness = 0;
}
$r = (($colors['red'] + $brightness) > 255) ? 255 : ($colors['red'] + $brightness);
$g = (($colors['green'] + $brightness) > 255) ? 255 : ($colors['green'] + $brightness);
$b = (($colors['blue'] + $brightness) > 255) ? 255 : ($colors['blue'] + $brightness);
$test = imagecolorallocate($im, $r, $g, $b);
imagesetpixel($im, $x, $y, $test);
}
}

imagepng($im);
imagedestroy($im);
}
}

watermark::emboss("apply-to-image.jpg", "pattern.jpg");

?>

and maybe someone(php guru) could make it to work with GenHex() function?
I mean, that some background shape(or image) will be watermarked with hexagons so the result would be the single image. :o

westmatrix99
03-02-2007, 07:22 PM
How big can you make this image?
What I mean is, if you take one hexagon image how big can you make it.
Then add the other images repeating horizontally and vertically inside that big one.
How would that code look?
Cheers

Inigoesdr
03-02-2007, 07:26 PM
Hello all :) I'm new one here... Hope you'll help newbie programmer...

Is there any way in PHP to get part of browser screen as JPEG (Or any other type) image?

No. PHP is server-side only. To alter an image with PHP you would have to have post it to a server with PHP or the image file would have to be on the server.

Sandrix
03-03-2007, 09:59 PM
How big can you make this image?
What I mean is, if you take one hexagon image how big can you make it.
Cheers


As big as I want I think :) Function is universal.

BTW



Then add the other images repeating horizontally and vertically inside that big one.
How would that code look?



That's what I said in previous post.. :) to generate an image and then watermark it with my hexagons, using the algorithm provided in first post.

But how?

Armondo
03-03-2007, 10:32 PM
nevermind...i just read the above posts.

chump2877
03-04-2007, 12:09 AM
Quote:
Originally Posted by chekura http://www.codingforums.com/images/buttons/viewpost.gif (http://www.codingforums.com/showthread.php?p=538239#post538239)
Hello all :) I'm new one here... Hope you'll help newbie programmer...

Is there any way in PHP to get part of browser screen as JPEG (Or any other type) image?

No. PHP is server-side only. To alter an image with PHP you would have to have post it to a server with PHP or the image file would have to be on the server.I don;t know enough about PHP's image functions to know for sure, but it seems like you might be able to accomplish this with a combination of Javascript and PHP...

With JS, you can grab all or some of the HTML/CSS on a page, by selecting elements on the page, grabbing the HTML/CSS inside of the selection, and dumping it in a JS variable. Then, via the POST or maybe AJAX, you could send the contents of this variable (the selected HTML/CSS) to a PHP file...

What I don;t know is if, at this point, you can convert the raw HTML/CSS into an image using PHP's image functions....It seems like with some math, logic and a little patience, you might be able to create a "screenshot" image file of part or all of a web page that way...The only thing that I can think of that might not translate well is the default GUI of the browser (i.e., scroll bars, form controls)..

Sure would be interested to know if this is possible, or if someone has ever attempted this, from someone who knows more about the image functions...

Inigoesdr
03-04-2007, 02:10 AM
You could, in theory, accomplish this, but the codebase would be ridiculously huge, not worth the effort, and not really accurate as you would only be generating what you -think- the page would look like in the user's browser.

Sandrix
03-04-2007, 06:30 PM
I don't get it. I just want to watermark the image with hexagons and want someone to help me with this :) so where is the problem?

Inigoesdr
03-04-2007, 09:51 PM
This will generate the hexagons image:

<?php

// Misc. Settings
$hexagon_width = 30;
$hexagon_height = 25;
$hexagon_spacer = 3;
$hexagon_columns = 5;
$hexagon_rows = 5;

header("Content-type: image/png"); // send PNG header
$img = imagecreate(150,175); // create the image pointer
imagecolorallocate($img,255,255,255); // set the background color
$color = imagecolorallocate($img,255,128,0); // set the hexagon color

// Custom function to (hopefully) allow scaling of hexagon shapes
function create_hexagon($img, $color, $height = 25, $width = 30, $left_offset = 0, $top_offset = 0)
{
$points = array($left_offset,($height / 2) + $top_offset, // point 1 (left)
(($width / 4 * 1) + $left_offset),$top_offset, // point 2 (left-top)
(($width / 4 * 3) + $left_offset),$top_offset, // point 3 (right-top)
$width + $left_offset,($height / 2) + $top_offset, // point 4 (right)
($width / 4 * 3) + $left_offset,$height + $top_offset, // point 5 (right-bottom)
($width / 4 * 1) + $left_offset,$height + $top_offset); // point 6 (left-bottom)
imagefilledpolygon($img,$points,6,$color);
}

// Loops for columns & rows of hexagons
for($i = 0; $i < $hexagon_columns; $i++)
{
for($j = 0; $j < $hexagon_rows; $j++)
{
$top_offset = ($i % 2) == 1 ? ($hexagon_height / 2) + $hexagon_spacer : 0; // Add a topspacer to odd numbered rows so the hexagons interlock
create_hexagon($img, $color, $hexagon_height, $hexagon_width, (($hexagon_width / 4 * 3) + $hexagon_spacer) * $i, $j * ($hexagon_height + $hexagon_spacer) + $top_offset);
}
}
imagepng($img); // output the image
imagedestroy($img); // free the memory used
?>
Now how exactly did you want to watermark it? (Text or an image).

Sandrix
03-05-2007, 07:43 AM
This will generate the hexagons image:

<?php

// Misc. Settings
$hexagon_width = 30;
$hexagon_height = 25;
$hexagon_spacer = 3;
$hexagon_columns = 5;
$hexagon_rows = 5;

header("Content-type: image/png"); // send PNG header
$img = imagecreate(150,175); // create the image pointer
imagecolorallocate($img,255,255,255); // set the background color
$color = imagecolorallocate($img,255,128,0); // set the hexagon color

// Custom function to (hopefully) allow scaling of hexagon shapes
function create_hexagon($img, $color, $height = 25, $width = 30, $left_offset = 0, $top_offset = 0)
{
$points = array($left_offset,($height / 2) + $top_offset, // point 1 (left)
(($width / 4 * 1) + $left_offset),$top_offset, // point 2 (left-top)
(($width / 4 * 3) + $left_offset),$top_offset, // point 3 (right-top)
$width + $left_offset,($height / 2) + $top_offset, // point 4 (right)
($width / 4 * 3) + $left_offset,$height + $top_offset, // point 5 (right-bottom)
($width / 4 * 1) + $left_offset,$height + $top_offset); // point 6 (left-bottom)
imagefilledpolygon($img,$points,6,$color);
}

// Loops for columns & rows of hexagons
for($i = 0; $i < $hexagon_columns; $i++)
{
for($j = 0; $j < $hexagon_rows; $j++)
{
$top_offset = ($i % 2) == 1 ? ($hexagon_height / 2) + $hexagon_spacer : 0; // Add a topspacer to odd numbered rows so the hexagons interlock
create_hexagon($img, $color, $hexagon_height, $hexagon_width, (($hexagon_width / 4 * 3) + $hexagon_spacer) * $i, $j * ($hexagon_height + $hexagon_spacer) + $top_offset);
}
}
imagepng($img); // output the image
imagedestroy($img); // free the memory used
?>
Now how exactly did you want to watermark it? (Text or an image).

Thank you very much for your responce :) I haven't tested the code you provided but I'm sure it works thanks again. Aand about watermarking, image watermaking will be great I mean I want to put all those hexagons on some background.

Sandrix
03-06-2007, 10:25 AM
I have changed

$top_offset = ($i % 2) == 1 ? ($hexagon_height / 2)

to


$top_offset = ($i % 2.2) == 1 ? ($hexagon_height / 2)

and now it works great! :) Thanks

sorry if this is a flood :(

Inigoesdr
03-06-2007, 05:25 PM
You can use imagecreatefromjpeg() (http://www.php.net/manual/en/function.imagecreatefromjpeg.php) or whatever your background image type is instead of imagecreate() to make a background.

Sandrix
03-06-2007, 08:50 PM
You can use imagecreatefromjpeg() (http://www.php.net/manual/en/function.imagecreatefromjpeg.php) or whatever your background image type is instead of imagecreate() to make a background.


Yeah already did that :) Thanks mate :) you can't imagine how important it was for me.

Sandrix
03-07-2007, 08:45 PM
Is the any way to draw more smooth lines?

Inigoesdr
03-07-2007, 10:21 PM
You can try playing with the points, adjusting the quality, and different output formats.

Sandrix
03-10-2007, 10:32 AM
I tryed that, but no result, I can't make the hexagons look smoother :(

http://img96.imageshack.us/img96/1104/polyfa1.png

Inigoesdr
03-10-2007, 03:40 PM
I tryed that, but no result, I can't make the hexagons look smoother :(

http://img96.imageshack.us/img96/1104/polyfa1.png

Here, try this out:
<?php
// Misc. Settings
$hexagon_width = 60;
$hexagon_height = 50;
$hexagon_spacer = 3;
$hexagon_columns = 5;
$hexagon_rows = 5;

header("Content-type: image/png"); // send PNG header
$img = imagecreate(600,700); // create the image pointer
imagecolorallocate($img,255,255,255); // set the background color
$color = imagecolorallocate($img,255,128,0); // set the hexagon color

// Custom function to (hopefully) allow scaling of hexagon shapes
function create_hexagon($img, $color, $height = 25, $width = 30, $left_offset = 0, $top_offset = 0)
{
$points = array($left_offset,($height / 2) + $top_offset, // point 1 (left)
(($width / 4 * 1) + $left_offset),$top_offset, // point 2 (left-top)
(($width / 4 * 3) + $left_offset),$top_offset, // point 3 (right-top)
$width + $left_offset,($height / 2) + $top_offset, // point 4 (right)
($width / 4 * 3) + $left_offset,$height + $top_offset, // point 5 (right-bottom)
($width / 4 * 1) + $left_offset,$height + $top_offset); // point 6 (left-bottom)
imagefilledpolygon($img,$points,6,$color);
}

// Loops for columns & rows of hexagons
for($i = 0; $i < $hexagon_columns; $i++)
{
for($j = 0; $j < $hexagon_rows; $j++)
{
$top_offset = ($i % 2) == 1 ? ($hexagon_height / 2) + $hexagon_spacer : 0; // Add a topspacer to odd numbered rows so the hexagons interlock
create_hexagon($img, $color, $hexagon_height, $hexagon_width, (($hexagon_width / 4 * 3) + $hexagon_spacer) * $i, $j * ($hexagon_height + $hexagon_spacer) + $top_offset);
}
}
$newimg = imagecreatetruecolor(150,175); // create the image pointer
imagecopyresampled($newimg, $img, 0, 0, 0, 0, 150, 175, 300, 350);
imagepng($newimg); // output the image
imagedestroy($img); // free the memory used
imagedestroy($newimg); // free the memory used
?>
I rendered the hexagons larger and resampled it on a truecolor image. The edges look a lot smoother.

Sandrix
03-10-2007, 11:12 PM
I fill ashamed :S

Thanks a lot, now it looks great :) I don't know how to thank you.

westmatrix99
03-11-2007, 09:15 PM
Sorry chekura, what was the purpose of this hexagon images?
Cheers
West


Hello all :) I'm new one here... Hope you'll help newbie programmer...

Is there any way in PHP to get part of browser screen as JPEG (Or any other type) image?

To concrete:

Using this code



Function GenHexagons($R, $CountX, $CountY, $Left, $Top){
$StepX = 1.5 * $R;
$stepY = Sqrt(3) * $R;

For($i = 0; $i < $CountX; $i++){
$X = $Left + Round( $StepX * $i );

For($j = 0; $j < $CountY; $j++){
$Y = $Top + Round( $stepY * ( $j + 0.5 * ($i % 2)));
// echo '<p><a href="'. $Y .'">Y = '.$Y.'</a></p>';
// echo '<p><a href="'. $X .'">X = '.$X.'</a></p>';
echo "<img src=\"hex.png\" style=\"position: absolute; top:".$Y."px; left:".$X."px; \" alt=\"\" />" ;
}
}

}

GenHexagons(21, 5, 5, 50, 0);


I get somthing like this:

http://www.geomedia.ge/screen123.PNG


PHP code makes so, that hexagon image is repeating horizontally and vertically and I get many images. But it would be great to combine them as one file image... Any idea? :confused:


Thanks :)

Sandrix
03-13-2007, 10:48 PM
westmatrix99

I think his problem is also solved :)

westmatrix99
03-13-2007, 10:50 PM
I do realize that but was just wondering what he or she needed it for, that's all.
Cheers
West

chekura
04-12-2007, 02:40 PM
Inigoesdr thx dude :P One more question please...

How can I make so that when mouse clicks on hexagon the border color was changed to other one and it's coordinates was outputted. Any idea?

I attached .zip file to this post, it's doing right what I want, but I can't understand it...

If you have any idea how to do that would be great to tell me :)

Thx again.

westmatrix99, I'm trying to make simple PHP Game map, but I'm new one in PHP coding and so...

Sandrix
04-19-2007, 04:35 PM
so is this thread still alive?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum