php上传透明gif和png';s


php upload black bg on transparent gifs and png's

我正在使用jquery.php插件上传和裁剪图像。除了透明gif/pngs上的黑色背景外,一切都很好。我想保持透明度。我读过其他帖子,意识到以前也出现过这种情况,但不完全理解如何将以前的修复程序应用到我的代码中。

            <?php
            ##########################################################################################################
            # IMAGE FUNCTIONS                                                                                        #
            # You do not need to alter these functions                                                               #
            ##########################################################################################################
            function resizeImage($image,$width,$height,$scale) {
                list($imagewidth, $imageheight, $imageType) = getimagesize($image);
                $imageType = image_type_to_mime_type($imageType);
                $newImageWidth = ceil($width * $scale);
                $newImageHeight = ceil($height * $scale);
                $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
                switch($imageType) {
                    case "image/gif":
                        $source=imagecreatefromgif($image); 
                        break;
                    case "image/pjpeg":
                    case "image/jpeg":
                    case "image/jpg":
                        $source=imagecreatefromjpeg($image); 
                        break;
                    case "image/png":
                    case "image/x-png":
                        $source=imagecreatefrompng($image); 
                        break;
                }
                imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
                switch($imageType) {
                    case "image/gif":
                        imagegif($newImage,$image); 
                        break;
                    case "image/pjpeg":
                    case "image/jpeg":
                    case "image/jpg":
                        imagejpeg($newImage,$image,90); 
                        break;
                    case "image/png":
                    case "image/x-png":
                        imagepng($newImage,$image);  
                        break;
                }
                chmod($image, 0777);
                return $image;
            }
            ?>

我通过添加取得了进展

            imagealphablending($newImage, true); 
            // Allocate a transparent color and fill the new image with it. 
            // Without this the image will have a black background instead of being transparent.
            $transparent = imagecolorallocatealpha( $newImage, 0, 0, 0, 127 ); 
            imagefill( $newImage, 0, 0, $transparent ); 
                imagesavealpha($newImage,true); 

在行$newImage=imagecreatetruecolor($newImageWidth,$newImageHeight)下方;

它现在适用于pngs,但不适用于gifs???