Resize image

@param  string      $hash   image file
@param  int         $width  new width
@param  int         $height new height
@param  bool        $crop   crop image
@return array|false
@author Dmitry (dio) Levashov
@author Alexey Sukhotin
327833
public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0)
    {
        if ($this->commandDisabled('resize')) {
            return $this->setError(elFinder::ERROR_PERM_DENIED);
        }

        if (($file = $this->file($hash)) == false) {
            return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
        }

        if (!$file['write'] || !$file['read']) {
            return $this->setError(elFinder::ERROR_PERM_DENIED);
        }

        $path = $this->decode($hash);

        if (!$this->canResize($path, $file)) {
            return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
        }

        $img = $this->tmpname($path);

        if (!($fp = @fopen($img, 'w+'))) {
            return false;
        }

        if (($res = $this->query('SELECT content FROM '.$this->tbf.' WHERE id="'.$path.'"'))
        && ($r = $res->fetch_assoc())) {
            fwrite($fp, $r['content']);
            rewind($fp);
            fclose($fp);
        } else {
            return false;
        }

        switch ($mode) {

            case 'propresize':
                $result = $this->imgResize($img, $width, $height, true, true);
                break;

            case 'crop':
                $result = $this->imgCrop($img, $width, $height, $x, $y);
                break;

            case 'fitsquare':
                $result = $this->imgSquareFit($img, $width, $height, 'center', 'middle', $bg ? $bg : $this->options['tmbBgColor']);
                break;

            default:
                $result = $this->imgResize($img, $width, $height, false, true);
                break;
        }

        if ($result) {

            $sql = sprintf('UPDATE %s SET content=LOAD_FILE("%s"), mtime=UNIX_TIMESTAMP() WHERE id=%d', $this->tbf, $this->loadFilePath($img), $path);

            if (!$this->query($sql)) {
                $content = file_get_contents($img);
                $sql = sprintf('UPDATE %s SET content="%s", mtime=UNIX_TIMESTAMP() WHERE id=%d', $this->tbf, $this->db->real_escape_string($content), $path);
                if (!$this->query($sql)) {
                    @unlink($img);

                    return false;
                }
            }
            @unlink($img);
            if (!empty($file['tmb']) && $file['tmb'] != "1") {
                $this->rmTmb($file['tmb']);
            }
            $this->clearcache();

            return $this->stat($path);
        }

           return false;
    }
564521
public function resize($width, $height) {
        if (!$width) $width = 1;
        if (!$height) $height = 1;
        return (
            (false !== ($img = new gd(array($width, $height)))) &&
            $img->imagecopyresampled($this) &&
            (false !== ($this->image = $img->get_image())) &&
            (false !== ($this->width = $img->get_width())) &&
            (false !== ($this->height = $img->get_height()))
        );
    }
1145016
public function resize_overflow($width, $height) {

        $big = (($this->width / $this->height) > ($width / $height))
            ? ($this->width * $height) / $this->height
            : ($this->height * $width) / $this->width;
        $big = intval($big);

        $return = ($img = new gd($this->image, $big));

        if ($return) {
            $this->image = $img->get_image();
            $this->width = $img->get_width();
            $this->height = $img->get_height();
        }

        return $return;
    }
651318
public function cropImage($newWidth, $newHeight, $cropPos = 'm')
		# Author:     Jarrod Oberto
		# Date:       08-09-11
		# Purpose:    Crops the image
		# Param in:   $newWidth: crop with
		#             $newHeight: crop height
		#       $cropPos: Can be any of the following:
		#             tl, t, tr, l, m, r, bl, b, br, auto
		#           Or:
		#             a custom position such as '30x50'
		# Param out:  n/a
		# Reference:
		# Notes:
		#
	{

		// *** Make sure the file passed in is valid
		if ( ! $this->image)
		{
			if ($this->debug)
			{
				throw new Exception('file ' . $this->getFileName() . ' is missing or invalid');
			}
			else
			{
				throw new Exception();
			}
		};

		$this->imageResized = $this->image;
		$this->crop($this->width, $this->height, $newWidth, $newHeight, $cropPos);

	}
