【问题标题】:Image compression during upload gives black background in PHP OOP [duplicate]上传期间的图像压缩在 PHP OOP 中产生黑色背景 [重复]
【发布时间】:2020-02-17 04:43:46
【问题描述】:

我已经在下面提到过 stackoverflow 中有答案,但这对我不起作用,因为该答案类似于 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);

    }

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

【问题讨论】:

    标签: php compression image-compression


    【解决方案1】:

    这似乎对我有用 - compressImage 方法已简单地包装在一个虚拟类中进行测试。调用如图所示的方法在输出目录中创建了一个漂亮、透明的 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)并设置压缩/质量〜虽然还没有完全测试

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

    【讨论】:

    • 兄弟上传图片后出现错误Warning: imagepng(user/profilepic/executive-package.png/php1B94.tmp): failed to open stream: No such file or directory in E:\xampp\htdocs\zain\lunch\classes\User.php on line 402 --- executive_package.png 是我的图片但我不知道那是什么php1B94.tmp
    • 您需要编辑$target=sprintf('%s/%s',$destination, pathinfo( $source, PATHINFO_BASENAME ) );,因为这适合我的系统。大概你的destination 变量是user/profilepic/executive-package.png ???
    • 什么是$destination?是user/profilepic/executive-package.png ??
    • yes 是这样的:$location = "user/profilepic/" . $filename;,完整地址是http://localhost/zain/lunch/user/profilepic/$filename
    • PNG 是Lossless 格式,图像/照片的压缩率不好。不建议对大图像使用 PNG。我试图复制你所说的并观察到类似的行为。您可能希望使用webp 图像进行调查 - 查看developers.google.com/speed/webp/gallery2 进行大小比较
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多