Skip to content

Commit

Permalink
Merge pull request #6 from 8fold/filenames
Browse files Browse the repository at this point in the history
BC: filename() and destination()
  • Loading branch information
joshbruce authored Apr 15, 2023
2 parents 8f016ee + 8bc428e commit ecd2487
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
69 changes: 35 additions & 34 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ class Image
* Will overwrite image file at path.
*
* @param string $url Use HTTP or FTP
* @param string $filename Destination
* @param string $destination Destination
*
* @return self
*/
public static function fromUrlToLocalPath(
string $url,
string $filename
string $destination
): self|ImageError|EnvironmentError {
$parts = explode('/', $filename);
$parts = explode('/', $destination);
$last = array_pop($parts);
if (str_contains($last, '.') === false) {
$file = self::filenameFromUrl($url);
if (is_string($file) === false) {
return $file;
}
$filename = $filename . '/' . $file;
$destination = $destination . '/' . $file;
}

$copied = self::copiedUrlToLocalpath($url, $filename);
$copied = self::copiedUrlToLocalpath($url, $destination);
if (is_bool($copied) === false) {
return $copied;
}
Expand All @@ -48,16 +48,16 @@ public static function fromUrlToLocalPath(
return ImageError::FailedToCopyFromUrl;
}

return self::atLocalPath($filename);
return self::atLocalPath($destination);
}

public static function atLocalPath(string $filename): self|ImageError
public static function atLocalPath(string $destination): self|ImageError
{
if (is_file($filename) === false) {
if (is_file($destination) === false) {
return ImageError::FileNotFound;
}

$mimetype = mime_content_type($filename);
$mimetype = mime_content_type($destination);
if (
in_array($mimetype, ImageFormats::SUPPORTED_MIME_TYPES) === false or
$mimetype === false
Expand All @@ -66,18 +66,18 @@ public static function atLocalPath(string $filename): self|ImageError
}

$image = match ($mimetype) {
default => imagecreatefromjpeg($filename)
default => imagecreatefromjpeg($destination)
};
if ($image === false) {
return ImageError::UnsupportedMimeType;
}

return self::fromImage($image, $filename);
return self::fromImage($image, $destination);
}

public static function fromImage(GDImage $image, string $filename): self
public static function fromImage(GDImage $image, string $destination): self
{
return new self($image, $filename);
return new self($image, $destination);
}

public static function copiedUrlToLocalPath(
Expand Down Expand Up @@ -125,19 +125,19 @@ public static function filenameFromUrl(string $url): string|EnvironmentError
return array_pop($parts);
}

private static function didMakeDirectoryFor(string $filename): bool
private static function didMakeDirectoryFor(string $destination): bool
{
$dir = self::directoryFromFilename($filename);
$dir = self::directoryFromFilename($destination);
if (is_dir($dir)) {
return true;
}

return mkdir($dir, 0777, true);
}

private static function directoryFromFilename(string $filename): string
private static function directoryFromFilename(string $destination): string
{
$dir = explode('/', $filename);
$dir = explode('/', $destination);
array_pop($dir);
return implode('/', $dir);
}
Expand All @@ -151,7 +151,7 @@ private static function isHttpOrFtp(string $path): bool

final private function __construct(
private readonly GDImage $image,
private readonly string $filename
private readonly string $destination
) {
}

Expand All @@ -160,50 +160,51 @@ public function image(): GDImage
return $this->image;
}

public function filename(bool $includePath = true): string
public function destination(): string
{
if ($includePath) {
return $this->filename;
}
return $this->destination;
}

$parts = explode('/', $this->filename);
public function filename(): string
{
$parts = explode('/', $this->destination());
return array_pop($parts);
}

public function scale(float $scale, string $filename): self|ImageError
public function scale(float $scale, string $destination): self|ImageError
{
$tWidth = intval($this->width() * $scale);
$new = imagescale($this->image(), $tWidth);
if ($new === false) {
return ImageError::FailedToScaleImage;
}

$image = self::fromImage($new, $filename);
if ($image->didSave($filename) === false) {
$image = self::fromImage($new, $destination);
if ($image->didSave($destination) === false) {
return ImageError::FailedToSaveAfterScaling;
}

return $image;
}

public function scaleToWidth(int $width, string $filename): self|ImageError
public function scaleToWidth(int $width, string $destination): self|ImageError
{
$scale = $width / $this->width();
return $this->scale($scale, $filename);
return $this->scale($scale, $destination);
}

public function scaleToHeight(int $height, string $filename): self|ImageError
public function scaleToHeight(int $height, string $destination): self|ImageError
{
$scale = $height / $this->height();
return $this->scale($scale, $filename);
return $this->scale($scale, $destination);
}

public function didSave(string $filename, bool $makeDirectory = true): bool
public function didSave(string $destination, bool $makeDirectory = true): bool
{
if (self::didMakeDirectoryFor($filename) === false) {
if (self::didMakeDirectoryFor($destination) === false) {
return false;
}
return imagejpeg($this->image(), $filename);
return imagejpeg($this->image(), $destination);
}

/**
Expand All @@ -212,7 +213,7 @@ public function didSave(string $filename, bool $makeDirectory = true): bool
private function getImageSize(): array
{
if (count($this->imageSizeInfo) === 0) {
$size = getimagesize($this->filename());
$size = getimagesize($this->destination());
if ($size === false) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function can_get_file_only(): void
$expected = '8fold-jewel-small.jpg';

$result = Image::atLocalPath(__DIR__ . '/test-files/8fold-jewel-small.jpg')
->filename(includePath: false);
->filename();

$this->assertSame(
$expected,
Expand Down

0 comments on commit ecd2487

Please sign in to comment.