ImageResizer.php 5.53 KB
<?php

namespace frontend\components;

use Yii;
use yii\imagine\Image;
use Imagine\Image\Point;
use Imagine\Image\ManipulatorInterface;

/**
 *
 * @author FilamentV <vortex.filament@gmail.com>
 * @copyright (c), Thread
 */
class ImageResizer
{
    const BASE_PATH_TO_CACHE = '@uploads/thumbs';
    const BASE_URL_TO_CACHE = '/uploads/thumbs/';

    /**
     * @param $image
     * @param $width
     * @param $height
     * @param string $prefix
     * @return string
     */
    public static function generateName($image, $width, $height, $prefix = 'thumb{$width}_{$height}.')
    {
        return str_replace(['{$width}', '{$height}'], [$width, $height], $prefix) . basename($image);
    }

    /**
     * @param $image
     * @param $width
     * @param $height
     * @param string $prefix
     * @return null|string
     */
    public static function getThumb($image, $width, $height, $prefix = 'thumb{$width}_{$height}.')
    {
        if (is_file($image)) {
            $base = Yii::getAlias(self::BASE_PATH_TO_CACHE) . '/';
            $name = self::generateName($image, $width, $height, $prefix);

            if (is_file($base . $name)) {
                return self::BASE_URL_TO_CACHE . $name;
            }
            $image = Image::thumbnail($image, $width, $height, ManipulatorInterface::THUMBNAIL_INSET);
            if ($image->save($base . $name, [
                'jpeg_quality' => 90,
            ])
            ) {
                return self::BASE_URL_TO_CACHE . $name;
            }
        }
        return null;
    }

    /**
     * @param $image
     * @param $watermark
     * @param $color
     * @param $prefix
     * @return null|string
     */
    public static function getSign($image, $watermark, $color, $prefix)
    {
        if (is_file($image) && is_file($watermark) && !empty($color)) {
            $base = Yii::getAlias(self::BASE_PATH_TO_CACHE) . '/';
            $name = basename($image);
            if (is_file($base . $prefix . $name)) {
                return self::BASE_URL_TO_CACHE . $prefix . $name;
            }
            self::changeColor($image, $base . $prefix . $name, $color);
            self::setWatermark($base . $prefix . $name, $base . $prefix . $name, $watermark);

            return self::BASE_URL_TO_CACHE . $prefix . $name;
        } else {
//            echo 'no';
        }
        return null;
    }

    /**
     * @param $source
     * @param $destination
     * @param $watermark
     */
    public static function setWatermark($source, $destination, $watermark)
    {
        if (is_file($source) && is_file($watermark)) {

            $watermark = Image::getImagine()->open($watermark);
            $image = Image::getImagine()->open($source);
            $size = $image->getSize();
            $wSize = $watermark->getSize();
            //
            $image->paste($watermark, new Point(($size->getWidth() - $wSize->getWidth()) / 2, ($size->getHeight() - $wSize->getHeight()) / 2));
            //
            $image->save($destination);
        }
    }

    /**
     * @param $source
     * @param $destination
     * @param $color
     * @return null|string
     */
    public static function changeColor($source, $destination, $color)
    {
//        echo $color;
        if (is_file($source)) {
            /* Convert hexdec color string to rgb(a) string */

            /**
             * @param $image
             * @param $color
             */


            $im = imagecreatefrompng($source);
            imagealphablending($im, true); // setting alpha blending on
            imagesavealpha($im, true); // save alphablending setting (important)
            static::imagefon($im, $color);
            imagepng($im, $destination);

            return $destination;
        }
        return null;
    }

    /**
     * @param $image
     * @param $color
     */
    public static function imagefon(&$image, $color)
    {
        $c = ImageResizer::hex2rgba($color);
        $width = imagesx($image);
        $height = imagesy($image);
//        $gray = imagecolorat($image, 0, 0);

        for ($x = 0; $x < $width; $x++) {
            for ($y = 0; $y < $height; $y++) {
                $rgb = imagecolorat($image, $x, $y);

                if (16777215 == $rgb) {
//                            imagesetpixel($image, $x, $y, 19997000);
                    imagesetpixel($image, $x, $y, imagecolorallocate($image, $c[0], $c[1], $c[2]));
                }
//                                imagesetpixel($image, $x, $y, imagecolorallocate($image, 0, 0, 0));
//                            } else {
//                                imagesetpixel($image, $x, $y, imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0)));
//                            }
            }
        }
    }

    /**
     * @param $color
     * @return array
     */
    public static function hex2rgba($color)
    {
        //
        $default = [0, 0, 0];
        //Return default if no color provided
        if (empty($color)) {
            return $default;
        }
        //Sanitize $color if "#" is provided
        if ($color[0] == '#') {
            $color = substr($color, 1);
        }
        //Check if color has 6 or 3 characters and get values
        if (strlen($color) == 6) {
            $hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
        } elseif (strlen($color) == 3) {
            $hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
        } else {
            return $default;
        }
        //Convert hexadec to rgb
        $rgb = array_map('hexdec', $hex);
        //
        return $rgb;
    }
}