Skip to content

Commit

Permalink
Merge pull request #12 from bizley/path-configs
Browse files Browse the repository at this point in the history
Option to set view and save path for images
  • Loading branch information
Bizley authored Nov 6, 2019
2 parents c194a67 + d93cd87 commit 18d80bd
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
vendor
docker
runtime
composer.lock
49 changes: 30 additions & 19 deletions src/ContentTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* @author Paweł Bizley Brzozowski
* @version 1.4.0
* @version 1.5.0
* @license Apache 2.0
* https://github.com/bizley/yii2-content-tools
*
Expand Down Expand Up @@ -181,7 +181,7 @@ class ContentTools extends Widget
* @since 1.3.0
*/
public $customJs;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -395,15 +395,22 @@ public function addTranslation()
public function initImagesEngine()
{
if ($this->imagesEngine !== false) {
if (empty($this->imagesEngine['upload']) || empty($this->imagesEngine['rotate']) || empty($this->imagesEngine['insert'])) {
if (
empty($this->imagesEngine['upload'])
|| empty($this->imagesEngine['rotate'])
|| empty($this->imagesEngine['insert'])
) {
throw new InvalidConfigException('Invalid options for the imagesEngine configuration!');
}

ContentToolsImagesAsset::register($this->getView());

$this->registerCsrfToken();

$this->getView()->registerJs(";var _CTImagesUrl = ['{$this->imagesEngine['upload']}', '{$this->imagesEngine['rotate']}', '{$this->imagesEngine['insert']}'];", View::POS_BEGIN);
$this->getView()->registerJs(
";var _CTImagesUrl = ['{$this->imagesEngine['upload']}', '{$this->imagesEngine['rotate']}', '{$this->imagesEngine['insert']}'];",
View::POS_BEGIN
);
} else {
$this->registerCsrfToken(true);

Expand All @@ -429,7 +436,7 @@ public function registerCsrfToken($empty = false)
*/
public function addStyles()
{
if (!empty($this->styles)) {
if (count($this->styles)) {
$newStyles = [];

foreach ($this->styles as $name => $style) {
Expand All @@ -451,10 +458,10 @@ public function addStyles()
$tags = explode(',', $style['tags']);

foreach ($tags as $tag) {
$possible_tag = str_replace("'", '', trim($tag));
$possibleTag = str_replace("'", '', trim($tag));

if (!empty($possible_tag)) {
$addTags[] = $possible_tag;
if (!empty($possibleTag)) {
$addTags[] = $possibleTag;
}
}
} else {
Expand All @@ -463,10 +470,10 @@ public function addStyles()
throw new InvalidConfigException('Invalid options for styles configuration!');
}

$possible_tag = str_replace("'", '', trim($tag));
$possibleTag = str_replace("'", '', trim($tag));

if (!empty($possible_tag)) {
$addTags[] = $possible_tag;
if (!empty($possibleTag)) {
$addTags[] = $possibleTag;
}
}
}
Expand All @@ -487,7 +494,10 @@ public function addStyles()
}

if (!empty($newStyles)) {
$this->getView()->registerJs('ContentTools.StylePalette.add([' . implode(',', $newStyles) . ']);', View::POS_END);
$this->getView()->registerJs(
'ContentTools.StylePalette.add([' . implode(',', $newStyles) . ']);',
View::POS_END
);
}
}
}
Expand Down Expand Up @@ -520,16 +530,16 @@ public function init()
{
parent::init();

if (!empty($this->page) && !is_string($this->page)) {
if ($this->page !== null && (!is_string($this->page) || $this->page === '')) {
throw new InvalidConfigException('Invalid page configuration!');
}
if (!is_string($this->tag)) {
if (!is_string($this->tag) || $this->tag === '') {
throw new InvalidConfigException('Invalid tag configuration!');
}
if (!is_string($this->dataInit)) {
if (!is_string($this->dataInit) || $this->dataInit === '') {
throw new InvalidConfigException('Invalid dataInit configuration!');
}
if (!is_string($this->dataName)) {
if (!is_string($this->dataName) || $this->dataName === '') {
throw new InvalidConfigException('Invalid dataName configuration!');
}
if (!is_array($this->options)) {
Expand All @@ -544,10 +554,10 @@ public function init()
if (!is_array($this->styles)) {
throw new InvalidConfigException('Invalid styles configuration!');
}
if (!is_string($this->language) && $this->language !== false && $this->language !== true) {
if ($this->language === '' || (!is_string($this->language) && !is_bool($this->language))) {
throw new InvalidConfigException('Invalid language configuration!');
}
if ($this->globalConfig !== false && $this->globalConfig !== true) {
if (!is_bool($this->globalConfig)) {
throw new InvalidConfigException('Invalid globalConfig configuration!');
}

Expand All @@ -558,12 +568,13 @@ public function init()

/**
* Returns data-* attribute with the given name.
* Since 1.5.0 attribute value is HTML-encoded.
* @param string $attribute Attribute name,
* @return string
*/
public static function dataAttribute($attribute)
{
return 'data-' . $attribute;
return 'data-' . Html::encode($attribute);
}

/**
Expand Down
51 changes: 38 additions & 13 deletions src/actions/InsertAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Yii;
use yii\base\Action;
use yii\base\InvalidParamException;
use yii\helpers\FileHelper;
use yii\helpers\Json;
use yii\imagine\Image;

Expand All @@ -25,13 +26,36 @@
* - `0,0,1,1` means full image with no cropping and
* - `0.25,0.25,0.75,0.75` means that box half the size of the full image in the center needs to be cropped from it.
*
* 0 1
* 0 +--------+
* | |
* | |
* | |
* 1 +--------+
*
* Cropping is handled by the Imagine library through yii2-imagine extension.
* JS engine can add `?_ignore=...` part to the url so it should be removed.
* If `crop` parameter is not set image should be inserted in the content as-is.
* Action returns the size, URL, and alternative description of image (cropped or not).
*/
class InsertAction extends Action
{
/**
* @var string Image upload folder. This can be Yii alias.
* Example: /var/www/site/web/images
* This value should be the same for all actions $uploadDir.
* @since 1.5.0
*/
public $uploadDir = '@webroot/content-tools-uploads';

/**
* @var string Web accesible path to upload folder. This can be Yii alias.
* Example: /images
* This value should be the same for all actions $viewPath.
* @since 1.5.0
*/
public $viewPath = '@web/content-tools-uploads';

/**
* {@inheritdoc}
*/
Expand All @@ -49,16 +73,17 @@ public function run()
}

$url = trim($data['url']);

if (strpos($url, '/') === 0) {
$url = substr($url, 1);
}

if (strpos($url, '?_ignore=') !== false) {
$url = substr($url, 0, strpos($url, '?_ignore='));
$imageName = substr($url, strrpos($url, '/') + 1);
if (strpos($imageName, '?_ignore=') !== false) {
$imageName = substr($imageName, 0, strpos($imageName, '?_ignore='));
}
$imagePath = FileHelper::normalizePath(
Yii::getAlias(FileHelper::normalizePath($this->uploadDir, '/'))
. DIRECTORY_SEPARATOR
. $imageName
);

$imageSizeInfo = @getimagesize($url);
$imageSizeInfo = @getimagesize($imagePath);

if ($imageSizeInfo === false) {
throw new InvalidParamException('Parameter "url" seems to be invalid!');
Expand Down Expand Up @@ -86,20 +111,20 @@ public function run()
list($width, $height) = $imageSizeInfo;

Image::crop(
$url,
$imagePath,
floor($width * $positions[3] - $width * $positions[1]),
floor($height * $positions[2] - $height * $positions[0]),
[
floor($width * $positions[1]),
floor($height * $positions[0])
]
)->save($url);
)->save($imagePath);
}

return Json::encode([
'size' => @getimagesize($url),
'url' => '/' . $url,
'alt' => basename($url)
'size' => @getimagesize($imagePath), // not using $imageSizeInfo because it's new size
'url' => Yii::getAlias(FileHelper::normalizePath($this->viewPath, '/') . '/' . $imageName),
'alt' => $imageName
]);

} catch (Exception $e) {
Expand Down
50 changes: 38 additions & 12 deletions src/actions/RotateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Yii;
use yii\base\Action;
use yii\base\InvalidParamException;
use yii\helpers\FileHelper;
use yii\helpers\Json;
use yii\imagine\Image;

Expand All @@ -28,6 +29,22 @@
*/
class RotateAction extends Action
{
/**
* @var string Image upload folder. This can be Yii alias.
* Example: /var/www/site/web/images
* This value should be the same for all actions $uploadDir.
* @since 1.5.0
*/
public $uploadDir = '@webroot/content-tools-uploads';

/**
* @var string Web accesible path to upload folder. This can be Yii alias.
* Example: /images
* This value should be the same for all actions $viewPath.
* @since 1.5.0
*/
public $viewPath = '@web/content-tools-uploads';

/**
* {@inheritdoc}
*/
Expand All @@ -40,31 +57,40 @@ public function run()
try {
$data = Yii::$app->request->post();

if (empty($data['url']) || !in_array($data['direction'], ['CW', 'CCW'], true)) {
if (
empty($data['url'])
|| empty($data['direction'])
|| !in_array($data['direction'], ['CW', 'CCW'], true)
) {
throw new InvalidParamException('Invalid rotate options!');
}

$url = trim($data['url']);

if (strpos($url, '/') === 0) {
$url = substr($url, 1);
}

if (strpos($url, '?_ignore=') !== false) {
$url = substr($url, 0, strpos($url, '?_ignore='));
$imageName = substr($url, strrpos($url, '/') + 1);
if (strpos($imageName, '?_ignore=') !== false) {
$imageName = substr($imageName, 0, strpos($imageName, '?_ignore='));
}
$imagePath = FileHelper::normalizePath(
Yii::getAlias(FileHelper::normalizePath($this->uploadDir, '/'))
. DIRECTORY_SEPARATOR
. $imageName
);

$imageSizeInfo = @getimagesize($url);
$imageSizeInfo = @getimagesize($imagePath);

if ($imageSizeInfo === false) {
throw new InvalidParamException('Parameter "url" seems to be invalid!');
}

Image::getImagine()->open($url)->copy()->rotate($data['direction'] === 'CW' ? 90 : -90)->save($url);
Image::getImagine()
->open($imagePath)
->copy()
->rotate($data['direction'] === 'CW' ? 90 : -90)
->save($imagePath);

return Json::encode([
'size' => $imageSizeInfo,
'url' => '/' . $url
'size' => @getimagesize($imagePath), // new size
'url' => Yii::getAlias(FileHelper::normalizePath($this->viewPath, '/') . '/' . $imageName),
]);

} catch (Exception $e) {
Expand Down
2 changes: 2 additions & 0 deletions src/actions/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class UploadAction extends Action
/**
* @var string Image upload folder. This can be Yii alias.
* Example: /var/www/site/web/images
* This value should be the same for all actions $uploadDir.
* @since 1.4.0
*/
public $uploadDir = '@webroot/content-tools-uploads';

/**
* @var string Web accesible path to upload folder. This can be Yii alias.
* Example: /images
* This value should be the same for all actions $viewPath.
* @since 1.4.0
*/
public $viewPath = '@web/content-tools-uploads';
Expand Down
14 changes: 5 additions & 9 deletions src/models/ImageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,15 @@ public function upload()
}

try {
$save_path = Yii::getAlias($this->_uploadDir);
// first normalize for alias translating then for OS
$save_path = FileHelper::normalizePath(Yii::getAlias(FileHelper::normalizePath($this->_uploadDir, '/')));
FileHelper::createDirectory($save_path);

$image = $this->image->baseName . '.' . $this->image->extension;
$this->path = $save_path . DIRECTORY_SEPARATOR . $image;
$this->url = Yii::getAlias(FileHelper::normalizePath($this->_viewPath, '/') . '/' . $image);

if (!$this->image->saveAs(FileHelper::normalizePath($save_path . '/' . $image))) {
return false;
}

$this->path = Yii::getAlias($save_path . '/' . $image);
$this->url = Yii::getAlias($this->_viewPath . '/' . $image);

return true;
return $this->image->saveAs($this->path);

} catch (Exception $e) {
Yii::error($e->getMessage());
Expand Down

0 comments on commit 18d80bd

Please sign in to comment.