Jump to content

Randomly invert/negate section of image


Greenriver

Recommended Posts

Hi,

 

I'm trying to invert a random section of an image. The only way I've thought of doing this in PHP, is to split the image up into sections using imagecopyresampled then negate one of the sections using the imagefilter IMG_FILTER_NEGATE. Then put them all back together using imagecopymerge. Now I have managed to get this working when negating the first section ($split_first), but whenever I try to negate any other section (e.g. $split_sec) nothing happens. Now I know my code doesn't randomly invert it, I'm just trying to get it to work for each section first then I'll try get that working. Below I have posted my code.

 

Any help would be be appreciated,

 

Thank you in advance!

 

<?php
$image=imagecreate(400, 100);
$red = imagecolorallocate($image, 255, 0, 0);
imagefill($image, 0, 0, $red);
$split_first=imagecreate(100, 100);
$split_sec=imagecreate(100, 100);
$split_thir=imagecreate(100, 100);
$split_four=imagecreate(100, 100);

imagecopyresampled($split_first,$image,0,0,0, 0,100,100,100,100);
imagecopyresampled($split_sec,$image,100,0,100,0,100,100,100,100);
imagecopyresampled($split_thir,$image,200,0,200,0,100,100,100,100);
imagecopyresampled($split_four,$image,300,0,300,0,100,100,100,100);


imagefilter($split_sec, IMG_FILTER_NEGATE);

imagecopymerge($image,$split_first,0,0,0,0,100,100,100);
imagecopymerge($image,$split_sec,100,0,100,0,0,100,100);
imagecopymerge($image,$split_thir,200,0,200,0,0,100,100);
imagecopymerge($image,$split_thir,300,0,300,0,0,100,100);

imagepng($image);
imagedestroy($image);
header('Content-Type: image/png');
echo $image;
?>

Link to comment
https://forums.phpfreaks.com/topic/226236-randomly-invertnegate-section-of-image/
Share on other sites

Hi,

 

Thank you for the help. The problem is that the image is not going to be plain colour like the one above (I'm just testing to get it working first) the image will be an actual photo and all of the image will need to be negated at one point, so that would mean I couldn't overwrite the second half with the first like in your idea.

Hi there,

 

Yes that's it, I think that is currently what my code does above, but it only can do the first section, and no others. Does that negate any section of the image? As I can't seem to fathom it to negate every section of the image.

Hi there,

 

Yeah so the idea is I have an image which is 400 x 100. I split it up into four sections each 100 x 100. Then select a section at random to negate. Thus far I've only been able to negate the first section, so the other three sections I can't get to negate. Thank you for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.