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
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?
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