Resize image

@throws \Exception
961589
public function actionImageResize() {
		$source = $this->getSource();

		$this->accessControl->checkPermission($this->getUserRole(), $this->action, $source->getPath());

		$info = $this->getImageEditorInfo();

		if (!$info->box || (int)$info->box->w <= 0) {
			throw new \Exception('Width not specified', Consts::ERROR_CODE_BAD_REQUEST);
		}

		if (!$info->box || (int)$info->box->h <= 0) {
			throw new \Exception('Height not specified', Consts::ERROR_CODE_BAD_REQUEST);
		}


		$info->img
			->resize((int)$info->box->w, (int)$info->box->h)
			->save($info->path . $info->newname, $source->quality);
	}
1167620
private function _resizeFit(int $width, int $height, bool $resample)
    {
        $this->check();

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

        $k = (($src_w / $src_h) > ($width / $height) ? $src_h / $height : $src_w / $width);

        $resW = (int)round($src_w / $k);
        $resH = (int)round($src_h / $k);

        $src_x = (int)(floor(($resW - $width) / 2) * $k);
        $src_y = (int)(floor(($resH - $height) / 2) * $k);

        $res = imagecreatetruecolor($width, $height);

        if ($this->image_type == 3) {
            imagealphablending($res, false);
            imagesavealpha($res, true);
        }

        if ($resample) {
            imagecopyresampled($res, $this->image_resource, 0, 0, $src_x, $src_y, $resW, $resH, $src_w, $src_h);
        } else {
            imagecopyresized($res, $this->image_resource, 0, 0, $src_x, $src_y, $resW, $resH, $src_w, $src_h);
        }

        imagedestroy($this->image_resource);

        $this->image_resource = $res;
        $this->image_width = $resW;
        $this->image_height = $resH;

        return $this;
    }
275320
public static function resize($image, $size)
    {
        if (!$image) {
            return false;
        }

        list($width, $height) = self::parseSize($size);

        if ($height == 'auto' && $width == 'auto') {
            return false;
        }

        $o_wd = imagesx($image);
        $o_ht = imagesy($image);

        $x = $y = 0;
        if ($height == 'auto') {
            $height = round(($width / $o_wd) * $o_ht);
        } elseif ($width == 'auto') {
            $width = round(($height / $o_ht) * $o_wd);
        } elseif ($width && !$height) {
            //make square image
            $height = $width;
            if ($o_wd > $o_ht) {
                $x = ceil(($width - $height) / 2);
                $o_wd = $o_ht;
            } elseif ($o_ht > $o_wd) {
                $y = ceil(($height - $width) / 2);
                $o_ht = $o_wd;
            }
        } else {
            $w = round($o_wd * $height / $o_ht);
            $h = round($o_ht * $width / $o_wd);

            if (($height - $h) < ($width - $w)) {
                $width = &$w;
            } else {
                $height = &$h;
            }
        }

        $tmp = imageCreateTrueColor($width, $height);
        imagesavealpha($tmp, true);
        $color = imagecolorallocatealpha($tmp, 0x00, 0x00, 0x00, 127);
        imagefill($tmp, 0, 0, $color);
        imagecopyresampled($tmp, $image, 0, 0, $x, $y, $width, $height, $o_wd, $o_ht);

        return $tmp;
    }
458198
protected function resizeImage()
    {
        $resized = false;

        if ($utilsPic = $this->getImageHandler()) {
            // checks if image can be resized, and resizes the image
            if ($utilsPic->resizeImage($this->getImagePath(), $this->getResizedImagePath(), $this->getWidth(), $this->getHeight())) {
                $resized = true;
            }
        }

        return $resized;
    }
399964
public function resize($pathToImage)
    {
        $file = $this->storage->get($pathToImage);

        $this->sizes->each(function ($size, $key) use ($pathToImage, $file) {
            $img = app(ImageManager::class)->make($file);
            $path = $this->createThumbPath($pathToImage, $key);

            switch ($size[2]) {
                case 'width':
                    // resize the image to a width of $sizes[ 0 ] and constrain aspect ratio (auto height)
                    $img->resize($size[0], null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                    break;
                case 'height':
                    // resize the image to a height of $sizes[ 1 ] and constrain aspect ratio (auto width)
                    $img->resize(null, $size[1], function ($constraint) {
                        $constraint->aspectRatio();
                    });
                    break;
                // 'resize' or any other fullbacks to this
                default:
                    $img->fit($size[0], $size[1]);
                    break;
            }

            $this->storage->put($path, $img->stream()->__toString());
        });
    }
1112872
public function resizePortraitImage($image)
    {
        $images = array();
        $original_width = imagesx($image);
        $originl_height = imagesy($image);

        foreach ($this->_sizes as $name => $size) {
            if (!is_int($size)) {
                list($width, $height) = AnHelperImage::parseSize($size);
                if ($original_width < $width) {
                    $size = array($original_width, $originl_height);
                } else {
                    $size = array($width, 'auto');
                }
            }

            $data = AnHelperImage::resize($image, $size);
            $width = imagesx($data);
            $height = imagesy($data);
            $images[$name] = array('size' => $width.'x'.$height,'data' => $data);
        }

        if ($this->_keep_original) {
            $images['original'] = array('size' => $original_width.'x'.$originl_height, 'data' => $image);
        }

        return $images;
    }
260761
public function reSize($width = 0, $height = 0)
    {
        $this->gdExtensionCheck();
        $dosya = $this->getFilePath();
        list($genislik, $yukseklik) = getimagesize($dosya);


        if (strstr($width, "%")) {
            $width = str_replace("%", "", $width);
            $bolw = (100 / $width);
            $width = ($genislik / $bolw);
        }
        if (strstr($height, "%")) {
            $height = str_replace("%", "", $height);
            $bolh = (100 / $height);
            $height = ($yukseklik / $bolh);
        }
        $hedef = imagecreatetruecolor($width, $height);


        switch ($this->getExt()) {
            case 'png':
                $kaynak = imagecreatefrompng($dosya);
                unlink($dosya);
                imagecopyresampled($hedef, $kaynak, 0, 0, 0, 0, $width, $height, $genislik, $yukseklik);
                imagepng($hedef, $dosya, 100);
                break;
            case 'jpg':
                $kaynak = imagecreatefromjpeg($dosya);
                unlink($dosya);
                imagecopyresampled($hedef, $kaynak, 0, 0, 0, 0, $width, $height, $genislik, $yukseklik);
                imagejpeg($hedef, $dosya, 100);
                break;
            case 'gif':
                $kaynak = imagecreatefromgif($dosya);
                unlink($dosya);
                imagecopyresampled($hedef, $kaynak, 0, 0, 0, 0, $width, $height, $genislik, $yukseklik);
                imagegif($hedef, $dosya, 100);
                break;
        }
        imagedestroy($hedef);
        imagedestroy($kaynak);

        return $this;
    }
24341
public static int resizeImage(BufferedImage inputImage, String dest, int width, int height, float quality)
			throws IOException {
		BufferedImage outputImage = Scalr.resize(inputImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, width, height,
				Scalr.OP_ANTIALIAS);
		return writeImage(dest, outputImage, 0.9f);
	}
1051075
public function resize($width, $height)
    {
        $newImage = imagecreatetruecolor($width, $height);

        $this->strategy->handleTransparency($newImage, $this->image);

        // Now resample the image
        imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());

        // And allocate to $this
        $this->image = $newImage;
    }
784603
private function resize(array $params)
    {
        $width = (isset($params['width']) && is_numeric($params['width'])) ? $params['width'] : null;
        $height = (isset($params['height']) && is_numeric($params['height'])) ? $params['height'] : null;

        if (!is_null($width) && !is_null($height)) {
            $this->image->resize(new Box($width, $height));
        } elseif (!is_null($width)) {
            $height = $this->image->getSize()->getHeight() / ($this->image->getSize()->getWidth() / $width);

            $this->image->resize(new Box($width, $height));
        } elseif (!is_null($height)) {
            $width = $this->image->getSize()->getWidth() / ($this->image->getSize()->getHeight() / $height);

            $this->image->resize(new Box($width, $height));
        } else {
            throw new Exception('Wrong resize width or height');
        }
    }
1237175
public function resize( $width = null, $height = null )
    {
        // Get the source width and height
        $sourceWidth = imagesx( $this->image );
        $sourceHeight = imagesy( $this->image );

        // Check if any of the width and height is null, and set it to the $source size
        // $width = $width ?: $sourceWidth;
        // $height = $height ?: $sourceHeight;
        $sourceAspectRatio = $sourceWidth / $sourceHeight;

        if ( !$width && !$height ) {
            $width = $sourceWidth;
            $height = $sourceHeight;
        }
        else if ( ! $width ) {
            $width = $height * $sourceAspectRatio;
        }
        else {
            $height = $width / $sourceAspectRatio;
        }

        $maxCanvasWidth = $width;
        $maxCanvasHeight = $height;

        $thumbnailAspectRatio = $maxCanvasWidth / $maxCanvasHeight;

        // Create canvas for the new image
        $canvas = $this->createCanvas( $width, $height );

        // Do the resizing of the image
        imagecopyresampled($canvas, $this->image, 0, 0, 0, 0, $width, $height, $sourceWidth, $sourceHeight);

        // Replace the current image
        $this->replace( $canvas );

        // Return this for linking
        return $this;
    }
1152801
public function drawException($exception) {
        $this->w = 1;
        $this->h = 1;
        $this->init();

        // Is the image big enough?
        $w = imagesx($this->im);
        $h = imagesy($this->im);

        $text = 'Error: ' . $exception->getMessage();

        $width = imagefontwidth(2) * strlen($text);
        $height = imagefontheight(2);
        if ($width > $w || $height > $h) {
            $width = max($w, $width);
            $height = max($h, $height);

            // We change the size of the image
            $newimg = imagecreatetruecolor($width, $height);
            imagefill($newimg, 0, 0, imagecolorat($this->im, 0, 0));
            imagecopy($newimg, $this->im, 0, 0, 0, 0, $w, $h);
            $this->im = $newimg;
        }

        $black = new CINColor('black');
        imagestring($this->im, 2, 0, 0, $text, $black->allocate($this->im));
    }
1265561
public function render()
    {
        if (($this->arguments['src'] === null && $this->arguments['image'] === null) || ($this->arguments['src'] !== null && $this->arguments['image'] !== null)) {
            throw new Exception('You must either specify a string src or a File object.', 1382284106);
        }

        try {
            $image = $this->imageService->getImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']);
            $cropString = $this->arguments['crop'];
            if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
                $cropString = $image->getProperty('crop');
            }
            $cropVariantCollection = CropVariantCollection::create((string)$cropString);
            $cropVariant = $this->arguments['cropVariant'] ?: 'default';
            $cropArea = $cropVariantCollection->getCropArea($cropVariant);
            $processingInstructions = [
                'width' => $this->arguments['width'],
                'height' => $this->arguments['height'],
                'minWidth' => $this->arguments['minWidth'],
                'minHeight' => $this->arguments['minHeight'],
                'maxWidth' => $this->arguments['maxWidth'],
                'maxHeight' => $this->arguments['maxHeight'],
                'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
                // Added by me
                'additionalParameters' => $this->generateAdditionalProcessingParameters(),
                'skipProcessing' => !ConfigurationUtility::isProcessingEnabled(),
            ];
            $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
            $imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);

            if (!$this->tag->hasAttribute('data-focus-area')) {
                $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
                if (!$focusArea->isEmpty()) {
                    $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
                }
            }
            $this->tag->addAttribute('src', $imageUri);
            $this->tag->addAttribute('width', $processedImage->getProperty('width'));
            $this->tag->addAttribute('height', $processedImage->getProperty('height'));

            $alt = $image->getProperty('alternative');
            $title = $image->getProperty('title');

            // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
            if (empty($this->arguments['alt'])) {
                $this->tag->addAttribute('alt', $alt);
            }
            if (empty($this->arguments['title']) && $title) {
                $this->tag->addAttribute('title', $title);
            }
        } catch (ResourceDoesNotExistException $e) {
            // thrown if file does not exist
            throw new Exception($e->getMessage(), 1509741911, $e);
        } catch (\UnexpectedValueException $e) {
            // thrown if a file has been replaced with a folder
            throw new Exception($e->getMessage(), 1509741912, $e);
        } catch (\RuntimeException $e) {
            // RuntimeException thrown if a file is outside of a storage
            throw new Exception($e->getMessage(), 1509741913, $e);
        } catch (\InvalidArgumentException $e) {
            // thrown if file storage does not exist
            throw new Exception($e->getMessage(), 1509741914, $e);
        }

        return $this->tag->render();
    }