82107
public function crop($width, $height, $mode = 'tl')
    {
        if ( !is_numeric($width) ) {
            throw new \InvalidArgumentException('$width must be numeric');
        }

        if ( !is_numeric($height) ) {
            throw new \InvalidArgumentException('$height must be numeric');
        }

        if ( $this->newImage )
        {
            $this->sourceImage = $this->newImage;
        }

        $oldWidth   = ($this->newImage) ? imagesx($this->newImage) : $this->source['width'];
        $oldHeight  = ($this->newImage) ? imagesy($this->newImage) : $this->source['height'];

        $this->create($width, $height);

        $startX     = $startY = 0;
        $cropWidth  = $sourceWidth  = $width;
        $cropHeight = $sourceHeight = $height;

        if ( is_array($mode) )
        {
            $startX = $mode[0];
            $startY = $mode[1];
        }

        if ($mode === self::CROP_TOP_CENTER)
        {
            $startX = ($oldWidth - $cropWidth) / 2;
        }
        else if ($mode === self::CROP_TOP_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
        }
        else if ($mode === self::CROP_CENTER_LEFT)
        {
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_CENTER_CENTER)
        {
            $startX = ($oldWidth - $cropWidth)   / 2;
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_CENTER_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_BOTTOM_LEFT)
        {
            $startY = $oldHeight - $cropHeight;
        }
        else if ($mode === self::CROP_BOTTOM_CENTER)
        {
            $startX = ($oldWidth - $cropWidth)   / 2;
            $startY = $oldHeight - $cropHeight;
        }
        else if ($mode === self::CROP_BOTTOM_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
            $startY = $oldHeight - $cropHeight;
        }
        else
        {
        }

        imagecopyresampled(
            $this->newImage,
            $this->sourceImage,
            0, 0, $startX, $startY,
            $cropWidth, $cropHeight, $sourceWidth, $sourceHeight);

        return $this;
    }
855836
public function crop($width, $height) {

        // GdThumb will not enforce the requested aspect ratio if the image is too
        // small, so we manually calculate what the size should be if the aspect
        // ratio is preserved.
        $options = $this->thumb->getOptions();
        if (empty($options['resizeUp'])) {
            $size = $this->thumb->getCurrentDimensions();
            $ratio = $width / $height;
            if ($size['width'] < $width) {
                $width = $size['width'];
                $height = $size['width'] / $ratio;
            }
            if ($size['height'] < $height) {
                $height = $size['height'];
                $width = $size['height'] * $ratio;
            }
        }

        // Do a normal adpative resize
        $this->thumb->adaptiveResize($width, $height);
        return $this;
    }
1104641
public function do_crop($width, $height, $offsetX = 0, $offsetY = 0)
    {
        return $this->_handler->do_crop($width, $height, $offsetX, $offsetY);
    }
1282596
function resize($width,$height,$crop=TRUE,$enlarge=TRUE) {
		// Adjust dimensions; retain aspect ratio
		$ratio=($origw=imagesx($this->data))/($origh=imagesy($this->data));
		if (!$crop) {
			if ($width/$ratio<=$height)
				$height=$width/$ratio;
			else
				$width=$height*$ratio;
		}
		if (!$enlarge) {
			$width=min($origw,$width);
			$height=min($origh,$height);
		}
		// Create blank image
		$tmp=imagecreatetruecolor($width,$height);
		imagesavealpha($tmp,TRUE);
		imagefill($tmp,0,0,IMG_COLOR_TRANSPARENT);
		// Resize
		if ($crop) {
			if ($width/$ratio<=$height) {
				$cropw=$origh*$width/$height;
				imagecopyresampled($tmp,$this->data,
					0,0,($origw-$cropw)/2,0,$width,$height,$cropw,$origh);
			}
			else {
				$croph=$origw*$height/$width;
				imagecopyresampled($tmp,$this->data,
					0,0,0,($origh-$croph)/2,$width,$height,$origw,$croph);
			}
		}
		else
			imagecopyresampled($tmp,$this->data,
				0,0,0,0,$width,$height,$origw,$origh);
		imagedestroy($this->data);
		$this->data=$tmp;
		return $this->save();
	}
1259495
private function actionResize($width, $height)
    {
        $image = $this->getPhpThumb();
        $image->resize($width, $height);
    }
