Goni Schindler Goni Schindler - 3 years ago 124
PHP Question

Php script loading forever not showing output

Well i was trying to figure out where is the problem all day long now,
i wrote some php code to embed text in given image. I put evry 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 forver. I guess i have some problems with my code but i can't figure it out, the debugger didn't help very much as well.
Here is my code:

<?php
$img = imagecreatefromjpeg ("https://dataencrypt.xyz/testimage.jpg");
$data = "abcdefghi"; //example
if($q=strlen($data)%3!=0){
$q==1?$data.="--":$data.="-";
}
if($img!=false){
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;
imagedestroy($img);

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)

into

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

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