forked from leftypol/leftypol
LibMagickMediaHandler.php: refactor inner implementation
This commit is contained in:
parent
0a09144d88
commit
66dd81537a
1 changed files with 107 additions and 81 deletions
|
@ -80,6 +80,101 @@ class LibMagickMediaHandler implements MediaHandler {
|
||||||
return $degrees == 0 || $degrees == 180;
|
return $degrees == 0 || $degrees == 180;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function generateThumbImpl(
|
||||||
|
\Imagick $imagick,
|
||||||
|
string $source_file_path,
|
||||||
|
string $source_file_mime,
|
||||||
|
string $preferred_out_file_dir,
|
||||||
|
string $preferred_out_file_name,
|
||||||
|
string $preferred_out_mime,
|
||||||
|
int $width,
|
||||||
|
int $height,
|
||||||
|
int $max_width,
|
||||||
|
int $max_height
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
$source_file_mime === 'image/gif'
|
||||||
|
&& $this->frames_for_gif_thumbs !== self::THUMB_KEEP_FRAMES_NO
|
||||||
|
&& $imagick->getNumberImages() > 1
|
||||||
|
) {
|
||||||
|
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.gif';
|
||||||
|
|
||||||
|
if ($width > $max_width || $height > $max_height) {
|
||||||
|
$thumb_width = $max_width;
|
||||||
|
$thumb_height = $max_height;
|
||||||
|
} else {
|
||||||
|
$thumb_width = $width;
|
||||||
|
$thumb_height = $height;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->frames_for_gif_thumbs !== self::THUMB_KEEP_FRAMES_ALL) {
|
||||||
|
$other = new \Imagick();
|
||||||
|
try {
|
||||||
|
$other->setFormat('gif');
|
||||||
|
|
||||||
|
$step = \floor($imagick->getNumberImages() / $this->frames_for_gif_thumbs);
|
||||||
|
|
||||||
|
for ($i = 0, $j = 0; $i < $imagick->getNumberImages(); $i += $step, $j++) {
|
||||||
|
$imagick->setIteratorIndex($i);
|
||||||
|
$delay = $imagick->getImageDelay();
|
||||||
|
|
||||||
|
$imagick->sampleImage($thumb_width, $thumb_height);
|
||||||
|
$imagick->setImagePage($thumb_width, $thumb_height, 0, 0);
|
||||||
|
$imagick->setImageDelay($delay);
|
||||||
|
|
||||||
|
$other->addImage($imagick->getImage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$other->optimizeImageLayers();
|
||||||
|
|
||||||
|
$other->setImageCompressionQuality(70);
|
||||||
|
$other->writeImage("gif:$out_path");
|
||||||
|
} finally {
|
||||||
|
$other->clear();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$imagick->stripImage();
|
||||||
|
$imagick->optimizeImageLayers();
|
||||||
|
|
||||||
|
$imagick->setImageCompressionQuality(70);
|
||||||
|
$imagick->writeImage("gif:$out_path");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ThumbGenerationResult(
|
||||||
|
$out_path,
|
||||||
|
'image/gif',
|
||||||
|
false,
|
||||||
|
$thumb_width,
|
||||||
|
$thumb_height
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
if ($width > $max_width || $height > $max_height) {
|
||||||
|
$thumb_width = $max_width;
|
||||||
|
$thumb_height = $max_height;
|
||||||
|
|
||||||
|
$imagick->thumbnailImage($max_width, $max_height, true);
|
||||||
|
} else {
|
||||||
|
$thumb_width = $width;
|
||||||
|
$thumb_height = $height;
|
||||||
|
}
|
||||||
|
|
||||||
|
$out_ext = Mime\mime_to_ext($preferred_out_mime);
|
||||||
|
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.' . $out_ext;
|
||||||
|
|
||||||
|
$imagick->stripImage();
|
||||||
|
$imagick->setImageCompressionQuality(70);
|
||||||
|
$imagick->writeImage("$out_ext:$out_path");
|
||||||
|
|
||||||
|
return new ThumbGenerationResult(
|
||||||
|
$out_path,
|
||||||
|
$preferred_out_mime,
|
||||||
|
false,
|
||||||
|
$thumb_width,
|
||||||
|
$thumb_height
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function checkImagickVersion(): bool {
|
public static function checkImagickVersion(): bool {
|
||||||
$imagick_ver = \phpversion('imagick');
|
$imagick_ver = \phpversion('imagick');
|
||||||
if ($imagick_ver !== false && \version_compare($imagick_ver, self::MIN_IMAGICK_VERSION, '>=')) {
|
if ($imagick_ver !== false && \version_compare($imagick_ver, self::MIN_IMAGICK_VERSION, '>=')) {
|
||||||
|
@ -159,87 +254,18 @@ class LibMagickMediaHandler implements MediaHandler {
|
||||||
$height = $tmp;
|
$height = $tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
return self::generateThumbImpl(
|
||||||
$source_file_mime === 'image/gif'
|
$imagick,
|
||||||
&& $this->frames_for_gif_thumbs !== self::THUMB_KEEP_FRAMES_NO
|
$source_file_path,
|
||||||
&& $imagick->getNumberImages() > 1
|
$source_file_mime,
|
||||||
) {
|
$preferred_out_file_dir,
|
||||||
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.gif';
|
$preferred_out_file_name,
|
||||||
|
$preferred_out_mime,
|
||||||
if ($width > $max_width || $height > $max_height) {
|
$width,
|
||||||
$thumb_width = $max_width;
|
$height,
|
||||||
$thumb_height = $max_height;
|
$max_width,
|
||||||
} else {
|
$max_height
|
||||||
$thumb_width = $width;
|
);
|
||||||
$thumb_height = $height;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->frames_for_gif_thumbs !== self::THUMB_KEEP_FRAMES_ALL) {
|
|
||||||
$other = new \Imagick();
|
|
||||||
try {
|
|
||||||
$other->setFormat('gif');
|
|
||||||
|
|
||||||
$step = \floor($imagick->getNumberImages() / $this->frames_for_gif_thumbs);
|
|
||||||
|
|
||||||
for ($i = 0, $j = 0; $i < $imagick->getNumberImages(); $i += $step, $j++) {
|
|
||||||
$imagick->setIteratorIndex($i);
|
|
||||||
$delay = $imagick->getImageDelay();
|
|
||||||
|
|
||||||
$imagick->sampleImage($thumb_width, $thumb_height);
|
|
||||||
$imagick->setImagePage($thumb_width, $thumb_height, 0, 0);
|
|
||||||
$imagick->setImageDelay($delay);
|
|
||||||
|
|
||||||
$other->addImage($imagick->getImage());
|
|
||||||
}
|
|
||||||
|
|
||||||
$other->optimizeImageLayers();
|
|
||||||
|
|
||||||
$other->setImageCompressionQuality(70);
|
|
||||||
$other->writeImage("gif:$out_path");
|
|
||||||
} finally {
|
|
||||||
$other->clear();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$imagick->stripImage();
|
|
||||||
$imagick->optimizeImageLayers();
|
|
||||||
|
|
||||||
$imagick->setImageCompressionQuality(70);
|
|
||||||
$imagick->writeImage("gif:$out_path");
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ThumbGenerationResult(
|
|
||||||
$out_path,
|
|
||||||
'image/gif',
|
|
||||||
false,
|
|
||||||
$thumb_width,
|
|
||||||
$thumb_height
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
if ($width > $max_width || $height > $max_height) {
|
|
||||||
$thumb_width = $max_width;
|
|
||||||
$thumb_height = $max_height;
|
|
||||||
|
|
||||||
$imagick->thumbnailImage($max_width, $max_height, true);
|
|
||||||
} else {
|
|
||||||
$thumb_width = $width;
|
|
||||||
$thumb_height = $height;
|
|
||||||
}
|
|
||||||
|
|
||||||
$out_ext = Mime\mime_to_ext($preferred_out_mime);
|
|
||||||
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.' . $out_ext;
|
|
||||||
|
|
||||||
$imagick->stripImage();
|
|
||||||
$imagick->setImageCompressionQuality(70);
|
|
||||||
$imagick->writeImage("$out_ext:$out_path");
|
|
||||||
|
|
||||||
return new ThumbGenerationResult(
|
|
||||||
$out_path,
|
|
||||||
$preferred_out_mime,
|
|
||||||
false,
|
|
||||||
$thumb_width,
|
|
||||||
$thumb_height
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue