Get virtual rendition for given width/height/ratio.
@param rendition Rendition
@param widthValue Width
@param heightValue Height
@param ratioValue Ratio
@return Rendition or null
961867
private RenditionMetadata getVirtualRendition(RenditionMetadata rendition, long widthValue, long heightValue, double ratioValue) {

    long width = widthValue;
    long height = heightValue;
    double ratio = ratioValue;

    // if ratio is missing: calculate from given rendition
    if (ratio < MediaFormatHandler.RATIO_TOLERANCE) {
      ratio = (double)rendition.getWidth() / (double)rendition.getHeight();
    }

    // if height is missing - calculate from width
    if (height == 0 && width > 0) {
      height = (int)Math.round(width / ratio);
    }

    // if width is missing - calculate from height
    if (width == 0 && height > 0) {
      width = (int)Math.round(height * ratio);
    }

    // return virtual rendition
    if (width > 0 && height > 0) {
      if (rendition instanceof VirtualTransformedRenditionMetadata) {
        VirtualTransformedRenditionMetadata cropRendition = (VirtualTransformedRenditionMetadata)rendition;
        return new VirtualTransformedRenditionMetadata(cropRendition.getRendition(), width, height,
            cropRendition.getCropDimension(), cropRendition.getRotation());
      }
      else {
        return new VirtualRenditionMetadata(rendition.getRendition(), width, height);
      }
    }
    else {
      return null;
    }
  }
1299174
public BufferedImage makeAWTImage(int width, int height) throws TranscoderException {
    ThumbnailTranscoder t = new ThumbnailTranscoder();
    t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(width));
    t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(height));
    // Don't clone. Assume this is used safely.
    TranscoderInput input = new TranscoderInput(document);
    t.transcode(input, null);
    return t.getLastImage();
  }
1232427
public static function aspect_ratio( $width, $height, $proper = false ) 
	{
		$ratio = $width / $height;

		if ( !$proper ) 
		{
			return $ratio;
		}

		$tolerance = 1.e-6;
		$h1=1; $h2=0;
		$k1=0; $k2=1;
		$b = 1/$ratio;

		do {
			$b = 1/$b;
			$a = floor($b);
			$aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
			$aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
			$b = $b-$a;

		} while ( abs( $ratio-$h1 / $k1 ) > $ratio * $tolerance );

		return $h1.":".$k1;
	}
788177
def widthratio(value, max_value, max_width):
    
    ratio = float(value) / float(max_value)
    return int(round(ratio * max_width))
998283
public Cell<C,T> prefSize (float width, float height) {
		prefWidth = new FixedValue<C, T>(layout.toolkit,width);
		prefHeight = new FixedValue<C, T>(layout.toolkit,height);
		return this;
	}
535589
private String buildMediaUrl(Dimension scaledDimension) {

    // check for file extension filtering
    if (!isMatchingFileExtension()) {
      return null;
    }

    // check if image has to be rescaled
    if (scaledDimension != null) {

      // check if scaling is not possible
      if (scaledDimension.equals(SCALING_NOT_POSSIBLE_DIMENSION)) {
        return null;
      }

      // otherwise generate scaled image URL
      return buildScaledMediaUrl(scaledDimension, this.media.getCropDimension());
    }

    // if no scaling but cropping required builid scaled image URL
    if (this.media.getCropDimension() != null) {
      return buildScaledMediaUrl(this.media.getCropDimension(), this.media.getCropDimension());
    }

    if (mediaArgs.isContentDispositionAttachment()) {
      // if not scaling and no cropping required but special content disposition headers required build download url
      return buildDownloadMediaUrl();
    }
    else {
      // if not scaling and no cropping required build native media URL
      return buildNativeMediaUrl();
    }
  }
1357015
public function resize($width, $height, $ratio = false, $interlace = false) {

		if(false === ('-' . intval($width) == '-' . $width)) {
			return trigger_error(sprintf('Argument 1 passed to %s() must be of the type integer, "%s" given', __METHOD__, gettype($width)), E_USER_ERROR);
		}

		if(false === ('-' . intval($height) == '-' . $height)) {
			return trigger_error(sprintf('Argument 2 passed to %s() must be of the type integer, "%s" given', __METHOD__, gettype($height)), E_USER_ERROR);
		}

		if(null !== $ratio && false === is_bool($ratio)) {
			return trigger_error(sprintf('Argument 3 passed to %s() must be of the type boolean, "%s" given', __METHOD__, gettype($ratio)), E_USER_ERROR);
		}

		if(false === is_bool($interlace)) {
			return trigger_error(sprintf('Argument 4 passed to %s() must be of the type boolean, "%s" given', __METHOD__, gettype($interlace)), E_USER_ERROR);
		}

		$this -> add(__FUNCTION__, func_get_args());

		return $this;
	}
