Пример #1
0
 /**
  * @param string $fileName
  *
  * @throws \Exception
  * @return ImageInterface
  *
  * @todo add more sophisticated checks by inspecting file
  */
 public static function fromFile($fileName)
 {
     $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
     switch ($ext) {
         case 'jpg':
         case 'jpeg':
             return Format\JPEG::fromFile($fileName);
             break;
         case 'png':
             return Format\PNG::fromFile($fileName);
             break;
         case 'webp':
             return Format\WebP::fromFile($fileName);
             break;
         case 'psd':
             return Format\PSD::fromFile($fileName);
             break;
     }
     throw new \Exception('Unrecognised file name');
 }
Пример #2
0
 /**
  * @param $string
  *
  * @return JPEG|WebP|PNG|false
  */
 public static function fromString($string)
 {
     $len = strlen($string);
     // try JPEG
     if ($len >= 2) {
         if (JPEG::SOI === substr($string, 0, 2)) {
             return JPEG::fromString($string);
         }
     }
     // try WebP
     if ($len >= 4) {
         if ('RIFF' === substr($string, 0, 4) && 'WEBP' === substr($string, 8, 4)) {
             return WebP::fromString($string);
         }
     }
     // try PNG
     if ($len >= 8) {
         if (PNG::SIGNATURE === substr($string, 0, 8)) {
             return PNG::fromString($string);
         }
     }
     return false;
 }