Isigar

Resize base64 image

Sep 14th, 2021
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. /**
  2.      * Get plain image with sizes of original image
  3.      * @return string
  4.      */
  5.     protected static function getPlainImage($imageElement): string
  6.     {
  7.         $fileName = $imageElement->fileId . '-' . $imageElement->areaWidth . 'x' . $imageElement->areaHeight;
  8.  
  9.         $img = imagecreatefromstring(base64_decode(self::PLAIN_IMAGE_BASE64));
  10.         $sourceWidth = imagesx($img);
  11.         $sourceHeight = imagesy($img);
  12.  
  13.         $thumb = imagecreatetruecolor($imageElement->areaWidth, $imageElement->areaHeight);
  14.  
  15.         $transparency = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
  16.         imagefilledrectangle($thumb, 0, 0, $imageElement->areaWidth, $imageElement->areaHeight, $transparency);
  17.  
  18.         imagecopyresampled($thumb, $img, 0, 0, 0, 0, $imageElement->areaWidth, $imageElement->areaHeight, $sourceWidth, $sourceHeight);
  19.         imagejpeg($thumb, $fileName);
  20.  
  21.         $imageContent = file_get_contents($fileName);
  22.  
  23.         //Clean & destroy
  24.         imagedestroy($img);
  25.         imagedestroy($thumb);
  26.         unlink($fileName);
  27.  
  28.         return 'data:image/jpeg;base64,' . base64_encode($imageContent);
  29.     }
Add Comment
Please, Sign In to add comment