Goni Schindler Goni Schindler - 3 years ago 158
PHP Question

PHP script loading forever and not showing output

I tried to figure out where the problem is all day long now, I wrote some PHP code to embed text in a given image. I place every 3 letters in each pixel by using its ASCII value as RGB values. My problem is that when I launch the script it keeps loading forever. I guess I have some problems with my code but I am unable to figure it out, the debugger didn't help very much as well.
Here is my code:

$img = imagecreatefromjpeg ("https://dataencrypt.xyz/testimage.jpg");
$data = "abcdefghi"; //example
for($i = 0;$i<(strlen($data)-3);$i+3){
$part = substr ($data , $i,3 );
$color = getEncryptedColor($img,$part);
imagesetpixel($img, $i,$i, $color);
$decrypted = decryptDataFromImage($img,0,0); // get the first 3 letters.
echo "".$decrypted;

function getEncryptedColor($img,$string){
return imagecolorallocate($img, ord($string[0]), ord($string[1]), ord($string[2]));
function printImageValues($image,$num){
for($x = 0;$x<$num;$x++){
echo "\n".imagecolorat($image, $x, $x);
echo "--END--";
function decryptDataFromImage($img,$x,$y){
$currpixel = imagecolorat($img, $x, $y);
$colors = imagecolorsforindex($img, $currpixel);
$str = "".chr($colors["red"]).chr($colors["green"]).chr($colors["blue"]);
return $str;

Answer Source

Your for loop runs forever because you wrote $i+3, instead of assigning that value to $i, which would be $i = $i + 3

So change this part:

for($i = 0;$i<(strlen($data)-3);$i+3)


for($i = 0;$i<(strlen($data)-3);$i+=3)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download