118776
public function scale($ratio)
    {
        $_width = (int)$this->do_getWidth();
        $_height = (int)$this->do_getHeight();

        if ($ratio === 1) {
            return $this;
        }

        $width = (int)($_width * $ratio);
        $height = (int)($_height * $ratio);

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

        return $this;
    }
291131
private RenditionMetadata getVirtualRendition(final Set<RenditionMetadata> candidates, MediaArgs mediaArgs) {

    // get from fixed with/height
    if (mediaArgs.getFixedWidth() > 0 || mediaArgs.getFixedHeight() > 0) {
      long destWidth = mediaArgs.getFixedWidth();
      long destHeight = mediaArgs.getFixedHeight();
      double destRatio = 0;
      if (destWidth > 0 && destHeight > 0) {
        destRatio = (double)destWidth / (double)destHeight;
      }
      return getVirtualRendition(candidates, destWidth, destHeight, destRatio);
    }

    // or from any media format
    return visitMediaFormats(mediaArgs, new MediaFormatVisitor<RenditionMetadata>() {
      @Override
      public RenditionMetadata visit(MediaFormat mediaFormat) {
        int destWidth = (int)mediaFormat.getEffectiveMinWidth();
        int destHeight = (int)mediaFormat.getEffectiveMinHeight();
        double destRatio = mediaFormat.getRatio();
        // try to find matching rendition, otherwise check for next media format
        RenditionMetadata rendition = getVirtualRendition(candidates, destWidth, destHeight, destRatio);
        if (rendition != null) {
          rendition.setMediaFormat(mediaFormat);
        }
        return rendition;
      }
    });
  }
625185
public function thumbizeByLimit( $width, $height )
	{
		if( !count( $this->size ) )
			throw new RuntimeException( 'No Source Image set.' );

		$ratio_s	= $this->size[0] / $this->size[1];
		$ratio_t	= $width / $height;
		if( ( $ratio_s / $ratio_t ) > 1 )
			$height	= ceil( $width / $ratio_s );
		else
			$width	= ceil( $height * $ratio_s );
		return $this->thumbize( $width, $height );
	}
1102264
function(ratio, opt) {

        var segments = this.segments;
        var numSegments = segments.length;
        if (numSegments === 0) return null; // if segments is an empty array

        if (ratio < 0) ratio = 0;
        if (ratio > 1) ratio = 1;

        opt = opt || {};
        var precision = (opt.precision === undefined) ? this.PRECISION : opt.precision;
        var segmentSubdivisions = (opt.segmentSubdivisions === undefined) ? this.getSegmentSubdivisions({ precision: precision }) : opt.segmentSubdivisions;
        var localOpt = { precision: precision, segmentSubdivisions: segmentSubdivisions };

        var pathLength = this.length(localOpt);
        var length = pathLength * ratio;

        return this.divideAtLength(length, localOpt);
    }
485024
public Cell<C,T> minSize (Value<C,T> width, Value<C,T> height) {
		minWidth = width;
		minHeight = height;
		return this;
	}
925429
def _scale(self, resource):
        
        original_width, original_height = resource.size

        target_width = int(round(original_width * self.value))
        target_height = int(round(original_height * self.value))

        resource_format = resource.format
        resource = resource.resize((target_width, target_height), Image.ANTIALIAS)
        resource.format = resource_format

        return resource
