How to resize images on the server
Adjusting the image size on the server side is much better than processing the image on the browser. This article describes how to adjust the image size on the server side of PHP. The code consists of two parts:
- ImageResizer () is used to process the image
- Loadimage () inserts the image url in a simpler format
-
- Function imageResizer ($ url, $ width, $ height ){
- Header ('content-type: image/jpeg ');
- List ($ width_orig, $ height_orig) = getimagesize ($ url );
- $ Ratio_orig = $ width_orig/$ height_orig;
- If ($ width/$ height> $ ratio_orig ){
- $ Width = $ height * $ ratio_orig;
- } Else {
- $ Height = $ width/$ ratio_orig;
- }
- // This resamples the image
- $ Image_p = imagecreatetruecolor ($ width, $ height );
- $ Image = imagecreatefromjpeg ($ url );
- Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ width, $ height, $ width_orig, $ height_orig );
- // Output the image
- Imagejpeg ($ image_p, null, 100 );
- }
- // Works with both POST and GET
- $ Method = $ _ SERVER ['request _ method'];
- If ($ method = 'get '){
- ImageResize ($ _ GET ['URL'], $ _ GET ['w'], $ _ GET ['H']);
- } Elseif ($ method = 'post '){
- ImageResize ($ _ POST ['URL'], $ _ POST ['w'], $ _ POST ['H']);
- }
- // Makes the process simpler
- Function loadImage ($ url, $ width, $ height ){
- Echo 'image. php? Url = ', urlencode ($ url ),
- '& W =', $ width,
- '& H =', $ height;
- }
- ?>
- // Above code wocould be in a file called image. php.
- // Images wocould be displayed like this:
- "Alt =" "/>
|