【问题标题】:php- compressing a base64 decoded image failsphp-压缩base64解码图像失败
【发布时间】:2016-12-14 05:39:56
【问题描述】:

我将图像作为 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

我的函数会上传原始图像。

【问题讨论】:

  • 一般性说明:尝试压缩已压缩格式(png、gif、jpg)的图像没有任何意义。
  • 如果图像那么大,可能需要调整系统的.ini 文件才能处理更大的文件。但是,如果确实上传了较大的文件,那么您可以划掉这条评论。其他原因导致了这种情况,我看不出它是什么。但是,请检查路径设置和权限。
  • 无论如何它都会减小尺寸,是的,我知道我也会失去清晰度。无论如何可以从后端减小图像的大小。据我所知,base64 将图像转换为字符串,它不会减小大小。我检查了原始文件和上传的文件。 @arkascha
  • 这不是关于上传更大的图像,我正在尝试将它们压缩一点,以便它们可以更快地加载。 @弗雷德-ii-
  • failed to open stream: No such file or directory 对我来说是错误的,告诉我压缩图像没有被正确保存(和/或读取);这就是我从中得到的。 @AmeliaEarheart

标签: php image image-processing image-compression


【解决方案1】:

为什么你不使用这个函数来创建任何类型的图像形式 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 "";
    }
}

【讨论】:

  • 它会减小图像的大小吗? @Haresh Vidja
  • 您想减小图像大小或图像质量吗?
  • 图像大小主要是..我不在乎我是否也会失去一点清晰度。但主要是图像大小。 @Haresh Vidja
  • @AmeliaEarheart,我已经修改了图像大小调整的代码,自定义高度和宽度传入函数。
  • 谢谢亲爱的..我会试试这个代码。希望这能解决问题。 @Haresh Vidja
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
相关资源
最近更新 更多