1178832
public function resize($width, $height, $scale = null)
	{
		if (null == $scale) {
			$scale = self::SCALE_AUTO;
		}
		
		$xy_scales = array(self::SCALE_EXACT, self::SCALE_CROP);
		if (in_array($scale, $xy_scales) && (empty($width) || empty($height))) {
			throw new Exception('Width and height must be non-zero integers');
		}
		else if (empty($width) && empty($height)) {
			throw new Exception('Width or height must be a non-zero integer');
		}
		
		$orig_x = $this->width();
		$orig_y = $this->height();
		
		// Handle auto scaling.
		if ($scale == self::SCALE_AUTO) {
			if (empty($height)) {
				$scale = self::SCALE_LANDSCAPE;
			}
			else if (empty($width)) {
				$scale = self::SCALE_PORTRAIT;
			}
			else if ($orig_y < $orig_x) {
				$scale = self::SCALE_LANDSCAPE;
			}
			else if ($orig_y > $orig_x) {
				$scale = self::SCALE_PORTRAIT;
			}
			else if ($height < $width) {
				$scale = self::SCALE_EXACT;
				$height = $width;
			}
			else {
				$scale = self::SCALE_EXACT;
				$width = $height;
			}
		}
		
		switch ($scale) {
			case self::SCALE_EXACT:
				break;
				
			case self::SCALE_PORTRAIT:
				$width = round($height * ($orig_x / $orig_y));
				$height = $height;
				break;
				
			case self::SCALE_LANDSCAPE:
				$height = round($width * ($orig_y / $orig_x));
				break;
				
			case self::SCALE_CROP:
				$given_width = $width;
				$given_height = $height;
				
				$ratio = max($height / $orig_y, $width / $orig_x);
				
				$height = round($orig_y * $ratio);
				$width  = round($orig_x * $ratio);
				break;
		}
		
		$new_image = imagecreatetruecolor($width, $height);
		$white = imagecolorallocate($new_image, 255, 255, 255);
		imagefill($new_image, 0, 0, $white);
		imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $orig_x, $orig_y);
		$this->image = $new_image;
		
		if ($scale == self::SCALE_CROP) {
			// Crop to the center.
			if ($width > $height) {
				$crop_start_x = ($width / 2) - ($given_width / 2);
				$crop_start_y = 0;
			}
			else {
				$crop_start_x = 0;
				$crop_start_y = ($height / 2) - ($given_height / 2);
			}
			
			$new_image = imagecreatetruecolor($given_width, $given_height);
			$white = imagecolorallocate($new_image, 255, 255, 255);
			imagefill($new_image, 0, 0, $white);
			imagecopyresampled(
				$new_image, $this->image, 0, 0, $crop_start_x, $crop_start_y,
				$given_width, $given_height, $given_width, $given_height
			);
			$this->image = $new_image;
		}
		
		return $this;
	}
673473
function zoom(ratio) {
    var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

    var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

    var imageData = this.imageData;


    ratio = Number(ratio);

    if (ratio < 0) {
      ratio = 1 / (1 - ratio);
    } else {
      ratio = 1 + ratio;
    }

    this.zoomTo(imageData.width * ratio / imageData.naturalWidth, hasTooltip, _originalEvent);

    return this;
  }
854398
public static double selectZoomToShowAll(JComponent panel , int width , int height ) {
		int w = panel.getWidth();
		int h = panel.getHeight();
		if( w == 0 ) {
			w = panel.getPreferredSize().width;
			h = panel.getPreferredSize().height;
		}

		double scale = Math.max(width/(double)w,height/(double)h);
		if( scale > 1.0 ) {
			return 1.0/scale;
		} else {
			return 1.0;
		}
	}
