Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Added support for jcrop
Browse files Browse the repository at this point in the history
  • Loading branch information
tonchik-tm committed Oct 15, 2020
1 parent 0354020 commit 76f5ff7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# IntelliJ project files
.idea
/lsp
/tests
/vendor
/composer.lock
21 changes: 18 additions & 3 deletions ImageThumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\ManipulatorInterface;
use Imagine\Image\Point;
use Yii;
use yii\base\ErrorException;
use yii\base\InvalidConfigException;
Expand All @@ -26,6 +27,7 @@ class ImageThumb
const THUMBNAIL_OUTBOUND = ManipulatorInterface::THUMBNAIL_OUTBOUND;
const THUMBNAIL_INSET = ManipulatorInterface::THUMBNAIL_INSET;
const THUMBNAIL_INSET_BOX = 'inset_box';
const THUMBNAIL_CROP = 'crop_box';
const QUALITY = 60;
const MKDIR_MODE = 0755;

Expand Down Expand Up @@ -74,6 +76,7 @@ public static function thumbFile($filename, $width, $height, $options=[])
'format' => null, // format finally file
'mode' => self::THUMBNAIL_OUTBOUND, // mode of resizing original image to use in case both width and height specified
'quality' => null,
'crop' => null,
'cacheMode' => self::CHECK_REM_MODE_NONE, // check file version on remote server
], $options);

Expand All @@ -83,6 +86,8 @@ public static function thumbFile($filename, $width, $height, $options=[])
if (\preg_match('/^https?:\/\//', $filename)) {
$fileNameIsUrl = true;
$commonCacheData = $filename . $width . $height . $o['mode'];
if (!empty($o['crop']['coord'])) $commonCacheData .= implode('', $o['crop']['coord']);

switch ($o['cacheMode']) {
case self::CHECK_REM_MODE_NONE:
$thumbnailFileName = \md5($commonCacheData);
Expand All @@ -103,7 +108,9 @@ public static function thumbFile($filename, $width, $height, $options=[])
if (!\is_file($filename)) {
throw new FileNotFoundException("File {$filename} doesn't exist");
}
$thumbnailFileName = \md5($filename . $width . $height . $o['mode'] . \filemtime($filename));
$commonCacheData = $filename . $width . $height . $o['mode'];
if (!empty($o['crop']['coord'])) $commonCacheData .= implode('', $o['crop']['coord']);
$thumbnailFileName = \md5($commonCacheData . \filemtime($filename));
}

$cachePath = Yii::getAlias('@webroot/' . static::$cacheAlias);
Expand Down Expand Up @@ -132,6 +139,14 @@ public static function thumbFile($filename, $width, $height, $options=[])

if ($o['mode'] === self::THUMBNAIL_INSET_BOX) {
$image = $image->thumbnail(new Box($width, $height), ManipulatorInterface::THUMBNAIL_INSET);
} elseif ($o['mode'] === self::THUMBNAIL_CROP) {
$s = $o['crop'];
if (!empty($s['source'])) {
$ratio = $s['ratio'] ?? 1;
$image = $image->resize(new Box($s['source'][0]*$ratio, $s['source'][1]*$ratio));
}
$image = $image->crop(new Point($s['coord']['x']*$ratio, $s['coord']['y']*$ratio), new Box($s['coord']['w']*$ratio, $s['coord']['h']*$ratio));
$image->thumbnail(new Box($width, $height), ManipulatorInterface::THUMBNAIL_INSET);
} else {
$image = Image::thumbnail($image, $width, $height, $o['mode']);
}
Expand Down Expand Up @@ -234,8 +249,8 @@ public static function thumbPicture($filename, $width, $height, $options = [])

return
Html::beginTag('picture', $pictureOptions) . "\n\t" .
self::thumbSource($filename, $width, $height, $sourceOptions) . "\n\t" .
self::thumbImg($filename, $width, $height, $oImg) . "\n" .
self::thumbSource($filename, $width, $height, $sourceOptions) . "\n\t" .
self::thumbImg($filename, $width, $height, $oImg) . "\n" .
Html::endTag('picture');
}

Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ For example:
```php
use TonchikTm\Yii2Thumb\ImageThumb;

$mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET;
$mode = ImageThumb::THUMBNAIL_INSET;
$options = [
'source' => [
'format' => 'webp',
Expand Down Expand Up @@ -59,6 +59,25 @@ In the output we will get something like this code:
</picture>
```

If you use [Jcrop](http://deepliquid.com/content/Jcrop.html), you can use mode `ImageThumb::THUMBNAIL_CROP`:

```php
<?php
use TonchikTm\Yii2Thumb\ImageThumb;

$crop = [
'source' => [450, 450], // Size source image where create crop coordinates
'coord' => ['x'=>100,'y'=>50,'x2'=>350,'y2'=>200,'w'=>250,'h'=>150,], // data from jCrop
];
$options = [
'mode' => ImageThumb::THUMBNAIL_CROP,
'crop' => $crop,
];
?>
<hr>
<?= ImageThumb::thumbImg(Yii::getAlias('@webroot/assets/example.png'), 250, 150, $options) . "\n"; ?>
```

For other functions please see the source code.

The package is in the process of developing

0 comments on commit 76f5ff7

Please sign in to comment.