Manipulation::scaleImage

Scale an image using the percentage image scale.

@param int $newScale The percentage to scale the image to.

@return bool
341428
public function scaleImage($newScale)
    {
        $resampleImageFile = $this->driver->getSourceImageFile();
        $resampleDimension = $resampleImageFile->getDimension();
        $resampleDimension->maintainAspectRatio = $this->config->offsetGet('maintainAspectRatio');

        $this->driver->setResampleImage($resampleImageFile->withDimension(
            $resampleDimension
                ->withScale($newScale)
        ));

        return $this->driver->scale();
    }
1250398
private function _resize(int $width, int $height, bool $resample)
    {
        $this->check();

        $src_w = $this->image_width;
        $src_h = $this->image_height;

        $xscale = $src_w / $width;
        $yscale = $src_h / $height;

        if ($yscale > $xscale) {
            $dst_w = (int)round($src_w * (1 / $yscale));
            $dst_h = (int)round($src_h * (1 / $yscale));
        } else {
            $dst_w = (int)round($src_w * (1 / $xscale));
            $dst_h = (int)round($src_h * (1 / $xscale));
        }

        $res = imagecreatetruecolor($dst_w, $dst_h);

        if ($this->image_type == 3) {
            imagealphablending($res, false);
            imagesavealpha($res, true);
        }
        if ($resample) {
            imagecopyresampled($res, $this->image_resource, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        } else {
            imagecopyresized($res, $this->image_resource, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        }

        imagedestroy($this->image_resource);

        $this->image_resource = $res;
        $this->image_width = $dst_w;
        $this->image_height = $dst_h;

        return $this;
    }
300375
private function applyScale(ImageInterface $image, $scale)
    {
        return $this->modifyAllLayers(
            $image,
            function(ImageInterface $layer) use ($scale) {
                return $this->scaler->scale(
                    $layer,
                    $scale['x'],
                    $scale['y'],
                    $scale['mode'],
                    $scale['forceRatio'],
                    $scale['retina']
                );
            }
        );
    }
536462
function ScaleDown($maxSize)
    {
        
        $height = $this->Height();
        $width = $this->Width();
        
        if ($width < $maxSize && $height < $maxSize)
            return;
        
        $xRatio = $maxSize / $height ;
        $yRatio = $maxSize / $width;
        
        $this->ScaleProportional(min($xRatio, $yRatio));
    }
416560
public function resize($source, Array $settings = []) : GD
    {
        $this->resample($source, $settings, 'imagecopyresized');

        return $this;
    }
434299
public function setZoomScale($pValue = 100) {
		// Microsoft Office Excel 2007 only allows setting a scale between 10 and 400 via the user interface,
		// but it is apparently still able to handle any scale >= 1
		if (($pValue >= 1) || is_null($pValue)) {
			$this->_zoomScale = $pValue;
		} else {
			throw new PHPExcel_Exception("Scale must be greater than or equal to 1.");
		}
		return $this;
	}
1227482
public function resizePercent($percent = 50)
    {
        if ( $percent < 1)
        {
            throw new \InvalidArgumentException('percent must be >= 1');
        }

        $this->resize($this->source['width'] * ($percent / 100), $this->source['height'] * ($percent / 100));

        return $this;
    }
314065
public function scaleResize($width, $height) {

        // calculate source coordinates
        $kw = $this->width() / $width;
        $kh = $this->height() / $height;
        if($kh < $kw) {
            $src_h = $this->height();
            $src_y = 0;
            $src_w = round($kh * $width);
            $src_x = round(($this->width() - $src_w) / 2);
        } else {
            $src_h = round($kh * $height);
            $src_y = round(($this->height() - $src_h) / 2);
            $src_w = $this->width();
            $src_x = 0;
        }

        // copy new image
        $new = imagecreatetruecolor($width, $height);
        imagecopyresampled($new, $this->resource, 0, 0, $src_x, $src_y,
            $width, $height, $src_w, $src_h);
        $this->resource = $new;

        // for method chaining
        return $this;
    }
309754
public function scale($new_width = null, $new_height = null)
        {
            if(!is_null($new_width) && is_null($new_height))
                $new_height = $new_width * $this->height / $this->width;
            elseif(is_null($new_width) && !is_null($new_height))
                $new_width = $this->width / $this->height * $new_height;
            elseif(!is_null($new_width) && !is_null($new_height)) {
                if($this->width < $this->height)
                    $new_width = $this->width / $this->height * $new_height;
                else
                    $new_height = $new_width * $this->height / $this->width;
            } else
                return false;

            return $this->resize($new_width, $new_height);
        }
396286
public function setImageFormat($imageformat,$pngScaleFactor = null){
        if($imageformat == self::$IMAGE_FORMAT_JPEG){
            $this->imageDeviceCommand = "jpeg";
            $this->imageExtention="jpg";
            $this->pngDownScaleFactor = isset($pngScaleFactor) ? "-dDownScaleFactor=".$pngScaleFactor : "";
        }else if($imageformat == self::$IMAGE_FORMAT_PNG){
            $this->imageDeviceCommand = "png16m";
            $this->imageExtention="png";
            $this->pngDownScaleFactor = "";
        }
        return $this;
    }
1360417
protected function resize(ImagineImageInterface $image, $mode = ImageInterface::RATIOMODE_INSET, $filter = ImagineImageInterface::FILTER_UNDEFINED)
    {
        if ($mode !== ImageInterface::RATIOMODE_INSET &&
            $mode !== ImageInterface::RATIOMODE_OUTBOUND
        ) {
            throw new \InvalidArgumentException('Invalid mode specified');
        }

        $imageSize = $image->getSize();
        $requestedDimensions = $this->calculateDimensions($imageSize);

        $image->strip();

        $resizeDimensions = $requestedDimensions;
        if ($mode === ImageInterface::RATIOMODE_OUTBOUND) {
            $resizeDimensions = $this->calculateOutboundScalingDimensions($imageSize, $requestedDimensions);
        }

        $image->resize($resizeDimensions, $filter);

        if ($mode === ImageInterface::RATIOMODE_OUTBOUND) {
            $image->crop(new Point(
                max(0, round(($resizeDimensions->getWidth() - $requestedDimensions->getWidth()) / 2)),
                max(0, round(($resizeDimensions->getHeight() - $requestedDimensions->getHeight()) / 2))
            ), $requestedDimensions);
        }

        return $image;
    }
92278
public function scale( $width, $height, $fit = true )
	{
		if( ( $info = getimagesize( $this->_filename ) ) === false ) {
			throw new MW_Media_Exception( 'Unable to retrive image size' );
		}

		if( $fit === true )
		{
			list( $width, $height ) = $this->_getSizeFitted( $info[0], $info[1], $width, $height );

			if( $info[0] <= $width && $info[1] <= $height ) {
				return;
			}
		}

		if( ( $image = imagecreatetruecolor( $width, $height ) ) === false ) {
			throw new MW_Media_Exception( 'Unable to create new image' );
		}

		if( imagecopyresampled( $image, $this->_image, 0, 0, 0, 0, $width, $height, $info[0], $info[1] ) === false ) {
			throw new MW_Media_Exception( 'Unable to resize image' );
		}

		imagedestroy( $this->_image );
		$this->_image = $image;
	}
238198
public function scale()
    {
        $resampleDimension = $this->resampleImageFile->getDimension();

        $resampleImageResource =& $this->getResampleImageResource();

        try {
            return $resampleImageResource->scaleImage($resampleDimension->getWidth(), $resampleDimension->getHeight(),
                true);
        } catch (\ImagickException $e) {
            $this->errors[ $e->getCode() ] = $e->getMessage();
        }

        return false;
    }
1294515
public function apply($imageRessource)
  {
    $startWidth = imagesx($imageRessource);
    $startHeight = imagesy($imageRessource);

    $xratio = $startWidth / $this->width;

    $newWidth  = $this->width;
    $newHeight = $startHeight * $newWidth / $startWidth;

    $resized = Image::createEmptyContainer($newWidth, $newHeight);
    imagecopyresampled($resized, $imageRessource, 0, 0, 0, 0, $newWidth, $newHeight, $startWidth, $startHeight);
    imagedestroy($imageRessource);
    return $resized;
  }
160605
public function toGrayscale()
    {
        $this->getInfo();

        $this->edited = imagecreate($this->width['orig'], $this->height['orig']);

        //Creates the 256 color palette
        for ($c=0;$c<256;$c++)

    {
            $palette[$c] = imagecolorallocate($this->edited,$c,$c,$c);
        }

        //Reads the origonal colors pixel by pixel
        for ($y=0;$y<$this->height['orig'];$y++)

    {
            for ($x=0;$x<$this->width['orig'];$x++)

    {
                $rgb = imagecolorat($this->original,$x,$y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;

                //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
                $gs = $this->colorToGray($r,$g,$b);
                imagesetpixel($this->edited,$x,$y,$palette[$gs]);
            }
        }

    }
1291982
public function scale()
    {
        $sourceDimension = $this->sourceImageFile->getDimension();
        $resampleDimension = $this->resampleImageFile->getDimension();

        $resampleAxis = $this->resampleImageFile->getDimension()->getAxis();
        $sourceAxis = $sourceDimension->getAxis();

        if (function_exists('imagecreatetruecolor')) {
            $this->resampleImageResource = imagecreatetruecolor(
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight()
            );

            imagealphablending($this->resampleImageResource, false);
            imagesavealpha($this->resampleImageResource, true);

            $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127);
            imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(),
                $resampleDimension->getHeight(), $transparent);

            return imagecopyresampled(
                $this->resampleImageResource,
                $this->sourceImageResource,
                $resampleAxis->getX(),
                $resampleAxis->getY(),
                $sourceAxis->getX(),
                $sourceAxis->getY(),
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight(),
                $sourceDimension->getWidth(),
                $sourceDimension->getHeight()
            );
        } else {
            $this->resampleImageResource = imagecreate($resampleDimension->getWidth(),
                $resampleDimension->getHeight());

            imagealphablending($this->resampleImageResource, false);
            imagesavealpha($this->resampleImageResource, true);

            $transparent = imagecolorallocatealpha($this->resampleImageResource, 255, 255, 255, 127);
            imagefilledrectangle($this->resampleImageResource, 0, 0, $resampleDimension->getWidth(),
                $resampleDimension->getHeight(), $transparent);

            return imagecopyresized(
                $this->resampleImageResource,
                $this->sourceImageResource,
                $resampleAxis->getX(),
                $resampleAxis->getY(),
                $sourceAxis->getX(),
                $sourceAxis->getY(),
                $resampleDimension->getWidth(),
                $resampleDimension->getHeight(),
                $sourceDimension->getWidth(),
                $sourceDimension->getHeight()
            );
        }
    }
381837
protected function _scaler($inputFile, $outputFile, $scaleType, $scale)
    {
        switch ($scaleType) {
            //skalowanie domyślne
            case 'default':
                $imgRes = \Mmi\Image\Image::inputToResource($inputFile);
                break;
            //skalowanie proporcjonalne do maksymalnego rozmiaru
            case 'scale':
                $v = explode('x', $scale);
                if (count($v) == 1 && is_numeric($v[0]) && intval($v[0]) > 0) {
                    $imgRes = \Mmi\Image\Image::scale($inputFile, $v[0]);
                } elseif (count($v) == 2 && is_numeric($v[0]) && intval($v[0]) > 0 && is_numeric($v[1]) && intval($v[1]) > 0) {
                    $imgRes = \Mmi\Image\Image::scale($inputFile, $v[0], $v[1]);
                }
                break;
            //skalowanie do maksymalnego X
            case 'scalex':
                $imgRes = \Mmi\Image\Image::scalex($inputFile, intval($scale));
                break;
            //skalowanie do maksymalnego Y
            case 'scaley':
                $imgRes = \Mmi\Image\Image::scaley($inputFile, intval($scale));
                break;
            //skalowanie z obcięciem
            case 'scalecrop':
                $v = explode('x', $scale);
                if (is_numeric($v[0]) && intval($v[0]) > 0 && is_numeric($v[1]) && intval($v[1]) > 0) {
                    $imgRes = \Mmi\Image\Image::scaleCrop($inputFile, $v[0], $v[1]);
                }
                break;
        }
        //brak obrazu
        if (!isset($imgRes)) {
            FrontController::getInstance()->getLogger()->warning('Unable to resize CMS file: ' . $outputFile);
            return;
        }
        //plik istnieje
        if (!file_exists(dirname($outputFile))) {
            try {
                mkdir(dirname($outputFile), 0777, true);
            } catch (\Mmi\App\KernelException $e) {
                FrontController::getInstance()->getLogger()->warning('Unable to create directories: ' . $e->getMessage());
                return;
            }
        }
        //określanie typu wyjścia
        $mimeType = \Mmi\FileSystem::mimeType($inputFile);
        //GIF
        if ($mimeType == 'image/gif') {
            imagealphablending($imgRes, false);
            imagesavealpha($imgRes, true);
            imagegif($imgRes, $outputFile);
            return;
        }
        //PNG (nieprzeźroczysty)
        if ($mimeType == 'image/png') {
            imagealphablending($imgRes, false);
            imagesavealpha($imgRes, true);
            imagepng($imgRes, $outputFile, 9);
            return;
        }
        //domyślnie JPEG
        imagejpeg($imgRes, $outputFile, intval(\App\Registry::$config->thumbQuality));
    }