Skip to content

Instantly share code, notes, and snippets.

@AlekVolsk
Created February 10, 2019 10:37
Show Gist options
  • Save AlekVolsk/e075d898a57231da77c008d8da04214e to your computer and use it in GitHub Desktop.
Save AlekVolsk/e075d898a57231da77c008d8da04214e to your computer and use it in GitHub Desktop.
Нарезка тумбочек для Joomla
<?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