Created
August 27, 2019 12:16
-
-
Save AlekVolsk/5cbba3006d5b42f06fccbcb40ff70581 to your computer and use it in GitHub Desktop.
getCacheSquaredImages – square image cropping function
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 | |
/** | |
* getCacheSquaredImages square image cropping function | |
* | |
* (array) $imagesList unassociated array of images with relative paths from the site root folder | |
* (string) $pathCache image cache folder path relative to the root folder of the site | |
* (int) $sliderSquareSize image full square size | |
* (int) $thumbSqaureSize image thumbnail square size | |
* | |
* retunt (array) unassociated array, where each element is an array with relative folders | |
* of the root of the site paths and images: source image (key 'original'), | |
* full cropped image (key 'full'), thumbnail image (key 'thumb') | |
*/ | |
define('PATH_ROOT', 'site root directory'); | |
if (!function_exists('getCacheSquaredImages')) { | |
function getCacheSquaredImages($imagesList, $pathCache, $sliderSquareSize, $thumbSqaureSize) | |
{ | |
if (!is_array($imagesList) || !$imagesList) { | |
return false; | |
} | |
$root = str_replace('\\', '/', PATH_ROOT); | |
$result = []; | |
foreach ($imagesList as $key => $imgIn) { | |
$inPath = str_replace('\\', '/', $root . $imgIn); | |
if (!file_exists($inPath)) { | |
continue; | |
} | |
$fname = pathinfo($imgIn, PATHINFO_FILENAME); | |
$outPathFull = str_replace('\\', '/', $root . $pathCache . $fname . '__sl.jpg'); | |
$outPathThumb = str_replace('\\', '/', $root . $pathCache . $fname . '__tmb.jpg'); | |
$fdir = pathinfo($outPathFull, PATHINFO_DIRNAME); | |
if (!is_dir($fdir)) { | |
mkdir($fdir, 0755, true); | |
} | |
$fdir = pathinfo($outPathThumb, PATHINFO_DIRNAME); | |
if (!is_dir($fdir)) { | |
mkdir($fdir, 0755, true); | |
} | |
$imgInfo = getimagesize($inPath); | |
switch ($imgInfo[2]) { | |
case IMAGETYPE_JPEG: | |
case IMAGETYPE_JPEG2000: | |
$img = imagecreatefromjpeg($inPath); | |
break; | |
case IMAGETYPE_PNG: | |
$img = imagecreatefrompng($inPath); | |
break; | |
case IMAGETYPE_GIF: | |
$img = imagecreatefromgif($inPath); | |
break; | |
case IMAGETYPE_WEBP: | |
$img = imagecreatefromwebp($inPath); | |
break; | |
default: | |
$img = false; | |
} | |
if (!$img) { | |
continue; | |
} | |
if ($imgInfo[0] == $imgInfo[1]) { | |
$s = $imgInfo[0]; | |
$x = 0; | |
$y = 0; | |
} elseif ($imgInfo[0] > $imgInfo[1]) { | |
$s = $imgInfo[1]; | |
$x = round(($imgInfo[0] / 2) - ($imgInfo[1] / 2)); | |
$y = 0; | |
} else { | |
$s = $imgInfo[0]; | |
$x = 0; | |
$y = round(($imgInfo[1] / 2) - ($imgInfo[0] / 2)); | |
} | |
$resultSl = false; | |
if (file_exists($outPathFull)) { | |
$resultSl = true; | |
} else { | |
$imgSl = imagecreatetruecolor($sliderSquareSize, $sliderSquareSize); | |
if (imagecopyresampled($imgSl, $img, 0, 0, $x, $y, $sliderSquareSize, $sliderSquareSize, $s, $s)) { | |
imagejpeg($imgSl, $outPathFull, 70); | |
if (file_exists($outPathFull)) { | |
$resultSl = true; | |
} | |
} | |
} | |
$resultTmb = false; | |
if (file_exists($outPathThumb)) { | |
$resultTmb = true; | |
} else { | |
$imgTmb = imagecreatetruecolor($thumbSqaureSize, $thumbSqaureSize); | |
if (imagecopyresampled($imgTmb, $img, 0, 0, $x, $y, $thumbSqaureSize, $thumbSqaureSize, $s, $s)) { | |
imagejpeg($imgTmb, $outPathThumb, 70); | |
if (file_exists($outPathThumb)) { | |
$resultTmb = true; | |
} | |
} | |
} | |
if ($resultSl && $resultTmb) { | |
$result[$key]['original'] = str_replace($root, '', $inPath); | |
$result[$key]['full'] = str_replace($root, '', $outPathFull); | |
$result[$key]['thumb'] = str_replace($root, '', $outPathThumb); | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment