Skip to content

Commit

Permalink
0.0.3: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Oct 26, 2020
1 parent f1d7fca commit 31e4f8b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
88 changes: 87 additions & 1 deletion Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php
namespace TFC\Image;
use Google\Cloud\Vision\V1\AnnotateImageResponse as Res;
use Google\Cloud\Vision\V1\BoundingPoly;
use Google\Cloud\Vision\V1\ImageAnnotatorClient as Annotator;
use Google\Cloud\Vision\V1\LocalizedObjectAnnotation as O;
use Google\Cloud\Vision\V1\NormalizedVertex as V;
use Google\Protobuf\Internal\RepeatedField;
use RecursiveDirectoryIterator as RDI;
use RecursiveIteratorIterator as RII;
# 2020-10-25
final class Command extends \Df\Framework\Console\Command {
/**
Expand All @@ -18,6 +26,84 @@ protected function configure() {$this->setName('tfc:image')->setDescription('Pro
*/
protected function p() {
df_google_init_service_account();
$this->output()->writeln(df_dump(scandir(df_product_images_path())));
foreach ($this->images() as $i) {/** @var sttring $i */
$this->image($i);
}
}

/**
* 2020-10-26
* @used-by p()
* @param string $path
* @throws \Google\ApiCore\ApiException
*/
private function image($path) {
$a = new Annotator; /** @var Annotator $a */
$f = file_get_contents($path); /** @var string $f */
try {$res = $a->objectLocalization($f); /** @var Res $res */}
finally {$a->close();}
$oo = $res->getLocalizedObjectAnnotations(); /** @var RepeatedField $oo */
if (1 === $oo->count()) {
$this->output()->writeln($path);
# 2020-10-26 https://cloud.google.com/vision/docs/reference/rest/v1/AnnotateImageResponse#LocalizedObjectAnnotation
$o = $oo[0]; /** @var O $o */
# 2020-10-26
# https://cloud.google.com/vision/docs/reference/rest/v1/projects.locations.products.referenceImages#BoundingPoly
$bp = $o->getBoundingPoly(); /** @var BoundingPoly $bp */
$vv = $bp->getNormalizedVertices(); /** @var RepeatedField $vv */
list($w, $h) = getimagesize($path); /** @var int $w */ /** @var int $h */
$f = function($a, $b) {return round($a * $b);};
$x = function($a) use($f, $w) {return $f($w, $a);};
$y = function($a) use($f, $h) {return $f($h, $a);};
$v0 = $vv[0]; /** @var V $v0 */
$v2 = $vv[2]; /** @var V $v2 */
# 2020-10-26
# https://cloud.google.com/vision/docs/reference/rest/v1/projects.locations.products.referenceImages#NormalizedVertex
$im = imagecreatefromjpeg($path);
try {
$im2 = imagecrop($im, [
'height' => $y($v2->getY() - $v0->getY())
,'width' => $x($v2->getX() - $v0->getX())
,'x' => $x($v0->getX()), 'y' => $y($v0->getY())
]);
$path = str_replace(df_product_images_path(), df_cc_path(dirname(BP), 'result'), $path);
if (!is_dir($dir = dirname($path))) {
mkdir($dir, 777, true);
}
imagejpeg($im2, $path);
imagedestroy($im2);
}
finally {imagedestroy($im);}
}
}

/**
* 2020-10-26
* @used-by p()
* @return string[]
*/
private function images() {return dfc($this, function() {return $this->scan(
df_product_images_path(), ['cache', 'placeholder']
);});}

/**
* 2020-10-26
* @used-by images()
* @used-by scan()
* @param string $base
* @param string[] $skip [optional]
* @return string[]
*/
private function scan($base, $skip = []) {
$r = []; /** @var string[] $r */
foreach (array_diff(scandir($base = "$base/"), array_merge($skip, ['.', '..'])) as $f) {
if (is_dir($f = $base . $f)) {
$r = array_merge($r, $this->scan($f));
}
else if (in_array(strtolower(df_file_ext($f)), ['jpg', 'jpeg'])) {
$r[]= $f;
}
}
return $r;
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tradefurniturecompany/image"
,"version": "0.0.2"
,"version": "0.0.3"
,"description": "A custom image processing module for tradefurniturecompany.co.uk (Magento 2)"
,"type": "magento2-module"
,"homepage": "https://github.com/tradefurniturecompany/image"
Expand All @@ -11,7 +11,7 @@
,"homepage": "https://mage2.pro/users/dmitry_fedyuk"
,"role": "Developer"
}]
,"require": {"google/cloud-vision": "*", "mage2pro/core": ">=7.0.0"}
,"require": {"google/cloud-vision": "*", "mage2pro/core": ">=7.0.10"}
,"autoload": {"files": ["registration.php"], "psr-4": {"TFC\\Image\\": ""}}
,"keywords": [
"eCommerce"
Expand Down

0 comments on commit 31e4f8b

Please sign in to comment.