762937
public BufferedImage create_BATTERY_Image(final int WIDTH, final int HEIGHT, final int VALUE) {
        if (WIDTH <= 0 || HEIGHT <= 0) {
            return UTIL.createImage(1, 1, Transparency.TRANSLUCENT);
        }

        final BufferedImage IMAGE = UTIL.createImage(WIDTH, HEIGHT, Transparency.TRANSLUCENT);
        final Graphics2D G2 = IMAGE.createGraphics();

        G2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        final int IMAGE_WIDTH = IMAGE.getWidth();
        final int IMAGE_HEIGHT = IMAGE.getHeight();

        // Background
        final GeneralPath BATTERY = new GeneralPath();
        BATTERY.setWindingRule(Path2D.WIND_EVEN_ODD);
        BATTERY.moveTo(IMAGE_WIDTH * 0.025, IMAGE_HEIGHT * 0.05555555555555555);
        BATTERY.lineTo(IMAGE_WIDTH * 0.9, IMAGE_HEIGHT * 0.05555555555555555);
        BATTERY.lineTo(IMAGE_WIDTH * 0.9, IMAGE_HEIGHT * 0.9444444444444444);
        BATTERY.lineTo(IMAGE_WIDTH * 0.025, IMAGE_HEIGHT * 0.9444444444444444);
        BATTERY.lineTo(IMAGE_WIDTH * 0.025, IMAGE_HEIGHT * 0.05555555555555555);
        BATTERY.closePath();
        BATTERY.moveTo(IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 0.0);
        BATTERY.lineTo(IMAGE_WIDTH * 0.0, IMAGE_HEIGHT * 0.0);
        BATTERY.lineTo(IMAGE_WIDTH * 0.0, IMAGE_HEIGHT * 1.0);
        BATTERY.lineTo(IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 1.0);
        BATTERY.lineTo(IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 0.7222222222222222);
        BATTERY.curveTo(IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 0.7222222222222222, IMAGE_WIDTH * 0.975, IMAGE_HEIGHT * 0.7222222222222222, IMAGE_WIDTH * 0.975, IMAGE_HEIGHT * 0.7222222222222222);
        BATTERY.curveTo(IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.7222222222222222, IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.6666666666666666, IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.6666666666666666);
        BATTERY.curveTo(IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.6666666666666666, IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.3333333333333333, IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.3333333333333333);
        BATTERY.curveTo(IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.3333333333333333, IMAGE_WIDTH * 1.0, IMAGE_HEIGHT * 0.2777777777777778, IMAGE_WIDTH * 0.975, IMAGE_HEIGHT * 0.2777777777777778);
        BATTERY.curveTo(IMAGE_WIDTH * 0.975, IMAGE_HEIGHT * 0.2777777777777778, IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 0.2777777777777778, IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 0.2777777777777778);
        BATTERY.lineTo(IMAGE_WIDTH * 0.925, IMAGE_HEIGHT * 0.0);
        BATTERY.closePath();
        final Point2D BATTERY_START = new Point2D.Double();
        final Point2D BATTERY_STOP = new Point2D.Double();

        switch(lightPosition)
        {
            case NORTH_EAST:
                BATTERY_START.setLocation(BATTERY.getBounds2D().getMaxX(), BATTERY.getBounds2D().getMinY());
                BATTERY_STOP.setLocation(BATTERY.getBounds2D().getMinX(), BATTERY.getBounds2D().getMaxY());
                break;
            case EAST:
                BATTERY_START.setLocation(BATTERY.getBounds2D().getMaxX(), 0);
                BATTERY_STOP.setLocation(BATTERY.getBounds2D().getMinX(), 0);
                break;
            case SOUTH_EAST:
                BATTERY_START.setLocation(BATTERY.getBounds2D().getMaxX(), BATTERY.getBounds2D().getMaxY());
                BATTERY_STOP.setLocation(BATTERY.getBounds2D().getMinX(), BATTERY.getBounds2D().getMinY());
                break;
            case SOUTH:
                BATTERY_START.setLocation(0, BATTERY.getBounds2D().getMaxY());
                BATTERY_STOP.setLocation(0, BATTERY.getBounds2D().getMinY());
                break;
            case SOUTH_WEST:
                BATTERY_START.setLocation(BATTERY.getBounds2D().getMinX(), BATTERY.getBounds2D().getMaxY());
                BATTERY_STOP.setLocation(BATTERY.getBounds2D().getMaxX(), BATTERY.getBounds2D().getMinY());
                break;
            case WEST:
                BATTERY_START.setLocation(BATTERY.getBounds2D().getMinX(), 0);
                BATTERY_STOP.setLocation(BATTERY.getBounds2D().getMaxX(), 0);
                break;
            case NORTH_WEST:
                BATTERY_START.setLocation(BATTERY.getBounds2D().getMinX(), BATTERY.getBounds2D().getMinY());
                BATTERY_STOP.setLocation(BATTERY.getBounds2D().getMaxX(), BATTERY.getBounds2D().getMaxY());
                break;
            case NORTH:
            default:
                BATTERY_START.setLocation(0, BATTERY.getBounds2D().getMinY());
                BATTERY_STOP.setLocation(0, BATTERY.getBounds2D().getMaxY());
                break;
        }

        final float[] BATTERY_FRACTIONS = {
            0.0f,
            1.0f
        };
        final Color[] BATTERY_COLORS = {
            new Color(255, 255, 255, 255),
            new Color(126, 126, 126, 255)
        };
        final LinearGradientPaint BATTERY_GRADIENT = new LinearGradientPaint(BATTERY_START, BATTERY_STOP, BATTERY_FRACTIONS, BATTERY_COLORS);
        G2.setPaint(BATTERY_GRADIENT);
        G2.fill(BATTERY);

        // Main
        final Rectangle2D BORDER = new Rectangle2D.Double(IMAGE_WIDTH * 0.025, IMAGE_WIDTH * 0.025, IMAGE_WIDTH * 0.875 * (VALUE / 100.0), IMAGE_HEIGHT * 0.88888888888888);
        final float[] BORDER_FRACTIONS = {
            0.0f,
            0.40f,
            1.0f
        };
        final Color[] BORDER_COLORS = {
            EMPTY_BORDER,
            HALF_BORDER,
            FULL_BORDER
        };
        borderGradient = new GradientWrapper(new Point2D.Double(0, 0), new Point2D.Double(100, 0), BORDER_FRACTIONS, BORDER_COLORS);
        G2.setPaint(borderGradient.getColorAt(VALUE / 100f));
        G2.fill(BORDER);

        final Rectangle2D LIQUID = new Rectangle2D.Double(IMAGE_WIDTH * 0.05, IMAGE_WIDTH * 0.05, IMAGE_WIDTH * 0.85 * (VALUE / 100.0), IMAGE_HEIGHT * 0.77777777777777);
        final Point2D LIQUID_START = new Point2D.Double(IMAGE_WIDTH * 0.05, 0);
        final Point2D LIQUID_STOP = new Point2D.Double(IMAGE_WIDTH * 0.875, 0);
        final float[] LIQUID_FRACTIONS = {
            0.0f,
            0.5f,
            1.0f
        };
        final Color[] LIQUID_COLORS_DARK = {
            EMPTY_DARK,
            HALF_DARK,
            FULL_DARK
        };
        final Color[] LIQUID_COLORS_LIGHT = {
            EMPTY_LIGHT,
            HALF_LIGHT,
            FULL_LIGHT
        };
        final float[] LIQUID_GRADIENT_FRACTIONS = {
            0.0f,
            0.4f,
            1.0f
        };
        liquidGradientDark = new GradientWrapper(new Point2D.Double(0, 0), new Point2D.Double(100, 0), LIQUID_GRADIENT_FRACTIONS, LIQUID_COLORS_DARK);
        liquidGradientLight = new GradientWrapper(new Point2D.Double(0, 0), new Point2D.Double(100, 0), LIQUID_GRADIENT_FRACTIONS, LIQUID_COLORS_LIGHT);
        final Color[] LIQUID_COLORS = {
            liquidGradientDark.getColorAt(VALUE / 100f),
            liquidGradientLight.getColorAt(VALUE / 100f),
            liquidGradientDark.getColorAt(VALUE / 100f)
        };
        final LinearGradientPaint LIQUID_GRADIENT = new LinearGradientPaint(LIQUID_START, LIQUID_STOP, LIQUID_FRACTIONS, LIQUID_COLORS);
        G2.setPaint(LIQUID_GRADIENT);
        G2.fill(LIQUID);

        // Foreground
        final Rectangle2D HIGHLIGHT = new Rectangle2D.Double(IMAGE_WIDTH * 0.025, IMAGE_WIDTH * 0.025, IMAGE_WIDTH * 0.875, IMAGE_HEIGHT * 0.44444444444444);
        final Point2D HIGHLIGHT_START = new Point2D.Double(0, HIGHLIGHT.getBounds2D().getMinY());
        final Point2D HIGHLIGHT_STOP = new Point2D.Double(0, HIGHLIGHT.getBounds2D().getMaxY());
        final float[] HIGHLIGHT_FRACTIONS = {
            0.0f,
            1.0f
        };
        final Color[] HIGHLIGHT_COLORS = {
            new Color(1.0f, 1.0f, 1.0f, 0.0f),
            new Color(1.0f, 1.0f, 1.0f, 0.8f)
        };

        final LinearGradientPaint HIGHLIGHT_GRADIENT = new LinearGradientPaint(HIGHLIGHT_START, HIGHLIGHT_STOP, HIGHLIGHT_FRACTIONS, HIGHLIGHT_COLORS);
        G2.setPaint(HIGHLIGHT_GRADIENT);
        G2.fill(HIGHLIGHT);

        G2.dispose();

        return IMAGE;
    }