1

我正在使用 jquery / php 插件来上传和裁剪图像。除了透明 gifs/png 上的黑色背景外,一切都很好。我想保持透明度。我已经阅读了其他帖子并意识到这已经出现过,但不完全了解如何将以前的修复应用到我的代码中。

            <?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;
            }

            ?>
4

1 回答 1

2

我通过添加取得了进展

            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); 行下方

它现在适用于 png,但不适用于 gif?

于 2012-08-20T15:28:30.603 回答