Php 上传时,前摄像头拍摄的照片变为黑色

Php 上传时,前摄像头拍摄的照片变为黑色,php,Php,我使用GD库来调整图像的大小,使用exif来固定方向。它工作得很好,但在一些特定的设备上,如三星Galaxies,从上传的前置摄像头拍摄的照片会变成黑色。我怎么修理它 图像大小调整方法: public function process($task, $savePath, $taskWidth=0, $taskHeight=0, $quality=80) { $quality = (int) $quality; $quality = ($quality < 0 or $qua

我使用GD库来调整图像的大小,使用exif来固定方向。它工作得很好,但在一些特定的设备上,如三星Galaxies,从上传的前置摄像头拍摄的照片会变成黑色。我怎么修理它

图像大小调整方法:

public function process($task, $savePath, $taskWidth=0, $taskHeight=0, $quality=80)
{
    $quality = (int) $quality;
    $quality = ($quality < 0 or $quality > 100) ? 80 : $quality;

    if (file_exists ($this->path))
    {
        if (in_array($this->extension, $this->photoExtensions))
        {
            if ($this->extension == "gif")
            {
                copy($this->path, $savePath);
                return true;
            }

            switch ($this->extension)
            {
                case 'png':
                    $im = imagecreatefrompng($this->path);
                    break;

                default:
                    $im = imagecreatefromjpeg($this->path);
                    break;
            }

            if ($task == "crop")
            {
                if ($taskWidth > 0 && $taskHeight > 0)
                {
                    $cropWidth = $this->imageWidth;
                    $cropHeight = $this->imageHeight;
                    $ratioWidth = 1;
                    $ratioHeight = 1;
                    $destinationX = 0;
                    $destinationY = 0;
                    $sourceX = 0;
                    $sourceY = 0;

                    $cropWidth = ($taskWidth == 0 || $taskWidth > $this->imageWidth) ? $this->imageWidth : $taskWidth;
                    $cropHeight = ($taskHeight == 0 || $taskHeight > $this->imageHeight) ? $this->imageHeight : $taskHeight;

                    if ($cropWidth > $this->imageWidth)
                    {
                        $destinationX = ($cropWidth - $this->imageWidth) / 2;
                    }

                    if ($cropHeight > $this->imageHeight)
                    {
                        $destinationY = ($cropHeight - $this->imageHeight) / 2;
                    }

                    if ($cropWidth < $this->imageWidth || $cropHeight < $this->imageHeight)
                    {
                        $ratioWidth = $cropWidth / $this->imageWidth;
                        $ratioHeight = $cropHeight / $this->imageHeight;

                        if ($cropHeight > $this->imageHeight)
                        {
                            $sourceX  = ($this->imageWidth - $cropWidth) / 2;
                        }
                        elseif ($cropWidth > $this->imageWidth)
                        {
                            $sourceY  = ($this->imageHeight - $cropHeight) / 2;
                        }
                        else
                        {
                            if ($ratioHeight > $ratioWidth)
                            {
                                $sourceX = round(($this->imageWidth - ($cropWidth / $ratioHeight)) / 2);
                            }
                            else
                            {
                                $sourceY = round(($this->imageHeight - ($cropHeight / $ratioWidth)) / 2);
                            }
                        }
                    }

                    $cropImage = @imagecreatetruecolor($cropWidth, $cropHeight);

                    if ($this->extension == "png")
                    {
                        @imagesavealpha($cropImage, true);
                        @imagefill($cropImage, 0, 0, @imagecolorallocatealpha($cropImage, 0, 0, 0, 127));
                    }

                    @imagecopyresampled($cropImage, $im ,$destinationX, $destinationY, $sourceX, $sourceY, $cropWidth - 2 * $destinationX, $cropHeight - 2 * $destinationY, $this->imageWidth - 2 * $sourceX, $this->imageHeight - 2 * $sourceY);

                    @imageinterlace($cropImage, true);

                    switch ($this->extension)
                    {
                        case 'png':
                            @imagepng($cropImage, $savePath);
                            break;

                        default:
                            @imagejpeg($cropImage, $savePath, $quality);
                            break;
                    }

                    @imagedestroy($cropImage);
                }
            }
            elseif ($task == "resize")
            {
                if ($taskWidth == 0 && $taskHeight == 0)
                {
                    return false;
                }

                if ($taskWidth > 0 && $taskHeight == 0)
                {
                    $resizeWidth = $taskWidth;
                    $resizeRatio = $resizeWidth / $this->imageWidth;
                    $resizeHeight = floor($this->imageHeight * $resizeRatio);
                }
                elseif ($taskHeight > 0 && $taskWidth == 0)
                {
                    $resizeHeight = $taskHeight;
                    $resizeRatio = $resizeHeight / $this->imageHeight;
                    $resizeWidth = floor($this->imageWidth * $resizeRatio);
                }
                elseif ($taskWidth > 0 && $taskHeight > 0)
                {
                    $resizeWidth = $taskWidth;
                    $resizeHeight = $taskHeight;
                }

                $resizeImage = @imagecreatetruecolor($resizeWidth, $resizeHeight);

                if ($this->extension == "png")
                {
                    @imagesavealpha($resizeImage, true);
                    @imagefill($resizeImage, 0, 0, @imagecolorallocatealpha($resizeImage, 0, 0, 0, 127));
                }

                @imagecopyresampled($resizeImage, $im, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $this->imageWidth, $this->imageHeight);
                @imageinterlace($resizeImage, true);

                switch ($this->extension)
                {
                    case 'png':
                        @imagepng($resizeImage, $savePath);
                        break;

                    default:
                        @imagejpeg($resizeImage, $savePath, $quality);
                        break;
                }

                @imagedestroy($resizeImage);
            }
            elseif ($task == "scale")
            {
                if ($taskWidth == 0)
                {
                    $taskWidth = 100;
                }

                if ($taskHeight == 0)
                {
                    $taskHeight = 100;
                }

                $scaleWidth = $this->imageWidth * ($taskWidth / 100);
                $scaleHeight = $this->imageHeight * ($taskHeight / 100);
                $scaleImage = @imagecreatetruecolor($scaleWidth, $scaleHeight);

                if ($this->extension == "png")
                {
                    @imagesavealpha($scaleImage, true);
                    @imagefill($scaleImage, 0, 0, imagecolorallocatealpha($scaleImage, 0, 0, 0, 127));
                }

                @imagecopyresampled($scaleImage, $im, 0, 0, 0, 0, $scaleWidth, $scaleHeight, $this->imageWidth, $this->imageHeight);
                @imageinterlace($scaleImage, true);

                switch ($this->extension)
                {
                    case 'png':
                        @imagepng($scaleImage, $savePath);
                        break;

                    default:
                        @imagejpeg($scaleImage, $savePath, $quality);
                        break;
                }

                @imagedestroy($scaleImage);
            }
        }
    }
}
公共函数流程($task、$savePath、$taskWidth=0、$taskHeight=0、$quality=80)
{
$quality=(int)$quality;
$quality=($quality<0或$quality>100)?80:$quality;
如果(文件_存在($this->path))
{
if(在数组中($this->extension,$this->photoExtensions))
{
如果($this->extension==“gif”)
{
复制($this->path,$savePath);
返回true;
}
开关($this->extension)
{
案例“png”:
$im=imagecreatefrompng($this->path);
打破
违约:
$im=imagecreatefromjpeg($this->path);
打破
}
如果($task==“裁剪”)
{
如果($taskWidth>0&&$taskHeight>0)
{
$cropWidth=$this->imageWidth;
$cropHeight=$this->imageHeight;
$ratioWidth=1;
$ratioHeight=1;
$destinationX=0;
$destinationY=0;
$sourceX=0;
$sourceY=0;
$cropWidth=($taskWidth==0 | |$taskWidth>$this->imageWidth)?$this->imageWidth:$taskWidth;
$cropHeight=($taskHeight==0 | |$taskHeight>$this->imageHeight)?$this->imageHeight:$taskHeight;
如果($cropWidth>$this->imageWidth)
{
$destinationX=($cropWidth-$this->imageWidth)/2;
}
如果($cropHeight>$this->imageHeight)
{
$destinationY=($cropHeight-$this->imageHeight)/2;
}
如果($cropWidth<$this->imageWidth | |$cropHeight<$this->imageHeight)
{
$ratioWidth=$cropWidth/$this->imageWidth;
$ratioHeight=$cropHeight/$this->imageHeight;
如果($cropHeight>$this->imageHeight)
{
$sourceX=($this->imageWidth-$cropWidth)/2;
}
elseif($cropWidth>$this->imageWidth)
{
$sourceY=($this->imageHeight-$cropHeight)/2;
}
其他的
{
如果($ratiowhight>$ratioWidth)
{
$sourceX=round($this->imageWidth-($cropWidth/$ratioHeight))/2);
}
其他的
{
$sourceY=round($this->imageHeight-($cropHeight/$ratioWidth))/2);
}
}
}
$cropImage=@imagecreatetruecolor($cropWidth,$cropHeight);
如果($this->extension==“png”)
{
@imagesavealpha($cropImage,true);
@imagefill($cropImage,0,0,@imagecolorallocatealpha($cropImage,0,0,0127));
}
@imagecopyresampled($cropImage、$im、$destinationX、$destinationY、$sourceX、$sourceY、$cropWidth-2*$destinationX、$cropHeight-2*$destinationY、$this->imageWidth-2*$sourceX、$this->imageHeight-2*$sourceY);
@imageinterlace($cropImage,true);
开关($this->extension)
{
案例“png”:
@imagepng($cropImage,$savePath);
打破
违约:
@imagejpeg($cropImage,$savePath,$quality);
打破
}
@图像处理(cropImage);
}
}
elseif($task==“resize”)
{
如果($taskWidth==0&&$taskHeight==0)
{
返回false;
}
如果($taskWidth>0&&$taskHeight==0)
{
$resizeWidth=$taskWidth;
$resizeRatio=$resizeWidth/$this->imageWidth;
$resizeHeight=floor($this->imageHeight*$resizeRatio);
}
elseif($taskHeight>0&&$taskWidth==0)
{
$resizeHeight=$taskHeight;
$resizeRatio=$resizeHeight/$this->imageHeight;
$resizeWidth=floor($this->imageWidth*$resizeRatio);
}
elseif($taskWidth>0&&$taskHeight>0)
{
$resizeWidth=$taskWidth;
$resizeHeight=$taskHeight;
}
$resizeImage=@imagecreatetruecolor($resizeWidth,$resizeHeight);
如果($this->extension==“png”)
{
@imagesavealpha($resizeImage,true);
@imagefill($resizeImage,0,0,@imagecolorallocatealpha($resizeImage,0,0,0127));
}
@imagecopyresampled($resizeImage、$im、0、0、0、$resizeWidth、$resizeHeight、$this->imageWidth、$this->imageHeight);
@imageinterlace($resizeImage,true);
开关($this->extension)
{
案例“png”:
@imagepng($resizeImage,$savePath);