Example #1
0
 /**
  * Loads a GD resource from any of the following:
  *
  *   Img object        : $test= new Img('/images/myimage.png');
  *   GD resource       : imagecreatefrompng('/images/myimage.png')
  *   Filepath          : '/images/myimage.png', '/images/myimage.dat', '/images/myimagebase64.dat'
  *   URL               : 'http://www.myserver.com/images/myimage.jpg'
  *   dataURI           : 'data:image/png;charset=utf-8;base64,VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='
  *   binary string     : file_get_contents()
  *   base64 raw string : 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='
  *
  * @param   mixed     $image   Various types of image source data.
  * @return  resource           GD image resource.
  */
 private function GD_create($image)
 {
     # Init from another Img object
     if (is_a($image, 'AlpTools\\Img')) {
         $this->type = $image->type;
         $this->src_image = $image->src_image;
         $this->src_filename = $image->src_filename;
         $this->dst_filename = $image->dst_filename;
         return $this->clone_resource($image->img);
     } elseif (is_resource($image)) {
         $this->type = imagecolortransparent($image) > -1 ? IMAGETYPE_GIF : IMAGETYPE_PNG;
         $this->src_image = $this->src_filename = $this->dst_filename = $image . '';
         return $this->clone_resource($image);
     } elseif (@file_exists($image) || (bool) filter_var($image, FILTER_VALIDATE_URL)) {
         $this->src_filename = $this->dst_filename = File::get_filename($image);
         $this->src_image = $image;
         $info = getimagesize($image);
         # Init from normal/binary file
         if ($info) {
             $this->type = $info[2];
             return imagecreatefromstring(file_get_contents($image));
         } else {
             $image = base64_decode(file_get_contents($image));
             $type = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $image);
             if (strpos($this->type, 'text') !== 0 && $this->type != 'application/x-empty') {
                 # For constructor calls
                 $this->type = $this->get_mime($type);
                 return imagecreatefromstring($image);
             }
         }
     } elseif (is_string($image)) {
         # Store the original source on constructor calls
         $this->src_image = $image;
         # Init from binary data: file_get_contents()
         $type = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $image);
         if (strpos($type, 'text') !== 0 && $type != 'application/x-empty') {
             $this->src_filename = $this->dst_filename = 'binary_' . hash('md5', $image);
             $this->type = $this->get_mime($type);
             return imagecreatefromstring($image);
         }
         # Init from data URI: 'data:image/png;charset=utf-8;base64,VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='
         $info = File::uri_info($image);
         if ($info) {
             $this->src_filename = $this->dst_filename = 'dataURI_' . hash('md5', $info['data']);
             $this->type = $this->get_mime($info['mime']);
             return imagecreatefromstring(base64_decode($info['data']));
         } else {
             $base = base64_decode(str_replace(' ', '+', $image), true);
             if (base64_encode($base) === $image) {
                 $this->src_filename = $this->dst_filename = 'base64_' . hash('md5', $base);
                 $this->type = $this->get_mime(finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $base));
                 return imagecreatefromstring($base);
             } else {
                 return $this->text_image($text);
             }
         }
     }
     throw new \Exception(__NAMESPACE__ . __CLASS__ . ": Could not load GD resource. Invalid or unsupported source data.");
     # Something went wrong, stop processing
     return false;
 }