@param bool|null $alphaBlending
@param bool|null $saveAlpha

@return Image
554593
private function setTransparency(?bool $alphaBlending = null, ?bool $saveAlpha = null): self
    {
        imagecolortransparent($this->workingData, imagecolorallocate($this->workingData, 0, 0, 0));
        if (null !== $alphaBlending) {
            imagealphablending($this->workingData, $alphaBlending);
        }
        if (null !== $saveAlpha) {
            imagesavealpha($this->workingData, $saveAlpha);
        }
        return $this;
    }
947331
public function stroke($BrowserExpire = false)
    {
        if ($this->TransparentBackground) {
            imagealphablending($this->Picture, false);
            imagesavealpha($this->Picture, true);
        }

        if ($BrowserExpire) {
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Cache-Control: no-cache");
            header("Pragma: no-cache");
        }

        header('Content-type: image/png');
        imagepng($this->Picture);
    }
169256
public static void changeAlpha(InputStream srcInput, OutputStream destOutput,
                                   byte alpha) throws IOException {
        changeAlpha(srcInput, destOutput, alpha, null);
    }
962373
public function save($bOnDuplicateKeyUpdate = false)
    {
        /**
         * Trigger on an entity to initialize it before the save
         */
        if (method_exists(get_called_class(), 'beforeSave')) { static::beforeSave(); }

        $mPrimaryKeyName = $this->_mPrimaryKeyName;

        if ($bOnDuplicateKeyUpdate === false) { $bInsertMode = false; }
        else { $bInsertMode = true; }

        if ($mPrimaryKeyName === false) {

            throw new \Exception('['.__FILE__.' (l.'.__LINE__.'] no primary key on this table!');
        }
        else if (is_string($mPrimaryKeyName)) {

            $sMethodPrimaryKey = 'get_'.$this->_mPrimaryKeyNameWithoutMapping;
            $aPrimaryKey = array($mPrimaryKeyName => $this->$sMethodPrimaryKey());

            if ($this->$sMethodPrimaryKey() < 1) { $bInsertMode = true; }
        }
        else {

            $aPrimaryKey = array();

            $oOrm = new Orm;

            $iResults = $oOrm->select(array('*'))
                ->from(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class()));

            $oWhere = new Where;

            foreach($mPrimaryKeyName as $sKey => $sPrimaryKey) {

                $sMethodPrimaryKey = 'get_'.$this->_mPrimaryKeyNameWithoutMapping[$sKey];
                $aPrimaryKey[$sPrimaryKey] = $this->$sMethodPrimaryKey();


                $oWhere->andWhereEqual($sPrimaryKey, $aPrimaryKey[$sPrimaryKey]);
            }

            $aResults = $oOrm->where($oWhere)
                ->load();

            if (count($aResults) < 1) { $bInsertMode = true; }
        }

        $aEntityTmp = get_object_vars(LibEntity::getRealEntity($this));
        $aEntity = array();

        foreach ($aEntityTmp as $sKey => $mField) {

            if ($mField !== null && !is_object($mField)) {

                $aEntity[$sKey] = $mField;
            }
        }

        /**
         * check if the virtual foreign key in this model is respected
         */
        if (count($this->_aForeignKey) > 0) {

            foreach ($this->_aForeignKey as $sName => $aForeignKey) {

                if ($aForeignKey['has_one'] == 1) {

                    $sMethodPrimaryKey = 'get_'.$aForeignKey['foreign_key'];
                    $mFIeld = $this->$sMethodPrimaryKey();

                    if ($mFIeld) {

                        $oOrm = new Orm;

                        $iResults = $oOrm->select(array('*'))
                            ->from($aForeignKey['entity_join_name']);

                        $oWhere = new Where;

                        $oWhere->whereEqual($aForeignKey['primary_key_name'], $mFIeld);

                        $aResults = $oOrm->where($oWhere)
                            ->load();

                        if (count($aResults) < 1) {

                            if ($aForeignKey['foreign_key_options']['message']) {

                                throw new \Exception($aForeignKey['foreign_key_options']['message']);
                            }
                            else {

                                throw new \Exception('Foreign Key not respected!');
                            }
                        }
                    }
                }
            }
        }

        /**
         * check if the virtual foreign key in the others models are respected
         */
        $oReflectionClass  = new \ReflectionClass(get_called_class());
        $oReflectionProperties = $oReflectionClass->getProperties();

        foreach($oReflectionProperties as $mKey => $aOne) {

            $sCommentPhpDoc = $aOne->getDocComment();

            if (strstr($sCommentPhpDoc, '@OneToMany')) {

                $sClassName = $aOne->class;
                $oClass = new $sClassName;

                if (count($oClass->getForeignKey()) > 0) {

                    foreach ($oClass->getForeignKey() as $sName => $aForeignKey) {

                        if ($aForeignKey['has_many'] == 1) {

                            $sMethodPrimaryKey = 'get_'.$aForeignKey['foreign_key'];
                            $mFIeld = $this->$sMethodPrimaryKey();

                            if ($mFIeld) {

                                $oOrm = new Orm;

                                $iResults = $oOrm->select(array('*'))
                                    ->from($aForeignKey['entity_join_name']);

                                $oWhere = new Where;

                                $oWhere->whereEqual($aForeignKey['primary_key_name'], $mFIeld);

                                $aResults = $oOrm->where($oWhere)
                                    ->load();

                                if (count($aResults) < 1) {

                                    if ($aForeignKey['foreign_key_options']['message']) {

                                        throw new \Exception($aForeignKey['foreign_key_options']['message']);
                                    }
                                    else {

                                        throw new \Exception('Foreign Key not respected!');
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        $oOrm = new Orm;

        if ($bInsertMode === true) {

            $oOrm->insert(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class()))
                ->values($aEntity);

            if ($bOnDuplicateKeyUpdate === true) {

                $oOrm->onDuplicateKeyUpdate($aEntity);
            }

            $iResults = $oOrm->save();
        }
        else {

            $iResults = $oOrm->update(preg_replace('/^.*\\\\([a-zA-Z0-9_]+)$/', '$1', get_called_class()))
                ->set($aEntity)
                ->where($aPrimaryKey)
                ->save();
        }

        if (is_string(LibEntity::getPrimaryKeyName($this))) {
            $methodName = 'set_'.LibEntity::getPrimaryKeyName($this);
            $this->$methodName($iResults);
        }

        return $iResults;
    }
279802
public function save() : AbstractResize
    {
        if ($this->file === null) {
            $this->file = str_replace($this->extension, '-copy' . $this->extension, $this->source['file']);
        }
        if ($this->path === null) {
            $this->path = $this->source['path'];
        }

        $this->saveFile();

        return $this;
    }
497015
public static function png($text, $outfile = false, $level = Constants::QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
    {
        $enc = QRencode::factory($level, $size, $margin);
        return $enc->encodePNG($text, $outfile, $saveandprint);
    }
1176045
final public function imageAsString($as = null)
	{
		$image = $this->imageAsDOMElement($as);
		return $image->ownerDocument->saveHTML($image);
	}
285601
public static void changeAlpha(String oldPath, String newPath, byte alpha) throws IOException {
        changeAlpha(new FileInputStream(oldPath), new FileOutputStream(newPath), alpha);
    }
546307
public function contrast($alpha = null, $beta = null) {
        $params = array();

        if ($alpha !== null) {
            $params[] = 'alpha=' . (float) $alpha;
        }

        if ($beta !== null) {
            $params[] = 'beta=' . (float) min(1, max(0, $beta));
        }

        return $this->addTransformation(
            'contrast' . ($params ? ':' . implode(',', $params) : '')
        );
    }
1183254
public function storage($image, ?string $alt = null, array $extra = []): ?string
    {
        return $this->generateImagePayload($image, $alt, $extra, true, true)->htmlstring ?? null;
    }
188946
public function save($savePath, $imageQuality = 95)
        {
            // If the image wasn't resized, fetch original image.
            if (!$this->imageResized) {
                $this->imageResized = $this->image;
            }

            // Get extension of the output file.
            $extension = Inflector::lower(File::extension($savePath));

            // Create and save an image based on it's extension.
            switch($extension)
            {
                case 'jpg':
                case 'jpeg':
                    if (imagetypes() & IMG_JPG) {
                        imagejpeg($this->imageResized, $savePath, $imageQuality);
                    }
                    break;

                case 'gif':
                    if (imagetypes() & IMG_GIF) {
                        imagegif($this->imageResized, $savePath);
                    }
                    break;

                case 'png':
                    // Scale quality from 0-100 to 0-9.
                    $scaleQuality = round(($imageQuality / 100) * 9);

                    // Invert quality setting as 0 is best, not 9.
                    $invertScaleQuality = 9 - $scaleQuality;

                    if (imagetypes() & IMG_PNG) {
                        imagepng($this->imageResized, $savePath, $invertScaleQuality);
                    }
                    break;

                default:
                    return false;
                    break;
            }

            // Remove the resource for the resized image.
            imagedestroy($this->imageResized);

            return true;
        }
27569
public function mapDataAndSaveObject( array $aData, array $aMap, $oObject = null )
    {
        if ( is_null( $oObject ) ) {
            $oObject = $this;
        }

        if ( $this->_mapDataFieldsToObject( $aData, $aMap, $oObject ) ) {
            return $oObject->save();
        }

        return null;
    }
118342
public function setSaveAsExtension($saveAs = null)
    {
        if (isset($saveAs)) {
            $saveAs = strtolower($saveAs);
            $this->checkFileExtension($saveAs);
            $this->saveAs = $saveAs;
            $this->extension = $saveAs;
        }

        $this->log("Prepare to save image as: " . $this->extension);

        return $this;
    }
643274
public function encodePNG($intext, $outfile = false,$saveandprint=false)
    {
        try {
            ob_start();
            $tab = $this->encode($intext);
            $err = ob_get_contents();
            ob_end_clean();

            if ($err != '')
                QRtools::log($outfile, "ERROR: " . $err);

            $maxSize = (int)(Constants::QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));

            QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
        } catch (Exception $e) {
            echo $e->getMessage();
            die();

            QRtools::log($outfile, $e->getMessage());
        }
    }
1349392
private function saveAsWebp($imageInstance, $path, $sourceExtension, $saveOptions)
    {
        /** @var ConfigModel $settings */
        $config = ImagerService::getConfig();

        if ($config->getSetting('useCwebp')) {

            // Save temp file
            $tempFile = $this->saveTemporaryFile($imageInstance, $sourceExtension);

            // Convert to webp with cwebp
            $command = escapeshellcmd($config->getSetting('cwebpPath').' '.$config->getSetting('cwebpOptions').' -q '.$saveOptions['webp_quality'].' "'.$tempFile.'" -o "'.$path.'"');
            $r = shell_exec($command);

            if (!file_exists($path)) {
                $msg = Craft::t('imager', 'Creation of webp with cwebp failed with error "'.$r. '". The executed command was "' . $command . '"');
                Craft::error($msg, __METHOD__);
                throw new ImagerException($msg);
            }

            // Delete temp file
            unlink($tempFile);
        } else {
            if (ImagerService::$imageDriver === 'gd') {
                /** @var GdImage $imageInstance */
                $instance = $imageInstance->getGdResource();

                if (false === /** @scrutinizer ignore-call */ \imagewebp($instance, $path, $saveOptions['webp_quality'])) {
                    $msg = Craft::t('imager', 'GD webp save operation failed');
                    Craft::error($msg, __METHOD__);
                    throw new ImagerException($msg);
                }

                // Fix for corrupt file bug (http://stackoverflow.com/questions/30078090/imagewebp-php-creates-corrupted-webp-files)
                if (filesize($path) % 2 === 1) {
                    file_put_contents($path, "\0", FILE_APPEND);
                }
            }

            if (ImagerService::$imageDriver === 'imagick') {
                 /** @var ImagickImage $imageInstance */
                $instance = $imageInstance->getImagick();

                $instance->setImageFormat('webp');

                $hasTransparency = $instance->getImageAlphaChannel();

                if ($hasTransparency) {
                    $instance->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
                    $instance->setBackgroundColor(new \ImagickPixel('transparent'));
                }

                $instance->setImageCompressionQuality($saveOptions['webp_quality']);
                $imagickOptions = $saveOptions['webp_imagick_options'];

                if ($imagickOptions && \count($imagickOptions) > 0) {
                    foreach ($imagickOptions as $key => $val) {
                        $instance->setOption('webp:'.$key, $val);
                    }
                }

                $instance->writeImage($path);
            }
        }
    }
1046355
public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE)
    {
        $image = self::image($frame, $pixelPerPoint, $outerFrame);

        if ($filename === false) {
            Header("Content-type: image/png");
            ImagePng($image);
        } else {
            if($saveandprint===TRUE){
                ImagePng($image, $filename);
                header("Content-type: image/png");
                ImagePng($image);
            }else{
                ImagePng($image, $filename);
            }
        }

        ImageDestroy($image);
    }
42094
public function setSaveHandler($saveHandler = null)
    {
        $this->setOriginalSaveHandler($saveHandler);
        if (!$saveHandler instanceof AbstractProxy &&
            !$saveHandler instanceof NativeSessionHandler &&
            !$saveHandler instanceof \SessionHandlerInterface &&
            null !== $saveHandler) {
            throw new \InvalidArgumentException('Must be instance of AbstractProxy or NativeSessionHandler; implement \SessionHandlerInterface; or be null.');
        }

        // Wrap $saveHandler in proxy and prevent double wrapping of proxy
        if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
            $saveHandler = new SessionHandlerProxy($saveHandler);
        } elseif (!$saveHandler instanceof AbstractProxy) {
            $saveHandler = version_compare(phpversion(), '5.4.0', '>=') ?
                new SessionHandlerProxy(new \SessionHandler()) : new NativeProxy();
        }
        $this->saveHandler = $saveHandler;

        if ($this->saveHandler instanceof \SessionHandlerInterface) {
            if (version_compare(phpversion(), '5.4.0', '>=')) {
                session_set_save_handler($this->saveHandler, false);
            } else {
                session_set_save_handler(
                    array($this->saveHandler, 'open'),
                    array($this->saveHandler, 'close'),
                    array($this->saveHandler, 'read'),
                    array($this->saveHandler, 'write'),
                    array($this->saveHandler, 'destroy'),
                    array($this->saveHandler, 'gc')
                );
            }
        }
    }