534867
protected function resizeCrop(ImageInterface $image, $width, $height, $enlarge = true)
    {
        $size = $image->getSize();

        if ($enlarge === false && $size->getWidth() < $width) {
            $width = $size->getWidth();
        }

        if ($enlarge === false && $size->getHeight() < $height) {
            $height = $size->getHeight();
        }

        list($optimal_width, $optimal_height) = $this->getOptimalCrop($size, $width, $height, $enlarge);

        // Find center - this will be used for the crop
        $center_x = ($optimal_width / 2) - ($width / 2);
        $center_y = ($optimal_height / 2) - ($height / 2);

        return $image->resize(new Box($optimal_width, $optimal_height))
            ->crop(new Point($center_x, $center_y), new Box($width, $height));
    }
737215
public function croppedResize($width, $height)
    {
        return $this->createCloneWithResource(
            function (InterventionImage $resource) use ($width, $height) {
                return $resource->fit($width, $height);
            }
        );
    }
768524
public function do_crop($width, $height, $offsetX = 0, $offsetY = 0)
    {
        $this->_image->cropImage($width, $height, $offsetX, $offsetY);
        $this->_image->setImagePage($width, $height, 0, 0);

        $this->_width = $this->_image->getImageWidth();
        $this->_height = $this->_image->getImageHeight();

        return $this;
    }
1030426
public function runResize(Image $image, $fit, $width, $height)
    {
        if ($fit === 'contain') {
            return $this->runContainResize($image, $width, $height);
        }

        if ($fit === 'fill') {
            return $this->runFillResize($image, $width, $height);
        }

        if ($fit === 'max') {
            return $this->runMaxResize($image, $width, $height);
        }

        if ($fit === 'stretch') {
            return $this->runStretchResize($image, $width, $height);
        }

        if ($fit === 'crop') {
            return $this->runCropResize($image, $width, $height);
        }

        return $image;
    }
1059019
public function resize($crop = false)
    {
        if ($crop) {
            return $this->resizeCrop();
        } else {
            $sourceDimension = $this->sourceImageFile->getDimension();
            $resampleDimension = $this->resampleImageFile->getDimension();

            if (($sourceDimension->getWidth() <= $resampleDimension->getWidth()) && ($sourceDimension->getHeight() <= $resampleDimension->getHeight())) {
                return true;
            } //no resizing needed

            //try max width first...
            $resizeRatio = $resampleDimension->getWidth() / $sourceDimension->getWidth();
            $resizeWidth = $resampleDimension->getWidth();
            $resizeHeight = $sourceDimension->getHeight() * $resizeRatio;

            //if that didn't work
            if ($resizeHeight > $resampleDimension->getHeight()) {
                $resizeRatio = $resampleDimension->getHeight() / $sourceDimension->getHeight();
                $resizeHeight = $resampleDimension->getHeight();
                $resizeWidth = $sourceDimension->getWidth() * $resizeRatio;
            }

            if (function_exists('imagecreatetruecolor')) {
                $this->resampleImageResource = imagecreatetruecolor($resizeWidth, $resizeHeight);

                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,
                    0,
                    0,
                    0,
                    0,
                    $resizeWidth,
                    $resizeHeight,
                    $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 imagecopyresampled(
                    $this->resampleImageResource,
                    $this->sourceImageResource,
                    0,
                    0,
                    0,
                    0,
                    $resizeWidth,
                    $resizeHeight,
                    $sourceDimension->getWidth(),
                    $sourceDimension->getHeight()
                );
            }
        }
    }
221549
protected function resizeExact($file, $width, $height)
	{
		return $this->imagine
			->open($file->getRealPath())
			->resize(new Box($width, $height));
	}
914723
public function runFillResize($image, $width, $height)
    {
        $image = $this->runMaxResize($image, $width, $height);

        return $image->resizeCanvas($width, $height, 'center');
    }
983752
public function Fill($width, $height)
    {
        $width = $this->castDimension($width, 'Width');
        $height = $this->castDimension($height, 'Height');
        $variant = $this->variantName(__FUNCTION__, $width, $height);
        return $this->manipulateImage($variant, function (Image_Backend $backend) use ($width, $height) {
            if ($backend->getWidth() === $width && $backend->getHeight() === $height) {
                return $this;
            }
            return $backend->croppedResize($width, $height);
        });
    }