douzhen5158 2016-08-08 12:32
浏览 124

php-压缩base64解码图像失败

I get images as base64 encoded string front end and I have to decode them and save them as images in server. Image can be of any format- png,gif or jpeg. This part works fine. But sometimes the images uploaded by the users can be of very large size, so I'm trying to compress them from backend and this part failed miserably.

I have two functions. One for converting base64 string to image and other one for compressing it.

This is the function that converts the string to an image.

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

}

And this is the function that does compressing:

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

But the above function does not do any compression, it throws error:

 failed to open stream: No such file or directory

and my function uploads the original image.

  • 写回答

1条回答 默认 最新

  • drazvzi741287 2016-08-08 12:44
    关注

    Why you dont use this function for create any type of image form base64 edncoded image string?

    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 "";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog