protected function doGetInfo($name)
 {
     $ext = strtolower(FileSystemTool::getFileExtension($name));
     if (array_key_exists($ext, $this->extensions2Types)) {
         $type = $this->extensions2Types[$ext];
         $info = [];
         $this->collectInfo($name, $ext, $info);
         $info['type'] = $type;
         return $info;
     }
     return false;
 }
 public function generate($name)
 {
     $this->prefix = FileSystemTool::getFileName($name);
     $this->suffix = FileSystemTool::getFileExtension($name);
     if ('' !== $this->suffix) {
         $this->suffix = '.' . $this->suffix;
     }
     if (null === $this->generateAffixCb) {
         $this->generateAffixCb = function ($n) {
             return '-' . ++$n;
         };
     }
     return parent::generate($name);
 }
Example #3
0
function colis_get_info_by_name($name)
{
    $type = 'none';
    $ext = strtolower(FileSystemTool::getFileExtension($name));
    $ext2Type = ['jpg' => 'image', 'jpeg' => 'image', 'gif' => 'image', 'png' => 'image', 'mp4' => 'video'];
    if (array_key_exists($ext, $ext2Type)) {
        $type = $ext2Type[$ext];
    }
    $finalName = null;
    if (0 === strpos($name, 'http://') || 0 === strpos($name, 'https://')) {
        $finalName = $name;
        // handling youtube urls
        if ('video' === $type) {
            $type = 'externalVideo';
        } elseif (false !== ($youTubeId = YouTubeTool::getId($finalName))) {
            $type = 'youtube';
        }
    } else {
        // look for a file named name in a specific dir...
        $finalName = ITEMS_DIR_URL . '/' . $name;
    }
    switch ($type) {
        case 'image':
            return ['type' => $type, 'src' => $finalName];
            break;
        case 'youtube':
            $v = YouTubeVideo::create()->setVideoId($youTubeId)->setApiKey(YOUTUBE_API_KEY);
            $iframe = '<iframe src="https://www.youtube.com/embed/' . $youTubeId . '" frameborder="0" allowfullscreen></iframe>';
            return ['type' => $type, 'title' => $v->getTitle(), 'description' => nl2br($v->getDescription()), 'duration' => $v->getDuration(), 'thumbnail' => $v->getThumbnail(), 'iframe' => $iframe];
            break;
        case 'externalVideo':
            $realPath = WEB_ROOT_DIR . $finalName;
            return ['type' => $type, 'src' => $finalName];
            break;
        case 'video':
            $realPath = WEB_ROOT_DIR . $finalName;
            $duration = getVideoDuration($realPath);
            return ['type' => 'localVideo', 'src' => $finalName, 'duration' => $duration];
            break;
        default:
            return ["type" => $type];
            break;
    }
}
Example #4
0
 public function getInfo($name, &$err)
 {
     $ext = strtolower(FileSystemTool::getFileExtension($name));
     if (in_array($ext, $this->extensions, true)) {
         if (0 === strpos($name, 'http://') || 0 === strpos($name, 'https://')) {
             return ['type' => 'image', 'src' => $name];
         } else {
             if (null !== $this->dir && null !== $this->webRootDir) {
                 $realPath = $this->dir . '/' . preg_replace('!\\.+!', '.', $name);
                 if (file_exists($realPath)) {
                     $realPath = realpath($realPath);
                     $url = str_replace(realpath($this->webRootDir), '', $realPath);
                     return ['type' => 'image', 'src' => $url];
                 }
             } else {
                 $err = "VideoInfoHandler: dir or webRootDir not set";
             }
         }
     }
     return false;
 }
 protected function collectInfo($name, $extension, array &$info)
 {
     parent::collectInfo($name, $extension, $info);
     if (null !== $this->itemDir) {
         if (is_dir($this->itemDir)) {
             DirScanner::create()->scanDir($this->itemDir, function ($path, $rPath, $level) use($extension, &$info) {
                 $ext = strtolower(FileSystemTool::getFileExtension($path));
                 if ($extension === $ext) {
                     // info[src] is an url
                     if (null !== $this->webRoot) {
                         $info['src'] = str_replace($this->webRoot, '', $path);
                     } else {
                         $info['src'] = $path;
                     }
                     $this->onCollectInfoAfter($path, $rPath, $extension, $level, $info);
                 }
             });
         } else {
             // if the dir doesn't exist yet, it's not a problem: we collect nothing
         }
     } else {
         throw new ColisException("itemDir is not set");
     }
 }
Example #6
0
 /**
  * @return array of use statements' class names inside the given directory
  */
 public static function getUseDependenciesByFolder($dir)
 {
     $ret = [];
     DirScanner::create()->scanDir($dir, function ($path, $rPath, $level) use(&$ret) {
         if (is_file($path) && 'php' === strtolower(FileSystemTool::getFileExtension($path))) {
             $tokens = token_get_all(file_get_contents($path));
             $ret = array_merge($ret, TokenFinderTool::getUseDependencies($tokens));
         }
     });
     $ret = array_unique($ret);
     sort($ret);
     return $ret;
 }
 /**
  *
  * Create the biggest thumbnail possible given the maxWidth and maxHeight.
  *
  * Ratio is always preserved.
  * The destination directory is created if necessary.
  * The function ensures that an image is created (handy to use with uploaded files for security).
  *
  * If maxWidth is null and maxHeight is null, the created image will have the same dimensions
  * as the original image.
  *
  * If maxWidth is set and maxHeight is null, then the created image's width will be maxWidth, and the image's height will
  * be the natural height that maintain the ratio.
  *
  * Conversely, if maxHeight is set and maxWidth is null, then the created image's height will be maxHeight, and the image's width will
  * be the natural width that maintain the ratio.
  *
  * If both maxWidth and maxHeight are set, then the created image will have dimensions so that it honors both limitations,
  * and in accordance with the image original ratio.
  *
  *
  *
  * maxWidth and maxHeight must be strictly positive integers.
  *
  *
  *
  *
  *
  *
  * Notes:
  * Tested transparency of gif and png successfully using
  *      gif to gif
  *      and
  *      png to png
  *
  * With (from phpinfo):
  * - imac 10.11.1
  * - php: 5.6.12
  * - gdVersion: bundled (2.1.0 compatible)
  *
  *
  *
  *
  *
  * @param $src
  * @param $dst
  * @param int $maxWidth
  * @param int $maxHeight
  * @return bool
  */
 public static function biggest($src, $dst, $maxWidth = null, $maxHeight = null)
 {
     list($srcWidth, $srcHeight, $srcType) = getimagesize($src);
     //------------------------------------------------------------------------------/
     // Compute height and width
     //------------------------------------------------------------------------------/
     $ratio = $srcWidth / $srcHeight;
     $width = $srcWidth;
     $height = $srcHeight;
     $maxWidth = (int) $maxWidth;
     $maxHeight = (int) $maxHeight;
     $res = false;
     if (0 !== $maxWidth && 0 === $maxHeight) {
         $width = $maxWidth;
         $height = $width / $ratio;
     } elseif (0 !== $maxHeight && 0 === $maxWidth) {
         $height = $maxHeight;
         $width = $height * $ratio;
     } elseif (0 !== $maxWidth && 0 !== $maxHeight) {
         // taller
         if ($height > $maxHeight) {
             $width = $maxHeight / $height * $width;
             $height = $maxHeight;
         }
         // wider
         if ($width > $maxWidth) {
             $height = $maxWidth / $width * $height;
             $width = $maxWidth;
         }
     }
     $imageFinal = imagecreatetruecolor($width, $height);
     switch ($srcType) {
         case IMAGETYPE_JPEG:
         case IMAGETYPE_JPEG2000:
             $image = imagecreatefromjpeg($src);
             break;
         case IMAGETYPE_PNG:
             $image = imagecreatefrompng($src);
             imagealphablending($imageFinal, false);
             imagesavealpha($imageFinal, true);
             $transparent = imagecolorallocatealpha($imageFinal, 255, 255, 255, 127);
             imagefilledrectangle($imageFinal, 0, 0, $width, $height, $transparent);
             break;
         case IMAGETYPE_GIF:
             $image = imagecreatefromgif($src);
             $transparent_index = imagecolortransparent($image);
             if ($transparent_index >= 0) {
                 imagepalettecopy($image, $imageFinal);
                 imagefill($imageFinal, 0, 0, $transparent_index);
                 imagecolortransparent($imageFinal, $transparent_index);
                 imagetruecolortopalette($imageFinal, true, 256);
             }
             break;
         default:
             throw new \RuntimeException("Unsupported image format: {$srcType}");
             break;
     }
     imagecopyresampled($imageFinal, $image, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
     // assuming the file has an explicit extension (otherwise accept an array as argument...)
     $ext = strtolower(FileSystemTool::getFileExtension($dst));
     if (true === FileSystemTool::mkdir(dirname($dst), 0777, true)) {
         switch ($ext) {
             case 'jpeg':
             case 'jpg':
                 $res = imagejpeg($imageFinal, $dst);
                 break;
             case 'png':
                 $res = imagepng($imageFinal, $dst);
                 break;
             case 'gif':
                 $res = imagegif($imageFinal, $dst);
                 break;
             default:
                 throw new \RuntimeException("Unsupported destination image extension: {$ext}");
                 break;
         }
         imagedestroy($imageFinal);
         imagedestroy($image);
     } else {
         $dst_parent = dirname($dst);
         trigger_error("Couldn't create the target directory {$dst_parent}", E_USER_WARNING);
     }
     return $res;
 }