I just wanted to note here something i found else where that was very helpful to me when using jpeg images. when using imagecreatefromjpeg() it can be difficult to allocate new colors.
The work around i found was posted under imagecolorallocate() and prescribes that you first use imagecreate(), allocate colors, and then copy the jpeg into this image.