Browse Source

webp

pull/114/head
Lorenzo Yario 3 months ago
committed by Zankaria
parent
commit
46e61a71cf
  1. 93
      inc/image.php

93
inc/image.php

@ -11,7 +11,7 @@ class Image {
public $src, $format, $image, $size; public $src, $format, $image, $size;
public function __construct($src, $format = false, $size = false) { public function __construct($src, $format = false, $size = false) {
global $config; global $config;
$this->src = $src; $this->src = $src;
$this->format = $format; $this->format = $format;
@ -25,21 +25,21 @@ class Image {
error(_('Unsupported file format: ') . $this->format); error(_('Unsupported file format: ') . $this->format);
} }
} }
$this->image = new $classname($this, $size); $this->image = new $classname($this, $size);
if (!$this->image->valid()) { if (!$this->image->valid()) {
$this->delete(); $this->delete();
error($config['error']['invalidimg']); error($config['error']['invalidimg']);
} }
$this->size = (object)array('width' => $this->image->_width(), 'height' => $this->image->_height()); $this->size = (object)array('width' => $this->image->_width(), 'height' => $this->image->_height());
if ($this->size->width < 1 || $this->size->height < 1) { if ($this->size->width < 1 || $this->size->height < 1) {
$this->delete(); $this->delete();
error($config['error']['invalidimg']); error($config['error']['invalidimg']);
} }
} }
public function resize($extension, $max_width, $max_height) { public function resize($extension, $max_width, $max_height) {
global $config; global $config;
@ -63,16 +63,16 @@ class Image {
error(_('Unsupported file format: ') . $extension); error(_('Unsupported file format: ') . $extension);
} }
} }
$thumb = new $classname(false); $thumb = new $classname(false);
$thumb->src = $this->src; $thumb->src = $this->src;
$thumb->format = $this->format; $thumb->format = $this->format;
$thumb->original_width = $this->size->width; $thumb->original_width = $this->size->width;
$thumb->original_height = $this->size->height; $thumb->original_height = $this->size->height;
$x_ratio = $max_width / $this->size->width; $x_ratio = $max_width / $this->size->width;
$y_ratio = $max_height / $this->size->height; $y_ratio = $max_height / $this->size->height;
if (($this->size->width <= $max_width) && ($this->size->height <= $max_height)) { if (($this->size->width <= $max_width) && ($this->size->height <= $max_height)) {
$width = $this->size->width; $width = $this->size->width;
$height = $this->size->height; $height = $this->size->height;
@ -83,16 +83,16 @@ class Image {
$width = ceil($y_ratio * $this->size->width); $width = ceil($y_ratio * $this->size->width);
$height = $max_height; $height = $max_height;
} }
$thumb->_resize($this->image->image, $width, $height); $thumb->_resize($this->image->image, $width, $height);
return $thumb; return $thumb;
} }
public function to($dst) { public function to($dst) {
$this->image->to($dst); $this->image->to($dst);
} }
public function delete() { public function delete() {
file_unlink($this->src); file_unlink($this->src);
} }
@ -115,26 +115,26 @@ class ImageGD {
} }
class ImageBase extends ImageGD { class ImageBase extends ImageGD {
public $image, $src, $original, $original_width, $original_height, $width, $height; public $image, $src, $original, $original_width, $original_height, $width, $height;
public function valid() { public function valid() {
return (bool)$this->image; return (bool)$this->image;
} }
public function __construct($img, $size = false) { public function __construct($img, $size = false) {
if (method_exists($this, 'init')) if (method_exists($this, 'init'))
$this->init(); $this->init();
if ($size && $size[0] > 0 && $size[1] > 0) { if ($size && $size[0] > 0 && $size[1] > 0) {
$this->width = $size[0]; $this->width = $size[0];
$this->height = $size[1]; $this->height = $size[1];
} }
if ($img !== false) { if ($img !== false) {
$this->src = $img->src; $this->src = $img->src;
$this->from(); $this->from();
} }
} }
public function _width() { public function _width() {
if (method_exists($this, 'width')) if (method_exists($this, 'width'))
return $this->width(); return $this->width();
@ -157,7 +157,7 @@ class ImageBase extends ImageGD {
$this->original = &$original; $this->original = &$original;
$this->width = $width; $this->width = $width;
$this->height = $height; $this->height = $height;
if (method_exists($this, 'resize')) if (method_exists($this, 'resize'))
$this->resize(); $this->resize();
else else
@ -200,31 +200,31 @@ class ImageImagick extends ImageBase {
} }
public function resize() { public function resize() {
global $config; global $config;
if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '')) { if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '')) {
$this->image = new Imagick(); $this->image = new Imagick();
$this->image->setFormat('gif'); $this->image->setFormat('gif');
$keep_frames = array(); $keep_frames = array();
for ($i = 0; $i < $this->original->getNumberImages(); $i += floor($this->original->getNumberImages() / $config['thumb_keep_animation_frames'])) for ($i = 0; $i < $this->original->getNumberImages(); $i += floor($this->original->getNumberImages() / $config['thumb_keep_animation_frames']))
$keep_frames[] = $i; $keep_frames[] = $i;
$i = 0; $i = 0;
$delay = 0; $delay = 0;
foreach ($this->original as $frame) { foreach ($this->original as $frame) {
$delay += $frame->getImageDelay(); $delay += $frame->getImageDelay();
if (in_array($i, $keep_frames)) { if (in_array($i, $keep_frames)) {
// $frame->scaleImage($this->width, $this->height, false); // $frame->scaleImage($this->width, $this->height, false);
$frame->sampleImage($this->width, $this->height); $frame->sampleImage($this->width, $this->height);
$frame->setImagePage($this->width, $this->height, 0, 0); $frame->setImagePage($this->width, $this->height, 0, 0);
$frame->setImageDelay($delay); $frame->setImageDelay($delay);
$delay = 0; $delay = 0;
$this->image->addImage($frame->getImage()); $this->image->addImage($frame->getImage());
} }
$i++; $i++;
} }
} else { } else {
$this->image = clone $this->original; $this->image = clone $this->original;
$this->image->scaleImage($this->width, $this->height, false); $this->image->scaleImage($this->width, $this->height, false);
@ -235,15 +235,15 @@ class ImageImagick extends ImageBase {
class ImageConvert extends ImageBase { class ImageConvert extends ImageBase {
public $width, $height, $temp, $gm = false, $gifsicle = false; public $width, $height, $temp, $gm = false, $gifsicle = false;
public function init() { public function init() {
global $config; global $config;
if ($config['thumb_method'] == 'gm' || $config['thumb_method'] == 'gm+gifsicle') if ($config['thumb_method'] == 'gm' || $config['thumb_method'] == 'gm+gifsicle')
$this->gm = true; $this->gm = true;
if ($config['thumb_method'] == 'convert+gifsicle' || $config['thumb_method'] == 'gm+gifsicle') if ($config['thumb_method'] == 'convert+gifsicle' || $config['thumb_method'] == 'gm+gifsicle')
$this->gifsicle = true; $this->gifsicle = true;
$this->temp = false; $this->temp = false;
} }
public function get_size($src, $try_gd_first = true) { public function get_size($src, $try_gd_first = true) {
@ -265,7 +265,7 @@ class ImageConvert extends ImageBase {
if ($size) { if ($size) {
$this->width = $size[0]; $this->width = $size[0];
$this->height = $size[1]; $this->height = $size[1];
$this->image = true; $this->image = true;
} else { } else {
// mark as invalid // mark as invalid
@ -274,7 +274,7 @@ class ImageConvert extends ImageBase {
} }
public function to($src) { public function to($src) {
global $config; global $config;
if (!$this->temp) { if (!$this->temp) {
if ($config['strip_exif']) { if ($config['strip_exif']) {
if($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' . if($error = shell_exec_error(($this->gm ? 'gm ' : '') . 'convert ' .
@ -306,16 +306,16 @@ class ImageConvert extends ImageBase {
} }
public function resize() { public function resize() {
global $config; global $config;
if ($this->temp) { if ($this->temp) {
// remove old // remove old
$this->destroy(); $this->destroy();
} }
$this->temp = tempnam($config['tmp'], 'convert') . ($config['thumb_ext'] == '' ? '' : '.' . $config['thumb_ext']); $this->temp = tempnam($config['tmp'], 'convert') . ($config['thumb_ext'] == '' ? '' : '.' . $config['thumb_ext']);
$config['thumb_keep_animation_frames'] = (int)$config['thumb_keep_animation_frames']; $config['thumb_keep_animation_frames'] = (int)$config['thumb_keep_animation_frames'];
if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '') && $config['thumb_keep_animation_frames'] > 1) { if ($this->format == 'gif' && ($config['thumb_ext'] == 'gif' || $config['thumb_ext'] == '') && $config['thumb_keep_animation_frames'] > 1) {
if ($this->gifsicle) { if ($this->gifsicle) {
if (($error = shell_exec("gifsicle -w --unoptimize -O2 --resize {$this->width}x{$this->height} < " . if (($error = shell_exec("gifsicle -w --unoptimize -O2 --resize {$this->width}x{$this->height} < " .
@ -380,7 +380,7 @@ class ImageConvert extends ImageBase {
} }
} }
} }
// For when -auto-orient doesn't exist (older versions) // For when -auto-orient doesn't exist (older versions)
static public function jpeg_exif_orientation($src, $exif = false) { static public function jpeg_exif_orientation($src, $exif = false) {
if (!$exif) { if (!$exif) {
@ -398,16 +398,16 @@ class ImageConvert extends ImageBase {
// 8888 // 8888
// 88 // 88
// 88 // 88
return '-flop'; return '-flop';
case 3: case 3:
// 88 // 88
// 88 // 88
// 8888 // 8888
// 88 // 88
// 888888 // 888888
return '-flip -flop'; return '-flip -flop';
case 4: case 4:
// 88 // 88
@ -415,31 +415,31 @@ class ImageConvert extends ImageBase {
// 8888 // 8888
// 88 // 88
// 888888 // 888888
return '-flip'; return '-flip';
case 5: case 5:
// 8888888888 // 8888888888
// 88 88 // 88 88
// 88 // 88
return '-rotate 90 -flop'; return '-rotate 90 -flop';
case 6: case 6:
// 88 // 88
// 88 88 // 88 88
// 8888888888 // 8888888888
return '-rotate 90'; return '-rotate 90';
case 7: case 7:
// 88 // 88
// 88 88 // 88 88
// 8888888888 // 8888888888
return '-rotate "-90" -flop'; return '-rotate "-90" -flop';
case 8: case 8:
// 8888888888 // 8888888888
// 88 88 // 88 88
// 88 // 88
return '-rotate "-90"'; return '-rotate "-90"';
} }
} }
@ -497,6 +497,11 @@ class ImageBMP extends ImageBase {
} }
} }
class ImageWEBP extends ImageBase {
public function from() {
$this->image = @imagecreatefromwebp($this->src);
}
public function to($src) {
imagewebp($this->image, $src);
}
}

Loading…
Cancel
Save