File "image_proccess.class.php"
Full path: /home/kosmetik/public_html/wp-content/plugins/unlimited-elements-for-elementor/inc_php/framework/image_proccess.class.php
File
size: 15.5 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');
class UniteImageViewUC
{
private $pathCache;
private $pathImages;
private $urlImages;
private $filename = null;
private $maxWidth = null;
private $maxHeight = null;
private $type = null;
private $effect = null;
private $effect_arg1 = null;
private $effect_arg2 = null;
const EFFECT_BW = "bw";
const EFFECT_BRIGHTNESS = "bright";
const EFFECT_CONTRAST = "contrast";
const EFFECT_EDGE = "edge";
const EFFECT_EMBOSS = "emboss";
const EFFECT_BLUR = "blur";
const EFFECT_BLUR3 = "blur3";
const EFFECT_MEAN = "mean";
const EFFECT_SMOOTH = "smooth";
const EFFECT_DARK = "dark";
const TYPE_EXACT = "exact";
const TYPE_EXACT_TOP = "exacttop";
private $jpg_quality;
public function __construct()
{
$this->jpg_quality = GlobalsUC::DEFAULT_JPG_QUALITY;
}
private function throwError($message, $code = null)
{
UniteFunctionsUC::throwError($message, $code);
}
private function validateType($type)
{
switch ($type) {
case self::TYPE_EXACT:
case self::TYPE_EXACT_TOP:
break;
default:
$this->throwError("Wrong image type: " . $type);
break;
}
}
private function getThumbFilename()
{
$info = pathInfo($this->filename);
$postfix = "";
$dirname = UniteFunctionsUC::getVal($info, "dirname");
if ($dirname == ".") {
$dirname = null;
}
if (!empty($dirname)) {
$postfix = str_replace("/", "-", $dirname);
}
$ext = $info["extension"];
$name = $info["filename"];
$width = ceil($this->maxWidth);
$height = ceil($this->maxHeight);
$thumbFilename = $name . "_" . $width . "x" . $height;
$this->type = trim($this->type);
if (!empty($this->type)) {
$thumbFilename .= "_" . $this->type;
}
if (!empty($this->effect)) {
$thumbFilename .= "_e" . $this->effect;
if (!empty($this->effect_arg1)) {
$thumbFilename .= "x" . $this->effect_arg1;
}
}
if (!empty($postfix)) {
$thumbFilename .= "_" . $postfix;
}
$thumbFilename .= "." . $ext;
return $thumbFilename;
}
private function getThumbFilepath()
{
$filename = $this->getThumbFilename();
$filepath = $this->pathCache . $filename;
return $filepath;
}
private function outputEmptyImageCode()
{
echo "empty image";
exit;
}
private function outputImage($filepath)
{
$info = UniteFunctionsUC::getPathInfo($filepath);
$ext = $info["extension"];
$ext = strtolower($ext);
if ($ext == "jpg") {
$ext = "jpeg";
}
$numExpires = 31536000;
$strExpires = @date('D, d M Y H:i:s', time() + $numExpires);
$contents = file_get_contents($filepath);
$filesize = strlen($contents);
header("Expires: {$strExpires} GMT");
header("Cache-Control: public");
header("Content-Type: image/{$ext}");
header("Content-Length: {$filesize}");
echo UniteProviderFunctionsUC::escCombinedHtml($contents);
exit;
}
private function getGdSrcImage($filepath, $type)
{
$src_img = false;
switch ($type) {
case IMAGETYPE_JPEG:
$src_img = @imagecreatefromjpeg($filepath);
break;
case IMAGETYPE_PNG:
$src_img = @imagecreatefrompng($filepath);
break;
case IMAGETYPE_GIF:
$src_img = @imagecreatefromgif($filepath);
break;
case IMAGETYPE_BMP:
$src_img = @imagecreatefromwbmp($filepath);
break;
default:
$this->throwError("wrong image format, can't resize");
break;
}
if ($src_img == false) {
$this->throwError("Can't resize image");
}
return $src_img;
}
private function saveGdImage($dst_img, $filepath, $type)
{
$successSaving = false;
switch ($type) {
case IMAGETYPE_JPEG:
$successSaving = imagejpeg($dst_img, $filepath, $this->jpg_quality);
break;
case IMAGETYPE_PNG:
$successSaving = imagepng($dst_img, $filepath);
break;
case IMAGETYPE_GIF:
$successSaving = imagegif($dst_img, $filepath);
break;
case IMAGETYPE_BMP:
$successSaving = imagewbmp($dst_img, $filepath);
break;
}
return $successSaving;
}
private function cropImageSaveNew($filepath, $filepathNew)
{
$imgInfo = getimagesize($filepath);
$imgType = $imgInfo[2];
$src_img = $this->getGdSrcImage($filepath, $imgType);
$width = imageSX($src_img);
$height = imageSY($src_img);
$startx = 0;
$starty = 0;
$percent = $this->maxWidth / $width;
$newWidth = $this->maxWidth;
$newHeight = ceil($percent * $height);
if ($this->type == "exact") {
$startx = 0;
$starty = ($newHeight - $this->maxHeight) / 2 / $percent;
}
if ($newHeight < $this->maxHeight) {
$percent = $this->maxHeight / $height;
$newHeight = $this->maxHeight;
$newWidth = ceil($percent * $width);
if ($this->type == "exact") {
$startx = ($newWidth - $this->maxWidth) / 2 / $percent;
$starty = 0;
}
}
$tmp_img = ImageCreateTrueColor($newWidth, $newHeight);
$this->handleTransparency($tmp_img, $imgType, $newWidth, $newHeight);
imagecopyresampled($tmp_img, $src_img, 0, 0, $startx, $starty, $newWidth, $newHeight, $width, $height);
$this->handleImageEffects($tmp_img);
$dst_img = ImageCreateTrueColor($this->maxWidth, $this->maxHeight);
$this->handleTransparency($dst_img, $imgType, $this->maxWidth, $this->maxHeight);
imagecopy($dst_img, $tmp_img, 0, 0, 0, 0, $newWidth, $newHeight);
$is_saved = $this->saveGdImage($dst_img, $filepathNew, $imgType);
imagedestroy($dst_img);
imagedestroy($src_img);
imagedestroy($tmp_img);
return $is_saved;
}
private function handleTransparency(&$dst_img, $imgType, $newWidth, $newHeight)
{
if ($imgType == IMAGETYPE_PNG || $imgType == IMAGETYPE_GIF) {
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $newWidth, $newHeight, $transparent);
}
}
private function handleImageEffects(&$imgHandle)
{
if (empty($this->effect)) {
return false;
}
switch ($this->effect) {
case self::EFFECT_BW:
if (defined("IMG_FILTER_GRAYSCALE")) {
imagefilter($imgHandle, IMG_FILTER_GRAYSCALE);
}
break;
case self::EFFECT_BRIGHTNESS:
if (defined("IMG_FILTER_BRIGHTNESS")) {
if (!is_numeric($this->effect_arg1)) {
$this->effect_arg1 = 50;
}
UniteFunctionsUC::validateNumeric($this->effect_arg1, "'ea1' argument");
imagefilter($imgHandle, IMG_FILTER_BRIGHTNESS, $this->effect_arg1);
}
break;
case self::EFFECT_DARK:
if (defined("IMG_FILTER_BRIGHTNESS")) {
if (!is_numeric($this->effect_arg1)) {
$this->effect_arg1 = -50;
}
UniteFunctionsUC::validateNumeric($this->effect_arg1, "'ea1' argument");
imagefilter($imgHandle, IMG_FILTER_BRIGHTNESS, $this->effect_arg1);
}
break;
case self::EFFECT_CONTRAST:
if (defined("IMG_FILTER_CONTRAST")) {
if (!is_numeric($this->effect_arg1)) {
$this->effect_arg1 = -5;
}
imagefilter($imgHandle, IMG_FILTER_CONTRAST, $this->effect_arg1);
}
break;
case self::EFFECT_EDGE:
if (defined("IMG_FILTER_EDGEDETECT")) {
imagefilter($imgHandle, IMG_FILTER_EDGEDETECT);
}
break;
case self::EFFECT_EMBOSS:
if (defined("IMG_FILTER_EMBOSS")) {
imagefilter($imgHandle, IMG_FILTER_EMBOSS);
}
break;
case self::EFFECT_BLUR:
$this->effect_Blur($imgHandle, 5);
break;
case self::EFFECT_MEAN:
if (defined("IMG_FILTER_MEAN_REMOVAL")) {
imagefilter($imgHandle, IMG_FILTER_MEAN_REMOVAL);
}
break;
case self::EFFECT_SMOOTH:
if (defined("IMG_FILTER_SMOOTH")) {
if (!is_numeric($this->effect_arg1)) {
$this->effect_arg1 = 15;
}
imagefilter($imgHandle, IMG_FILTER_SMOOTH, $this->effect_arg1);
}
break;
case self::EFFECT_BLUR3:
$this->effect_Blur($imgHandle, 5);
break;
default:
$this->throwError("Effect not supported: <b>" . $this->effect . "</b>");
break;
}
}
private function effect_Blur(&$gdimg, $radius = 0.5)
{
$radius = round(max(0, min($radius, 50)) * 2);
if (!$radius) {
return false;
}
$w = ImageSX($gdimg);
$h = ImageSY($gdimg);
if ($imgBlur = ImageCreateTrueColor($w, $h)) {
for ($i = 0; $i < $radius; $i++) {
ImageCopy($imgBlur, $gdimg, 0, 0, 1, 1, $w - 1, $h - 1);
ImageCopyMerge($imgBlur, $gdimg, 1, 1, 0, 0, $w, $h, 50.0);
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 1, 0, $w - 1, $h, 33.33333);
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 1, $w, $h - 1, 25.0);
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 1, 0, $w - 1, $h, 33.33333);
ImageCopyMerge($imgBlur, $gdimg, 1, 0, 0, 0, $w, $h, 25.0);
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 1, $w, $h - 1, 20.0);
ImageCopyMerge($imgBlur, $gdimg, 0, 1, 0, 0, $w, $h, 16.666667);
ImageCopyMerge($imgBlur, $gdimg, 0, 0, 0, 0, $w, $h, 50.0);
ImageCopy($gdimg, $imgBlur, 0, 0, 0, 0, $w, $h);
}
return true;
}
return false;
}
private function resizeImageSaveNew($filepath, $filepathNew)
{
$imgInfo = getimagesize($filepath);
$imgType = $imgInfo[2];
$src_img = $this->getGdSrcImage($filepath, $imgType);
$width = imageSX($src_img);
$height = imageSY($src_img);
$newWidth = $width;
$newHeight = $height;
if ($height > $this->maxHeight) {
$procent = $this->maxHeight / $height;
$newWidth = ceil($width * $procent);
$newHeight = $this->maxHeight;
}
if ($newWidth > $this->maxWidth) {
$procent = $this->maxWidth / $newWidth;
$newHeight = ceil($newHeight * $procent);
$newWidth = $this->maxWidth;
}
if ($newWidth == $width && $newHeight == $height && empty($this->effect)) {
$success = copy($filepath, $filepathNew);
if ($success == false) {
$this->throwError("can't copy the image from one path to another");
}
} else {
$dst_img = ImageCreateTrueColor($newWidth, $newHeight);
$this->handleTransparency($dst_img, $imgType, $newWidth, $newHeight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$this->handleImageEffects($dst_img);
$is_saved = $this->saveGdImage($dst_img, $filepathNew, $imgType);
imagedestroy($dst_img);
}
imagedestroy($src_img);
return true;
}
public function setEffect($effect, $arg1 = "")
{
$this->effect = $effect;
$this->effect_arg1 = $arg1;
}
public function setJPGQuality($quality)
{
$this->jpg_quality = $quality;
}
public function makeThumb($filepathImage, $pathThumbs, $maxWidth = -1, $maxHeight = -1, $type = "")
{
UniteFunctionsUC::validateFilepath($filepathImage, "image not found");
UniteFunctionsUC::validateDir($pathThumbs, "Thumbs folder don't exists.");
if ($type == self::TYPE_EXACT || $type == self::TYPE_EXACT_TOP) {
if ($maxHeight == -1) {
$this->throwError("image with exact type must have height!");
}
if ($maxWidth == -1) {
$this->throwError("image with exact type must have width!");
}
}
$info = UniteFunctionsUC::getPathInfo($filepathImage);
$filename = UniteFunctionsUC::getVal($info, "basename");
UniteFunctionsUC::validateNotEmpty($filename, "image filename not given");
if (function_exists("gd_info") == false) {
$this->throwError("php must support GD Library");
}
if ($maxWidth == -1 && $maxHeight == -1) {
$this->throwError("Wrong thumb size");
}
if ($maxWidth == -1) {
$maxWidth = 1000000;
}
if ($maxHeight == -1) {
$maxHeight = 100000;
}
$this->filename = $filename;
$this->maxWidth = $maxWidth;
$this->maxHeight = $maxHeight;
$this->type = $type;
$this->pathCache = $pathThumbs;
$filenameThumb = $this->getThumbFilename();
$filepathNew = $this->pathCache . $filenameThumb;
if (file_exists($filepathNew)) {
return $filenameThumb;
}
if ($type == self::TYPE_EXACT || $type == self::TYPE_EXACT_TOP) {
$isSaved = $this->cropImageSaveNew($filepathImage, $filepathNew);
} else {
$isSaved = $this->resizeImageSaveNew($filepathImage, $filepathNew);
}
if ($isSaved) {
return $filenameThumb;
} else {
return "";
}
}
public function convertPngDataToPng($strPngData)
{
$strPngData = str_replace("data:image/png;base64,", "", $strPngData);
$strPng = base64_decode($strPngData);
return $strPng;
}
public function convertJPGDataToJPG($strJpgData)
{
$strJpgData = str_replace("data:image/jpeg;base64,", "", $strJpgData);
$strJpg = base64_decode($strJpgData);
return $strJpg;
}
public function png2jpg($filepathPNG, $filepathJPGOutput, $quality = 70)
{
$image = imagecreatefrompng($filepathPNG);
imagejpeg($image, $filepathJPGOutput, $quality);
imagedestroy($image);
}
public function strPngToStrJpg($strPng, $quality = 70)
{
$image = imagecreatefromstring($strPng);
if (empty($image)) {
UniteFunctionsUC::throwError("can't convert image");
}
ob_start();
imagejpeg($image, null, $quality);
$strJpg = ob_get_contents();
ob_end_clean();
imagedestroy($image);
return $strJpg;
}
}