Merge different thumb modes.

This commit is contained in:
Lars Jung 2014-11-20 01:01:51 +01:00
parent e2fe515a97
commit bf663d575b
3 changed files with 27 additions and 137 deletions

View file

@ -10,13 +10,12 @@ modulejs.define('ext/thumbnails', ['_', 'core/settings', 'core/event', 'core/ser
}, allsettings.thumbnails);
function requestThumb(type, href, mode, ratio, callback) {
function requestThumb(type, href, ratio, callback) {
server.request({
action: 'getThumbHref',
type: type,
href: href,
mode: mode,
width: settings.size * ratio,
height: settings.size
}, function (json) {
@ -41,7 +40,7 @@ modulejs.define('ext/thumbnails', ['_', 'core/settings', 'core/event', 'core/ser
if (item.thumbSquare) {
item.$view.find('.icon.square img').addClass('thumb').attr('src', item.thumbSquare);
} else {
requestThumb(type, item.absHref, 'square', 1, function (src) {
requestThumb(type, item.absHref, 1, function (src) {
if (src && item.$view) {
item.thumbSquare = src;
@ -52,7 +51,7 @@ modulejs.define('ext/thumbnails', ['_', 'core/settings', 'core/event', 'core/ser
if (item.thumbRational) {
item.$view.find('.icon.rational img').addClass('thumb').attr('src', item.thumbRational);
} else {
requestThumb(type, item.absHref, 'rational', 2, function (src) {
requestThumb(type, item.absHref, 4/3, function (src) {
if (src && item.$view) {
item.thumbRational = src;

View file

@ -117,12 +117,11 @@ class Api {
$type = Util::use_request_param("type");
$src_url = Util::use_request_param("href");
$mode = Util::use_request_param("mode");
$width = Util::use_request_param("width");
$height = Util::use_request_param("height");
$thumb = new Thumb($this->app);
$thumb_url = $thumb->thumb($type, $src_url, $mode, $width, $height);
$thumb_url = $thumb->thumb($type, $src_url, $width, $height);
Util::json_fail(3, "thumbnail creation failed", $thumb_url === null);
Util::json_exit(array("absHref" => $thumb_url));

View file

@ -23,13 +23,14 @@ class Thumb {
}
public function thumb($type, $source_url, $mode, $width, $height) {
public function thumb($type, $source_url, $width, $height) {
$source_path = $this->app->to_path($source_url);
if (!file_exists($source_path) || Util::starts_with($source_path, CACHE_PATH)) {
return null;
}
$capture_path = $source_path;
if ($type === "img") {
$capture_path = $source_path;
} else if ($type === "mov") {
@ -42,17 +43,17 @@ class Thumb {
$capture_path = $this->capture(Thumb::$CONVERT_CMDV, $source_path);
}
return $this->thumb_href($capture_path, $mode, $width, $height);
return $this->thumb_href($capture_path, $width, $height);
}
private function thumb_href($source_path, $mode, $width, $height) {
private function thumb_href($source_path, $width, $height) {
if (!file_exists($source_path)) {
return null;
}
$name = "thumb-" . sha1("$source_path-$width-$height-$mode") . ".jpg";
$name = "thumb-" . sha1("$source_path-$width-$height") . ".jpg";
$thumb_path = $this->thumbs_path . "/" . $name;
$thumb_url = $this->thumbs_href . "/" . $name;
@ -73,7 +74,7 @@ class Thumb {
$image->set_source($source_path);
}
$image->thumb($mode, $width, $height);
$image->thumb($width, $height);
$image->save_dest_jpeg($thumb_path, 80);
}
@ -164,15 +165,6 @@ class Image {
}
public function save_dest_png($filename, $quality = 9) {
if (!is_null($this->dest)) {
@imagepng($this->dest, $filename, $quality);
@chmod($filename, 0775);
}
}
public function release_dest() {
if (!is_null($this->dest)) {
@ -195,133 +187,33 @@ class Image {
}
private function magic($dest_x, $dest_y, $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height, $can_width = null, $can_height = null, $color = null) {
public function thumb($width, $height) {
if (is_null($this->source)) {
return;
}
if ($can_width === 0) {
$can_width = 1;
}
if ($can_height === 0) {
$can_height = 1;
}
if ($dest_width === 0) {
$dest_width = 1;
}
if ($dest_height === 0) {
$dest_height = 1;
}
$ratio = 1.0 * $width / $height;
$src_r = 1.0 * $this->width / $this->height;
if (!is_null($can_width) && !is_null($can_height)) {
$this->dest = imagecreatetruecolor($can_width, $can_height);
if ($src_r <= $ratio) {
$src_w = $this->width;
$src_h = $src_w / $ratio;
$src_x = 0;
} else {
$this->dest = imagecreatetruecolor($dest_width, $dest_height);
$src_h = $this->height;
$src_w = $src_h * $ratio;
$src_x = 0.5 * ($this->width - $src_w);
}
if (is_null($color)) {
$color = array(255, 255, 255);
}
$icol = imagecolorallocate($this->dest, $color[0], $color[1], $color[2]);
$src_x = intval($src_x);
$src_w = intval($src_w);
$src_h = intval($src_h);
$this->dest = imagecreatetruecolor($width, $height);
$icol = imagecolorallocate($this->dest, 255, 255, 255);
imagefill($this->dest, 0, 0, $icol);
imagecopyresampled($this->dest, $this->source, $dest_x, $dest_y, $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height);
}
public function thumb($mode, $width, $height = null, $color = null) {
if ($height === null) {
$height = $width;
}
if ($mode === "square") {
$this->square_thumb($width);
} elseif ($mode === "rational") {
$this->rational_thumb($width, $height);
} elseif ($mode === "center") {
$this->center_thumb($width, $height, $color);
} else {
$this->free_thumb($width, $height);
}
}
public function square_thumb($width) {
if (is_null($this->source)) {
return;
}
$a = min($this->width, $this->height);
$x = intval(($this->width - $a) / 2);
$y = intval(($this->height - $a) / 2);
$this->magic(0, 0, $x, $y, $width, $width, $a, $a);
}
public function rational_thumb($width, $height) {
if (is_null($this->source)) {
return;
}
$r = 1.0 * $this->width / $this->height;
$h = $height;
$w = $r * $h;
if ($w > $width) {
$w = $width;
$h = 1.0 / $r * $w;
}
$w = intval($w);
$h = intval($h);
$this->magic(0, 0, 0, 0, $w, $h, $this->width, $this->height);
}
public function center_thumb($width, $height, $color = null) {
if (is_null($this->source)) {
return;
}
$r = 1.0 * $this->width / $this->height;
$h = $height;
$w = $r * $h;
if ($w > $width) {
$w = $width;
$h = 1.0 / $r * $w;
}
$w = intval($w);
$h = intval($h);
$x = intval(($width - $w) / 2);
$y = intval(($height - $h) / 2);
$this->magic($x, $y, 0, 0, $w, $h, $this->width, $this->height, $width, $height, $color);
}
public function free_thumb($width, $height) {
if (is_null($this->source)) {
return;
}
$w = intval($width);
$h = intval($height);
$this->magic(0, 0, 0, 0, $w, $h, $this->width, $this->height);
imagecopyresampled($this->dest, $this->source, 0, 0, $src_x, 0, $width, $height, $src_w, $src_h);
}