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
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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.