Imageoptim::optimize

@param string $image
@param string|null

@return string
@throws \Exception
182861
public function optimize($image, $option = null)
    {
        // optimize: image optimization in the same format
        // webp: converts the image to the WebP image format
        if ($option === null) {
            $option = 'optimize';
        }

        $callApiUrl = $this->apiUrl . '/' . $this->apiKey . '?' . $option;

        $headers = [
            'User-Agent: Optimus-API',
            'Accept: image/*',
        ];

        $handle = curl_init();
        curl_setopt_array($handle, [
            CURLOPT_URL            => $callApiUrl,
            CURLOPT_HTTPHEADER     => $headers,
            CURLOPT_POSTFIELDS     => file_get_contents($image),
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER         => true,
            CURLOPT_SSL_VERIFYPEER => true,
        ]);

        $response = curl_exec($handle);
        $curlError = curl_error($handle);
        $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
        $body = substr($response, $header_size);
        // error catching
        if ( ! empty($curlError) || empty($body)) {
            throw new RuntimeException("Optimus-Error: {$curlError}, Output: {$body}");
        }

        return $body;
    }
472318
public function apply( $image ) {

        $size = $this->amount;
        $width = $image->getWidth();
        $height = $image->getHeight();
        $image->getCore()->scaleImage(max(1, ($width / $size)), max(1, ($height / $size)));
        $image->getCore()->scaleImage($width, $height);
        return $image;
    }
468765
public function apply( $image ) {

        $image->getCore()->sigmoidalContrastImage($this->amount > 0, $this->amount / 4, 0);
        return $image;
    }
1373359
public function run(Image $image)
    {
        $width = $this->getWidth();
        $height = $this->getHeight();
        $fit = $this->getFit();
        $dpr = $this->getDpr();

        list($width, $height) = $this->resolveMissingDimensions($image, $width, $height);
        list($width, $height) = $this->applyDpr($width, $height, $dpr);
        list($width, $height) = $this->limitImageSize($width, $height);

        if ((int) $width !== (int) $image->width() or (int) $height !== (int) $image->height()) {
            $image = $this->runResize($image, $fit, (int) $width, (int) $height);
        }

        return $image;
    }
488969
protected function _getImage($Image = null) {
		if (is_string($Image)) {
			$class = 'Imagine\\' . $this->config('engine') . '\Imagine';
			$Imagine = new $class();
			return $Imagine->open($Image);
		}
		if (!empty($this->_image)) {
			return $this->_image;
		}
		throw new \RuntimeException('Could not get the image object!');
	}
300418
protected function randomColor($image)
    {
        if($this->ismono) {
            return imagecolorallocatealpha($image, $this->monocolor[0], $this->monocolor[1], $this->monocolor[2], $this->rand(0, 96));
        }
        return imagecolorallocate($image, $this->rand(0, 255), $this->rand(0, 255), $this->rand(0, 255));
    }
183746
public function execute($image)
    {
        $width = $this->argument(0)->type('digit')->required()->value();
        $height = $this->argument(1)->type('digit')->required()->value();
        $anchor = $this->argument(2)->value('center');
        $relative = $this->argument(3)->type('boolean')->value(false);
        $bgcolor = $this->argument(4)->value();

        $original_width = $image->getWidth();
        $original_height = $image->getHeight();

        // check of only width or height is set
        $width = is_null($width) ? $original_width : intval($width);
        $height = is_null($height) ? $original_height : intval($height);

        // check on relative width/height
        if ($relative) {
            $width = $original_width + $width;
            $height = $original_height + $height;
        }

        // check for negative width/height
        $width = ($width <= 0) ? $width + $original_width : $width;
        $height = ($height <= 0) ? $height + $original_height : $height;

        // create new canvas
        $canvas = $image->getDriver()->newImage($width, $height, $bgcolor);

        // set copy position
        $canvas_size = $canvas->getSize()->align($anchor);
        $image_size = $image->getSize()->align($anchor);
        $canvas_pos = $image_size->relativePosition($canvas_size);
        $image_pos = $canvas_size->relativePosition($image_size);

        if ($width <= $original_width) {
            $dst_x = 0;
            $src_x = $canvas_pos->x;
            $src_w = $canvas_size->width;
        } else {
            $dst_x = $image_pos->x;
            $src_x = 0;
            $src_w = $original_width;
        }

        if ($height <= $original_height) {
            $dst_y = 0;
            $src_y = $canvas_pos->y;
            $src_h = $canvas_size->height;
        } else {
            $dst_y = $image_pos->y;
            $src_y = 0;
            $src_h = $original_height;
        }

        // make image area transparent to keep transparency
        // even if background-color is set
        $transparent = imagecolorallocatealpha($canvas->getCore(), 255, 255, 255, 127);
        imagealphablending($canvas->getCore(), false); // do not blend / just overwrite
        imagefilledrectangle($canvas->getCore(), $dst_x, $dst_y, $dst_x + $src_w - 1, $dst_y + $src_h - 1, $transparent);

        // copy image into new canvas
        imagecopy($canvas->getCore(), $image->getCore(), $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

        // set new core to canvas
        $image->setCore($canvas->getCore());

        return true;
    }
162239
public function execute($image)
    {
        $level = $this->argument(0)->between(-100, 100)->required()->value();

        return $image->getCore()->sigmoidalContrastImage($level > 0, $level / 4, 0);
    }
535913
public function execute($image)
    {
        $filling = $this->argument(0)->value();
        $x = $this->argument(1)->type('digit')->value();
        $y = $this->argument(2)->type('digit')->value();

        $width = $image->getWidth();
        $height = $image->getHeight();
        $resource = $image->getCore();

        try {

            // set image tile filling
            $source = new Decoder;
            $tile = $source->init($filling);
            imagesettile($image->getCore(), $tile->getCore());
            $filling = IMG_COLOR_TILED;

        } catch (\Hail\Image\Exception\NotReadableException $e) {

            // set solid color filling
            $color = new Color($filling);
            $filling = $color->getInt();
        }

        imagealphablending($resource, true);

        if (is_int($x) && is_int($y)) {

            // resource should be visible through transparency
            $base = $image->getDriver()->newImage($width, $height)->getCore();
            imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);

            // floodfill if exact position is defined
            imagefill($resource, $x, $y, $filling);

            // copy filled original over base
            imagecopy($base, $resource, 0, 0, 0, 0, $width, $height);

            // set base as new resource-core
            $image->setCore($base);
            imagedestroy($resource);

        } else {
            // fill whole image otherwise
            imagefilledrectangle($resource, 0, 0, $width - 1, $height - 1, $filling);
        }

        isset($tile) ? imagedestroy($tile->getCore()) : null;

        return true;
    }
