1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do you resize image in PHP, without imagecopyresampled?

Discussion in 'PHP' started by JEET, May 15, 2021.

  1. #1
    How do you resize image in PHP, without imagecopyresampled?

    Hi,
    I know we can use PHP inbuilt functions like imagecopyresampled etc to resize an image in php.

    But what I want to know is, how to do it without using any of those functions.
    In other words, what is the algo used to resize an image?

    Say if I want to resize an image of 100 x 100 to a size of 200 x 200
    or
    100 x 100 to a size of 150 x 150

    Thanks
    SEMrush
     
    JEET, May 15, 2021 IP
    SEMrush
  2. jaymi

    jaymi Member

    Messages:
    45
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    45
    #2
    Can you clarify what you mean by resize? do you mean just the output dimensions of the image (what is shown on the website), or to resize the actual image file dimensions?
     
    jaymi, May 15, 2021 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,690
    Likes Received:
    471
    Best Answers:
    19
    Trophy Points:
    235
    #3
    @jaymi
    Actual file resize. Just what imagecopyresampled does.
     
    JEET, May 15, 2021 IP
  4. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    120
    Likes Received:
    40
    Best Answers:
    4
    Trophy Points:
    85
    #4
    Okay I understand what you mean. You mean what algorithm is used to scale images right
    If you know the algorithm, you can implement it in any language.

    So I will give you a very quick explanation.

    First, I will assume you already know how images are represented digitally. (Pixel by Pixel and Each Pixel containing an array of RGB values).

    Now to resize an image, the algorithm I know about is called "Nearest Neighbors Scaling".

    For this example, let's say you want to scale up an image. (Increase its size).

    Imagine the Image is a 10px x 10px image. Meaning it contains 10 pixels on X and Y axis (Overall 100px). And we want to scale it up to 11px x 11px.

    First, Remember that images are simply arrays. Arrays of pixels.
    SO what happens is that the algorithm adds additional 21 pixels. It adds these additional pixels in such a way that the initial 10x10 image is in the center on the X and Y axis.

    Now these 21 new Pixels will be blank so the algorithm fills these pixels up with the color of the pixel closest to it from the original image (Neighbor).

    And that's a simple explanation of how scaling works. I read this algorithm is the fastest, easiest but it is not the best. Other algorithms include:
    • Bicubic Interpolation
    • Bilinear
    I find these ones harder to understand. But as a matter of fact, if you use imagescale() method, you can actually choose which algorithm you want to use. Like this:

    imagescale($image, $new_width, $new_height, IMG_NEAREST_NEIGHBOUR);
    
    //or
    
    imagescale($image, $new_width, $new_height, IMG_BICUBIC);
    
    /*
    Or
    ....
    You get the idea
    */
    PHP:
    So if you can understand this algorithm, you can certainly implement it with PHP. PHP has methods that allows you access the pixel by pixel value of images
     
    Efetobor Agbontaen, May 18, 2021 IP
    JEET likes this.
  5. JEET

    JEET Notable Member

    Messages:
    3,690
    Likes Received:
    471
    Best Answers:
    19
    Trophy Points:
    235
    #5
    JEET, May 19, 2021 IP
    Efetobor Agbontaen likes this.