上传过程中的图像压缩在 PHP OOP 中给出黑色背景

Image compression during upload gives black background in PHP OOP

我已经在下面提到在 Whosebug 中有答案,但这对我不起作用,因为那个答案就像 imagecreatetruecolor($width, $height) 但在我的代码中我没有宽度和高度。我的代码完全不同。请删除此重复标签
我试图在上传过程中压缩图片,它可以很好地压缩 jpg 张图片,但是当我上传 png transparent 张图片时,它会以黑色背景保存,我用谷歌搜索了很多但没有找到任何完美的解决方案到我的代码。有imagecreatetruecolor($width, $height)的答案,但是我的代码里没有width and height variables,我完全糊涂了。
这是我的函数代码:

public function updateProfilePic($file, $userid) {
    $filename = $file['user_img']['name'];
    $filetmp = $file['user_img']['tmp_name'];
    $valid_ext = array('png', 'jpeg', 'jpg');
    $location = "user/profilepic/" . $filename;
    $file_extension = pathinfo($location, PATHINFO_EXTENSION);
    $file_extensionstr = strtolower($file_extension);

    if(!empty($filename)){
        if (in_array($file_extensionstr, $valid_ext)) {
            $this->compressImage($filetmp, $location, 50);
            // Here I am trying to compress image

            return $this->updateProfilePicture($filename, $userid);
        } else {
            $msg = 'Invalid file type. You can upload only:-' . implode(', ', $valid_ext) . '';
            return $msg;
        }
    } else {
        $msg = 'Please upload your profile picture.';
        return $msg;
    }
}

public function compressImage($source, $destination, $quality) {
    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg'){
        $image = imagecreatefromjpeg($source);
    } elseif ($info['mime'] == 'image/png'){
        $image = imagecreatefrompng($source);
    }

    imagejpeg($image, $destination, $quality);

}

您所说的更新代码:

public function compressImage($source, $destination, $quality) {
        $info = getimagesize($source);
        $width_new = $info[0];
        $height_new = $info[1];

        $dimg = imagecreatetruecolor($width_new, $height_new);
        $background = imagecolorallocate($dimg , 0, 0, 0);
        imagecolortransparent($dimg, $background);
        imagealphablending($dimg, false);
        imagesavealpha($dimg, true);

        if ($info['mime'] == 'image/jpeg'){
            $image = imagecreatefromjpeg($source);
        } elseif ($info['mime'] == 'image/png'){
            $image = imagecreatefrompng($source);
        }

        imagejpeg($image, $destination, $quality);

    }

请先检查我的代码,然后告诉我解决方案。 请帮帮我

这似乎对我有用 - compressImage 方法只是简单地包装在虚拟 class 中用于测试。调用如图所示的方法在输出目录中创建了一个漂亮的透明 PNG 图像。

class fudd{

    public function __construct(){

    }

    public function compressImage( $source, $destination, $quality ) {
        list( $width, $height, $type, $attr ) = getimagesize( $source );
        $target=sprintf('%s/%s',$destination, pathinfo( $source, PATHINFO_BASENAME ) );
        $quality=$quality > 9 ? 5 : $quality;

        switch( image_type_to_mime_type( $type ) ){
            case 'image/jpeg':
                $image = imagecreatefromjpeg( $source );
                imagejpeg( $image, $target, $quality );
            break;
            case 'image/png':
                $image = imagecreatefrompng( $source );
                $bck   = imagecolorallocate( $image , 0, 0, 0 );
                imagecolortransparent( $image, $bck );
                imagealphablending( $image, false );
                imagesavealpha( $image, true );
                imagepng( $image, $target, $quality );
            break;
            default:
                exit( $type );
            break;              
        }
        imagedestroy( $image );
    }
}//end fudd



$img='c:/wwwroot/images/ict_cmyk_jigsaw_1.png';
$destination='c:/temp/fileuploads/stack/';
$quality=50;

$foo=new fudd;
$foo->compressImage( $img, $destination, $quality );

像这样尝试(为图像使用预定义的名称):

public function compressImage( $source, $destination, $quality ) {
    list( $width, $height, $type, $attr ) = getimagesize( $source );
    $quality=$quality > 9 ? 5 : $quality;

    switch( image_type_to_mime_type( $type ) ){
        case 'image/jpeg':
            $image = imagecreatefromjpeg( $source );
            imagejpeg( $image, $destination, $quality );
        break;
        case 'image/png':
            $image = imagecreatefrompng( $source );
            $bck   = imagecolorallocate( $image , 0, 0, 0 );
            imagecolortransparent( $image, $bck );
            imagealphablending( $image, false );
            imagesavealpha( $image, true );
            imagepng( $image, $destination, $quality );
        break;
        default:
            exit( $type );
        break;              
    }
    imagedestroy( $image );
}

我很快就写了一张 webp 图片,也许它会很有趣。 ($destination 需要编辑以供您使用)~ 基本上输入方法主体中显示的一种类型的图像(jpg、png、gif、webp)并设置 compression/quality ~ far虽然经过全面测试

public function createwebpimage( $source=false, $destination=null, $quality=50 ){
    if( $source ){

        list( $width, $height, $type, $attr ) = getimagesize( $source );
        $destination=sprintf( '%s/%s.webp', $destination, pathinfo( $source, PATHINFO_FILENAME ) );

        switch( image_type_to_mime_type( $type ) ){
            case 'image/jpeg':
                $image = imagecreatefromjpeg( $source );
            break;
            case 'image/png':
                $image = imagecreatefrompng( $source );
                $bck   = imagecolorallocate( $image , 0, 0, 0 );
                imagecolortransparent( $image, $bck );
                imagealphablending( $image, false );
                imagesavealpha( $image, true );
            break;
            case 'image/gif':
                $image = imagecreatefromgif( $source );
            break;
            case 'image/webp':
                $image=imagecreatefromwebp( $source );
            break;
            default:exit('invalid image type');
        }

        imagewebp( $image, $destination, $quality );
    }
}