211993
public function execute($image)
    {
        $colors = [];

        $size = $image->getSize();

        for ($x=0; $x <= ($size->width-1); $x++) {
            for ($y=0; $y <= ($size->height-1); $y++) {
                $colors[] = $image->pickColor($x, $y, 'array');
            }
        }

        $this->setOutput(md5(serialize($colors)));

        return true;
    }
121251
public function optimizeImage(ImageInterface $image) {
	    if (!in_array($image->getMimeType(), $this->getSupportedMimeTypes())) {
			throw new ImageOptimizerException(sprintf('Unsupported MIME type : %s', $image->getMimeType()), ImageOptimizerException::UNSUPPORTED_MIME_TYPE);
	    }
	    $if = $this->getTempnam("if", $this->getInputFileExtension());
		if (!file_put_contents($if, $image->getBinaryContent())) {
			throw new ImageOptimizerException(sprintf('Cannot write tmpfile : %s', $if), ImageOptimizerException::CANNOT_WRITE_TMPFILE);
		}
		$binaryContent = null;
		$mimeType = $this->execExternalOptimizer($if, $binaryContent);
		unlink($if);
		$image->setBinaryContent($binaryContent, $mimeType);
	}
333270
public function run(Image $image)
    {
        $brightness = $this->getBrightness();

        if ($brightness !== null) {
            $image->brightness($brightness);
        }

        return $image;
    }
1188996
public function setImage($image)
    {
        if ($image instanceof FeedImage) {
            $this->image = $image;
        } elseif (is_array($image)) {
            if (!empty($image)) {
                $this->image = FeedImage::createFromArray($image);
            }
        } elseif (is_null($image)) {
            $this->image = null;
        } else {
            throw new \InvalidArgumentException('Invalid image');
        }

        return $this;
    }
906002
public function execute($image)
    {
        $red = $this->argument(0)->between(-100, 100)->required()->value();
        $green = $this->argument(1)->between(-100, 100)->required()->value();
        $blue = $this->argument(2)->between(-100, 100)->required()->value();

        // normalize colorize levels
        $red = $this->normalizeLevel($red);
        $green = $this->normalizeLevel($green);
        $blue = $this->normalizeLevel($blue);

        $qrange = $image->getCore()->getQuantumRange();

        // apply
        $image->getCore()->levelImage(0, $red, $qrange['quantumRangeLong'], \Imagick::CHANNEL_RED);
        $image->getCore()->levelImage(0, $green, $qrange['quantumRangeLong'], \Imagick::CHANNEL_GREEN);
        $image->getCore()->levelImage(0, $blue, $qrange['quantumRangeLong'], \Imagick::CHANNEL_BLUE);

        return true;
    }
843417
public function imageExtension($image)
    {
        $filename = $image;
        $extension = explode('.', $filename);
        $ext = strtolower(end($extension));

        return $ext;
    }
404412
public function allocate($image): int {
        if ($image instanceof Image) {
            $image = $image->getResource();
        }

        return imagecolorallocatealpha(
            $image,
            $this->r,
            $this->g,
            $this->b,
            $this->a
        );
    }
542131
public function execute($image)
    {
        $mode = $this->argument(0)->type('bool')->value(true);

        if ($mode) {
            $mode = \Imagick::INTERLACE_LINE;
        } else {
            $mode = \Imagick::INTERLACE_NO;
        }

        $image->getCore()->setInterlaceScheme($mode);

        return true;
    }