•  0
    PHP

    Resize base64 image (png image) and save in png file

      Admin     14794        0        Report content

    function getimagepng($base64_string){
    $im = base64_decode($base64_string);
    $filename = “/tmp/”;
    $filename .= uniqid(“intrinsic_”).”.png”;
    
      $file = fopen($filename, “wb”);
      $newIamge = resize_image($im, 500, 500);
    
      fwrite($file, $newIamge);
      fclose($file);
      return $filename;
    }
    
    function resize_image($file, $w, $h, $crop=FALSE) {
      //list($width, $height) = getimagesize($file);
      $src = imagecreatefromstring($file);
      if (!$src) return false;
      $width = imagesx($src);
      $height = imagesy($src);
    
      $r = $width / $height;
      if ($crop) {
          if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
          } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
          }
          $newwidth = $w;
          $newheight = $h;
      } else {
          if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
          } else {
            $newheight = $w/$r;
            $newwidth = $w;
          }
      }
      //$src = imagecreatefrompng($file);
      $dst = imagecreatetruecolor($newwidth, $newheight);
      imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    
      // Buffering
    ob_start();
    imagepng($dst);
    $data = ob_get_contents();
    ob_end_clean();
      return $data;
    }

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations