首页 文章

php-压缩base64解码图像失败

提问于
浏览
3

我将图像作为base64编码的字符串前端,我必须解码它们并将它们保存为服务器中的图像 . 图像可以是任何格式 - png,gif或jpeg . 这部分工作正常 . 但有时用户上传的图像可能非常大,所以我试图从后端压缩它们,这部分失败了 .

我有两个功能 . 一个用于将base64字符串转换为图像,另一个用于压缩它 .

这是将字符串转换为图像的函数 .

function uploadTimelineImage($base64Img)
{
    $data= array(); 
    $upAt=date('YmdHis');

    if (strpos($base64Img, 'data:image/png;base64') !== false) 
    {
        $img = str_replace('data:image/png;base64,', '', $base64Img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);    
        $extension= 'png';
    }

    if (strpos($base64Img, 'data:image/gif;base64') !== false) 
    {
        $img = str_replace('data:image/gif;base64,', '', $base64Img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);    
        $extension= 'gif'; 
    }

    if (strpos($base64Img, 'data:image/jpeg;base64') !== false) 
    {
        $img = str_replace('data:image/jpeg;base64,', '', $base64Img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);    
        $extension= 'jpeg'; 
    }

    $fileName     = 'img'.$upAt.$extension;
    $filePath     = 'foldername/'.'img'.$upAt.$extension;

    //upload image to folder
    $success = file_put_contents($filePath, $data);
    $result  = $success ? 1 : 0;

    //call to compression function
    $compressThisImg= $filePath;
    $d = compress($compressThisImg, $fileName, 80);

    if($result==1)
    {
        return $fileName;
    }
    else
    {
        return null;
    }

}

这是压缩的功能:

//Function to compress an image
function compress($source, $destination, $quality) 
{
    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

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

    return $destination;
}

但是上面的函数没有做任何压缩,它会抛出错误:

failed to open stream: No such file or directory

我的功能上传原始图像 .

1 回答

  • 0

    为什么你不使用这个函数来创建任何类型的图像形式base64 edncoded图像字符串?

    function uploadTimelineImage($base64Img,$h,$w)
    {
        $im = imagecreatefromstring($base64Img);
        if ($im !== false) {
    
    
            $width = imagesx($im);
            $height = imagesy($im);
            $r = $width / $height; // ratio of image
    
            // calculating new size for maintain ratio of image
            if ($w/$h > $r) {
                $newwidth = $h*$r; 
                $newheight = $h;
            } else {
                $newheight = $w/$r;
                $newwidth = $w;
            }
    
            $dst = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresampled($dst, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            imagedestroy($im);
    
            $fileName  = 'img'.date('Ymd').'.jpeg';
            $filepath =  'folder/'.$fileName  ;
            imagejpeg($dst,$filepath);
    
            imagedestroy($dst);
            return $fileName;
        }
        else
        {
            return "";
        }
    }
    

相关问题