It seems that either the server you are pulling the image from is disconnecting the stream, or the method the server is using to redirect you to that image (php) requires some additional programming in the socket.

If I download the image and put it on another server, and then modify the script to work from that server directly (without php redirect) it works just fine everytime.

Strangely, if you compare the two images, on the corrupted image, I get a grey rectangle almost exactly where the "" banner has been inserted into the image. I don't know if that has any significance to the problem or not.

Here is the code I used:


alias plocal return 1

alias GetProfile {
  var %x = 1, %y = $len($$1), %puid = ""
  while (%x <= %y) {
    %puid = $+(%puid,$base($base($asc($mid($1,%x,1)),10,8),8,16))
    inc %x
  hadd -m $+(Picture.,%puid) Nickname $1

  if ($sock($+(PHeader.,%puid))) sockclose $+(PHeader.,%puid)
  if ($sock($+(PImage.,%puid))) sockclose $+(PImage.,%puid)

  if (!$plocal) sockopen $+(PHeader.,%puid) 80
  else sockopen $+(PImage.,%puid) ANOTHER.SERVER.COM 80

on *:SOCKOPEN:P*.*:{
  echo 3 -a SOCKOPEN $sockname $1-

  if (!$plocal) {
    sockwrite -n $sockname GET $+(/memberpic.php?size=l&epuid=,$gettok($sockname,2,46)) HTTP/1.0
    sockwrite -n $sockname Host:
  else {
    sockwrite -n $sockname GET /path/to/memberpic.jpeg
    sockwrite -n $sockname Host: ANOTHER.SERVER.COM
  sockwrite -n $sockname $crlf $+ $crlf

on *:SOCKREAD:PHeader.*:{
  sockread %sread
  if ($sockerr) {
    echo 5 -s $+(Error in $sockname ",$sock($sockname).wsmsg," [,$sock($sockname).wserr,])
  tokenize 32 %sread
  echo -a $sockname > $1-
  if ($1 == HTTP/1.1 && $2 != 200) sockclose $sockname
  if ($len(%sread) == 0) sockrename $sockname $replace($sockname,PHeader,PImage)

on *:SOCKREAD:PImage.*:{
  sockread &buffer

  if ($sockerr) {
    echo 5 -s $+(Error in $sockname ",$sock($sockname).wsmsg," [,$sock($sockname).wserr,])

  ;tokenize 32 $bvar(&buffer,1,$bvar(&buffer,0)).text
  ;echo -a $sockname > $1-

  bwrite test.jpg -1 -1 &buffer


on *:SOCKCLOSE:PImage.*:{
  echo 3 -a SOCKCLOSE $sockname $1-

Change the plocal alias to 0 for the sparkpea server, and to 1 for your secondary server.