php- compressing a base64 decoded image fails -


i images base64 encoded string front end , have decode them , save them images in server. image can of format- png,gif or jpeg. part works fine. images uploaded users can of large size, i'm trying compress them backend , part failed miserably.

i have 2 functions. 1 converting base64 string image , other 1 compressing it.

this function converts string 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 folder     $success = file_put_contents($filepath, $data);     $result  = $success ? 1 : 0;      //call compression function     $compressthisimg= $filepath;     $d = compress($compressthisimg, $filename, 80);      if($result==1)     {         return $filename;     }     else     {         return null;     }  } 

and function compressing:

//function compress 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 above function not compression, throws error:

 failed open stream: no such file or directory 

and function uploads original image.

why dont use function create 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 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 "";     } } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -