/** * @param OrganizationImage $image * @return string */ protected function getImageSubPath(OrganizationImage $image) { $id = $image->getId(); if (!$id) { throw new InvalidArgumentException('image must have ID'); } $extension = null; $filename = $image->getName(); if ($filename) { // get an extension from the filename $extension = pathinfo($filename, PATHINFO_EXTENSION); } else { // try get an extension from MIME if any $extension = str_replace('image/', '', $image->getType()); } if (!$extension) { throw new InvalidArgumentException('unable to get an image file extension'); } $firstLevel = substr($id, -1) ?: '0'; $secondLevel = substr($id, -2, 1) ?: '0'; return sprintf('%s/%s/%s.%s', $firstLevel, $secondLevel, $id, $extension); }