$rgb = imagecolorsforindex ($files, imagecolorat ($files, $i, $j)); это че и нафига? вообще какой то чудной подход