Write a php program to scale a given image

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height.

    Syntax:

    resource imagescale( $image, $new_width, $new_height = -1, $mode = IMG_BILINEAR_FIXED )

    Parameters: This function accepts four parameters as mentioned above and described below:

    • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
    • $new_width: This parameter holds the width to scale the image.
    • $new_height: This parameter holds the height to scale the image. If the value of this parameter is negative or omitted then the aspect ratio of image will preserve.
    • $mode: This parameter holds the mode. The value of this parameter is one of IMG_NEAREST_NEIGHBOUR, IMG_BILINEAR_FIXED, IMG_BICUBIC, IMG_BICUBIC_FIXED or anything else.

    Return Value: This function return the resource of scaled image on success or False on failure.

    Below programs illustrate the imagescale() function in PHP:

    Program 1:

    Output:

    Write a php program to scale a given image

    Program 2:

    $image = imagecreatetruecolor(500, 300); 

    $bg = imagecolorallocate($image, 205, 220, 200); 

    imagefill($image, 0, 0, $bg); 

    $col_ellipse = imagecolorallocate($image, 0, 102, 0); 

    imagefilledellipse($image, 250, 150, 400, 250, $col_ellipse); 

    $img = imagescale ( $image, 700, 500 );

    header("Content-type: image/png"); 

    imagepng($img); 

    ?> 

    Output:

    Write a php program to scale a given image

    Reference: https://www.php.net/manual/en/function.imagescale.php


    ImageCopyResized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);
    ImageCopyResampled(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);

    The dest and src parameters are image handles. The point (dx,dy) is the point in the destination image where the region will be copied. The point (sx,sy) is the upper-left corner of the source image. The sw, sh, dw, and dh parameters give the width and height of the copy regions in the source and destination.

    Example 9-10 takes the php.jpg image shown in Figure 9-8 and smoothly scales it down to one-quarter of its size, yielding the image in Figure 9-9.

    (PHP 5 >= 5.5.0, PHP 7, PHP 8)

    imagescaleScale an image using the given new width and height

    Description

    imagescale(
        GdImage $image,
        int $width,
        int $height = -1,
        int $mode = IMG_BILINEAR_FIXED
    ): GdImage|false

    Note:

    Unlike many of other image functions, imagescale() does not modify the passed image; instead, a new image is returned.

    Parameters

    image

    A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

    width

    The width to scale the image to.

    height

    The height to scale the image to. If omitted or negative, the aspect ratio will be preserved.

    mode

    One of IMG_NEAREST_NEIGHBOUR, IMG_BILINEAR_FIXED, IMG_BICUBIC, IMG_BICUBIC_FIXED or anything else (will use two pass).

    Note: IMG_WEIGHTED4 is not yet supported.

    Return Values

    Return the scaled image object on success or false on failure.

    Changelog

    VersionDescription
    8.0.0 On success, this function returns a GDImage instance now; previously, a resource was returned.
    8.0.0 image expects a GdImage instance now; previously, a resource was expected.

    See Also

    • imagecopyresized() - Copy and resize part of an image
    • imagecopyresampled() - Copy and resize part of an image with resampling

    How do I scale an image in PHP?

    The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().

    How to resize image in PHP with example?

    Images can be resized using ImageMagick or GD functions. If GD's functions are used, the size of the image file is also reduced when raw digital camera images are sampled.

    How can we get the properties of an image size type width height using PHP image functions?

    imagesx() and imagesy() The imagesx() and imagesy() are used to extract the width and height of the images respectively. For that, it accepts the resource type of data which will be returned on creating new images dynamically using PHP script.