387118
public function resize($width, $height)
    {
        //if the width is not specified, make it relative to the height
        if($width == -1){

            $this->width['dest'] = ($height * $this->width['orig']) / $this->height['orig'];
            $this->height['dest'] = $height;

        //if the height is not specified, make it relative to the width
        } elseif ($height == -1){

            $this->width['dest'] = $width;
            $this->height['dest'] = ($width * $this->height['orig']) / $this->width['orig'];

        } else {

            $this->width['dest'] = $width;
            $this->height['dest'] = $height;
        }

        //set resize code depending on the type of image it is
        switch ($this->type)

    {
            case "gif":
                $this->edited = imagecreate($this->width['dest'], $this->height['dest']);

                imagecopyresampled($this->edited, $this->original, 0, 0, 0, 0, $this->width['dest'], $this->height['dest'], $this->width['orig'], $this->height['orig']);

            break;

            case "jpg":
                $this->edited = imagecreatetruecolor($this->width['dest'], $this->height['dest']);

                imagecopyresampled($this->edited, $this->original, 0, 0, 0, 0, $this->width['dest'], $this->height['dest'], $this->width['orig'], $this->height['orig']);


            break;

            case "png":

                $this->edited = imagecreatetruecolor ($this->width['dest'], $this->height['dest']);

                //preserve the alpha if exists
                imagealphablending($this->edited, false);
                imagesavealpha($this->edited, true);

                imagecopyresampled($this->edited, $this->original, 0, 0, 0, 0, $this->width['dest'], $this->height['dest'], $this->width['orig'], $this->height['orig']);

            break;
        }

    }
408211
protected function getResize()
    {
        $resize = false;
        $imagePath = $this->getImagePath();
        if ($imagePath) {
            $imageSize = $this->getImageSize($imagePath);

            if ($imageSize['width'] > $this->getWidth() ||
                $imageSize['height'] > $this->getHeight()
            ) {
                $resize = true;
            }
        }

        return $resize;
    }
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;
    }
62399
public function getResize()
    {
        $ratio = 1.0;
        $image = request('img');

        $original_image = Image::make($this->lfm->setName($image)->path('absolute'));
        $original_width = $original_image->width();
        $original_height = $original_image->height();

        $scaled = false;

        // FIXME size should be configurable
        if ($original_width > 600) {
            $ratio = 600 / $original_width;
            $width = $original_width * $ratio;
            $height = $original_height * $ratio;
            $scaled = true;
        } else {
            $width = $original_width;
            $height = $original_height;
        }

        if ($height > 400) {
            $ratio = 400 / $original_height;
            $width = $original_width * $ratio;
            $height = $original_height * $ratio;
            $scaled = true;
        }

        return view('laravel-filemanager::resize')
            ->with('img', $this->lfm->pretty($image))
            ->with('height', number_format($height, 0))
            ->with('width', $width)
            ->with('original_height', $original_height)
            ->with('original_width', $original_width)
            ->with('scaled', $scaled)
            ->with('ratio', $ratio);
    }