views:

162

answers:

0

90% of images that I upload with Zend Framework work great and they can be displayed in the browser without a problem but sometimes there si an image that gets uploaded without error yet when I try to display it in the browser I get this error (from time to time, sometimes the image will start loading but it will take ages and it won't load fully, just a part from it, like 30%):

The image "http://example.com/uploads/photos/1421.png" cannot be displayed, because it contains errors.

Could you give me some suggestions what could be the problem here?

I tried googling and it seems that this problem can be caused by ASCII transfer mode and it's recommended to switch to binary transfer. How can I find out if I'm using ASCII or binary transfer? And how can I switch it?

Here is an extraction from my upload script, I have left out unimportant stuff to make it as short as possible (basically it uploads the photo, then it creates a thumbnail and then it adds watermark to the full photos):

                // adds entry to the table
                // returns an array of two paths
                // path to the full image and
                // path to the thumbnail
                $paths = $photos->add($data, $ext);

                if (file_exists($paths[0])) {
                   unlink($paths[0]);
                }
                if (file_exists($paths[1])) {
                   unlink($paths[1]);
                }

                // add filter for renaming the uploaded photo
                $form->photo->addFilter('Rename',
                                        array('target' => $paths[0],
                                              'overwrite' => true));

                // upload the photo
                $form->photo->receive();

                // create a thumbnail
                include BASE_PATH . '/library/My/PHPThumbnailer/ThumbLib.inc.php';
                $thumb = PhpThumbFactory::create($paths[0]);
                $thumb->adaptiveResize(85, 85);
                $thumb->save($paths[1]);
                // resize the main photo so it's not wider than 800px
                $resized = PhpThumbFactory::create($paths[0]);
                $resized->resize(800, 800);
                $resized->save($paths[0]);

                // add watermatk to the bottom right corner
                $pathToFullImage = BASE_PATH . '/public/' . $paths[0];
                $size = getimagesize($pathToFullImage);
                switch ($ext) {
                    case 'gif':
                        $im = imagecreatefromgif($pathToFullImage);
                        break;
                    case 'jpg':
                        $im = imagecreatefromjpeg($pathToFullImage);
                        break;
                    case 'png':
                        $im = imagecreatefrompng($pathToFullImage);
                        break;
                }
                if (false !== $im) {
                    $white = imagecolorallocate($im, 255, 255, 255);
                    $font = BASE_PATH . '/public/fonts/arial.ttf';
                    imagefttext($im,
                                13, // font size
                                0, // angle
                                $size[0] - 132, // x axis (top left is [0, 0])
                                $size[1] - 13, // y axis
                                $white,
                                $font,
                                'Example.com');
                    switch ($ext) {
                        case 'gif':
                            imagegif($im, $pathToFullImage);
                            break;
                        case 'jpg':
                            imagejpeg($im, $pathToFullImage, 100);
                            break;
                        case 'png':
                            imagepng($im, $pathToFullImage, 0);
                            break;
                    }
                    imagedestroy($im);
                }

EDIT: more info