上传时从前置摄像头拍摄的照片变为黑色

时间:2017-07-25 17:50:23

标签: php

我正在使用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);
            }
        }
    }
}

0 个答案:

没有答案