public function createVersion($imagePath, $sizeString = false, $crop = true, $fitImageInCanvas = false)
{
if (strlen($this->urlAlias) < 1) {
throw new \Exception('Image without urlAlias!');
}
$cachePath = $this->getModule()->getCachePath();
$subDirPath = $this->getSubDur();
$fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION);
if ($sizeString) {
$sizePart = '_' . $sizeString;
} else {
$sizePart = '';
}
$pathToSave = $cachePath . '/' . $subDirPath . '/' . $this->urlAlias . $sizePart . '.' . $fileExtension;
BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true);
if ($sizeString) {
$size = $this->getModule()->parseSize($sizeString);
} else {
$size = false;
}
if ($this->getModule()->graphicsLibrary == 'Imagick') {
// Fixes interlaced images
$interlaceFix = $this->interlaceFix($imagePath);
if ($interlaceFix) {
$image = new \Imagick();
$image->readImageBlob($interlaceFix);
} else {
$image = new \Imagick($imagePath);
}
$image->setImageCompressionQuality(100);
// Fixes image rotations
$this->ImagickAutoRotateImage($image);
// If the dimensions of the original image match the requested
// dimensions the original image is just copied to the new path
if ($image->getImageWidth() == $size['width'] && $image->getImageHeight() == $size['height']) {
copy($imagePath, $pathToSave);
return $image;
}
if ($size) {
if ($size['height'] && $size['width']) {
if (!$crop && $fitImageInCanvas) {
$image = $this->fitImageInCanvas($image, $size['width'], $size['height']);
} elseif ($crop) {
$image->cropThumbnailImage($size['width'], $size['height']);
} else {
$image->resizeImage($size['width'], $size['height'], \Imagick::FILTER_HAMMING, 1, false);
}
} elseif ($size['height']) {
$image->thumbnailImage(0, $size['height']);
} elseif ($size['width']) {
$image->thumbnailImage($size['width'], 0);
} else {
throw new \Exception('Something wrong with this->module->parseSize($sizeString)');
}
}
$image->writeImage($pathToSave);
} else {
$image = new \abeautifulsite\SimpleImage($imagePath);
// If the dimensions of the original image match the requested
// dimensions the original image is just copied to the new path
if ($image->get_width() == $size['width'] && $image->get_height() == $size['height']) {
copy($imagePath, $pathToSave);
return $image;
}
if ($size) {
if ($size['height'] && $size['width']) {
if ($crop) {
$image->thumbnail($size['width'], $size['height']);
} else {
$image->resize($size['width'], $size['height']);
}
} elseif ($size['height']) {
$image->fit_to_height($size['height']);
} elseif ($size['width']) {
$image->fit_to_width($size['width']);
} else {
throw new \Exception('Something wrong with this->module->parseSize($sizeString)');
}
}
//WaterMark
if ($this->getModule()->waterMark) {
if (!file_exists(Yii::getAlias($this->getModule()->waterMark))) {
throw new Exception('WaterMark not detected!');
}
$wmMaxWidth = intval($image->get_width() * 0.4);
$wmMaxHeight = intval($image->get_height() * 0.4);
$waterMarkPath = Yii::getAlias($this->getModule()->waterMark);
$waterMark = new \abeautifulsite\SimpleImage($waterMarkPath);
if ($waterMark->get_height() > $wmMaxHeight or $waterMark->get_width() > $wmMaxWidth) {
$waterMarkPath = $this->getModule()->getCachePath() . DIRECTORY_SEPARATOR . pathinfo($this->getModule()->waterMark)['filename'] . $wmMaxWidth . 'x' . $wmMaxHeight . '.' . pathinfo($this->getModule()->waterMark)['extension'];
//throw new Exception($waterMarkPath);
if (!file_exists($waterMarkPath)) {
$waterMark->fit_to_width($wmMaxWidth);
$waterMark->save($waterMarkPath, 100);
if (!file_exists($waterMarkPath)) {
throw new Exception('Cant save watermark to ' . $waterMarkPath . '!!!');
}
}
}
$image->overlay($waterMarkPath, 'bottom right', 0.5, -10, -10);
}
$image->save($pathToSave, 100);
}
return $image;
}