Created
February 10, 2019 10:37
-
-
Save AlekVolsk/e075d898a57231da77c008d8da04214e to your computer and use it in GitHub Desktop.
Нарезка тумбочек для Joomla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Usage | |
$imgIn = str_replace('\\', '/', $imgIn ); | |
$imgOut = str_replace('\\', '/', str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR, '', JPATH_CACHE) . DIRECTORY_SEPARATOR . $imgIn ); | |
if (!file_exists( JPATH_ROOT . DIRECTORY_SEPARATOR . $imgOut )) { | |
getCacheImageSquare($imgIn, $imgOut); | |
} | |
*/ | |
use Joomla\CMS\Filesystem\Folder; | |
use Joomla\CMS\Image\Image; | |
// уменьшить до указанной ширины | |
function getCacheImageW($imgIn, $imgOut, $size = 400) | |
{ | |
if (!$imgIn || !$imgOut) { | |
return false; | |
} | |
$in = JPATH_ROOT . DIRECTORY_SEPARATOR . $imgIn; | |
$out = JPATH_ROOT . DIRECTORY_SEPARATOR . $imgOut; | |
if (!file_exists($in)) { | |
return false; | |
} | |
Folder::create(dirname($out), 0755); | |
$image = new Image($in); | |
$mode = $image->getWidth() > $image->getHeight() ? Image::SCALE_INSIDE : Image::SCALE_OUTSIDE; | |
$image->resize((int)$size, (int)$size, false, $mode); | |
$image->toFile($out); | |
return file_exists($out); | |
} | |
// уменьшить до указанной высоты | |
function getCacheImageH($imgIn, $imgOut, $size = 200) | |
{ | |
if (!$imgIn || !$imgOut) { | |
return false; | |
} | |
$in = JPATH_ROOT . DIRECTORY_SEPARATOR . $imgIn; | |
$out = JPATH_ROOT . DIRECTORY_SEPARATOR . $imgOut; | |
if (!file_exists($in)) { | |
return false; | |
} | |
Folder::create(dirname($out), 0755); | |
$image = new Image($in); | |
$mode = $image->getWidth() < $image->getHeight() ? Image::SCALE_INSIDE : Image::SCALE_OUTSIDE; | |
$image->resize((int)$size, (int)$size, false, $mode); | |
$image->toFile($out); | |
return file_exists($out); | |
} | |
// обрезать до квадрата указанного размера | |
function getCacheImageSquare($imgIn, $imgOut, $size = 400) | |
{ | |
if (!$imgIn || !$imgOut) { | |
return false; | |
} | |
$in = JPATH_ROOT . DIRECTORY_SEPARATOR . $imgIn; | |
$out = JPATH_ROOT . DIRECTORY_SEPARATOR . $imgOut; | |
if (!file_exists($in)) { | |
return false; | |
} | |
Folder::create(dirname($out), 0755); | |
$image = new Image($in); | |
$image->cropResize((int)$size, (int)$size, false); | |
$image->toFile($out); | |
return file_exists($out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment