JBZoo\Utils\Image::strToBin PHP Method

strToBin() public static method

Convert string to binary data
public static strToBin ( $imageString ) : string
$imageString
return string
    public static function strToBin($imageString)
    {
        $cleanedString = str_replace(' ', '+', preg_replace('#^data:image/[^;]+;base64,#', '', $imageString));
        $result = base64_decode($cleanedString, true);
        if (!$result) {
            $result = $imageString;
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * Get meta data of image or base64 string
  *
  * @param null|string $image
  * @param bool|null $strict
  *
  * @return $this
  * @throws Exception
  */
 protected function _loadMeta($image = null, $strict = false)
 {
     // Gather meta data
     if (null === $image && $this->_filename) {
         $imageInfo = getimagesize($this->_filename);
         $this->_image = $this->_imageCreate($imageInfo['mime']);
     } elseif (Helper::isGdRes($image)) {
         $this->_image = $image;
         $imageInfo = array('0' => imagesx($this->_image), '1' => imagesy($this->_image), 'mime' => 'image/png');
     } else {
         if (!Sys::isFunc('getimagesizefromstring')) {
             throw new Exception('PHP 5.4 is required to use method getimagesizefromstring');
         }
         if ($strict) {
             $cleanedString = str_replace(' ', '+', preg_replace('#^data:image/[^;]+;base64,#', '', $image));
             if (base64_decode($cleanedString, true) === false) {
                 throw new Exception('Invalid image source.');
             }
         }
         $image = Helper::strToBin($image);
         $imageInfo = getimagesizefromstring($image);
         $this->_image = imagecreatefromstring($image);
     }
     // Set internal state
     $this->_mime = $imageInfo['mime'];
     $this->_width = $imageInfo['0'];
     $this->_height = $imageInfo['1'];
     $this->_exif = $this->_getExif();
     $this->_orient = $this->_getOrientation();
     // Prepare alpha chanel
     Helper::addAlpha($this->_image);
     return $this;
 }
All Usage Examples Of JBZoo\Utils\Image::strToBin