From c1d28d644130dc2660b0e148cd3eebe17392f7e1 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Thu, 17 Dec 2020 10:09:38 -0300 Subject: [PATCH] feat: add get image blurhash #456 --- composer.json | 5 +-- src/classes/entities/class-tainacan-item.php | 34 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index d03574e01..c6480e1ff 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,11 @@ { - "name": "medialabufg/tainacan", + "name": "tainacan/tainacan", "description": "Transforme seu site wordpress em um repositório digital.", "type": "wordpress-plugin", "require": { "respect/validation": "^1.1", - "smalot/pdfparser": "*" + "smalot/pdfparser": "*", + "kornrunner/blurhash": "^1.1" }, "require-dev": { "squizlabs/php_codesniffer": "^2.2 || ^3.0.2", diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index db05c9d0d..17e179b35 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -130,13 +130,47 @@ class Item extends Entity { array_unshift($sizes, 'full'); + if( in_array('tainacan-small', $sizes ) ) { + $tmp_src = wp_get_attachment_image_src( $this->get__thumbnail_id(), 'tainacan-small' ); + $blurhash = $this->get_iamge_blurhash($tmp_src[0], $tmp_src[1], $tmp_src[2]); + } + foreach ( $sizes as $size ) { $thumbs[$size] = wp_get_attachment_image_src( $this->get__thumbnail_id(), $size ); + $thumbs[$size][] = $blurhash; } return apply_filters("tainacan-item-get-thumbnail", $thumbs, $this); } + private function get_iamge_blurhash($file_path, $width, $height) { + $image = imagecreatefromstring(file_get_contents($file_path)); + if($image == false) + return ''; + + $max_width = 45; + if( $width > $max_width ) { + $image = imagescale($image, $max_width); + $width = imagesx($image); + $height = imagesy($image); + } + + $pixels = []; + for ($y = 0; $y < $height; ++$y) { + $row = []; + for ($x = 0; $x < $width; ++$x) { + $index = imagecolorat($image, $x, $y); + $colors = imagecolorsforindex($image, $index); + $row[] = [$colors['red'], $colors['green'], $colors['blue']]; + } + $pixels[] = $row; + } + $components_x = 4; + $components_y = 3; + $blurhash = \kornrunner\Blurhash\Blurhash::encode($pixels, $components_x, $components_y); + return $blurhash; + } + /** * @param $id */