PHP converts the image format to JPG and automatically scales to the specified size. Recently, when I changed my website to a friend, I found that some of my website images are in PNG format, and the image files are very large, almost all of them are above KB, while the jpg images in the same pixel will be small recently when I modified my website for a friend, the helper House editor found that some website images are in PNG format, and the image files are very large, almost all of them are above KB, the JPG images in the same pixel will be much smaller, so I went online to find a tutorial. now I want to convert the specified image format to JPG in PHP and automatically scale it to the specified size, supports JPG, BMP, PNG, and GIF formats.
If you don't talk about anything else, you can directly use the code. this code cannot be directly copied to your website. you need to know some PHP knowledge. if you don't understand PHP, you are advised to read the PHP Chinese manual provided by the fire.
Copy to ClipboardReference: [www.bkjia.com]
Function ImageCreateFromBMP ($ filename)
{
If (! $ F1 = fopen ($ filename, "rb") return FALSE;
$ FILE = unpack ("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread ($ f1, 14 ));
If ($ FILE ['File _ type']! = 19778) return FALSE;
$ BMP = unpack ('vheader _ size/Vwidth/Vheight/vplanes/vbits_per_pixel '.'/Vcompression/Vsize_bitmap/Vhoriz_resolution '.
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread ($ f1, 40 ));
$ BMP ['Colors '] = pow (2, $ BMP ['bits _ per_pixel']);
If ($ BMP ['size _ bitmap'] = 0) $ BMP ['size _ bitmap'] = $ FILE ['File _ size']-$ file ['bitmap _ offset'];
$ BMP ['bytes _ per_pixel '] = $ BMP ['bits _ per_pixel']/8;
$ BMP ['bytes _ per_pixel2 '] = ceil ($ BMP ['bytes _ per_pixel']);
$ BMP ['desc'] = ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 );
$ BMP ['desc']-= floor ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 );
$ BMP ['demo'] = 4-(4 * $ BMP ['demo']);
If ($ BMP ['demo'] = 4) $ BMP ['demo'] = 0;
$ PALETTE = array ();
If ($ BMP ['Colors '] <16777216)
{
$ PALETTE = unpack ('v'. $ BMP ['Colors '], fread ($ f1, $ BMP ['Colors'] * 4 ));
}
$ IMG = fread ($ f1, $ BMP ['size _ bitmap']);
$ VIDE = chr (0 );
$ Res = imagecreatetruecolor ($ BMP ['width'], $ BMP ['height']);
$ P = 0;
$ Y = $ BMP ['height']-1;
While ($ Y> = 0)
{
$ X = 0;
While ($ X <$ BMP ['width'])
{
If ($ BMP ['bits _ per_pixel '] = 24)
$ COLOR = unpack ("V", substr ($ IMG, $ P, 3). $ VIDE );
Elseif ($ BMP ['bits _ per_pixel '] = 16)
{
$ COLOR = unpack ("n", substr ($ IMG, $ P, 2 ));
$ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
}
Elseif ($ BMP ['bits _ per_pixel '] = 8)
{
$ COLOR = unpack ("n", $ VIDE. substr ($ IMG, $ P, 1 ));
$ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
}
Elseif ($ BMP ['bits _ per_pixel '] = 4)
{
$ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1 ));
If ($ P * 2) % 2 = 0) $ COLOR [1] = ($ COLOR [1]> 4 ); else $ COLOR [1] = ($ COLOR [1] & 0x0F );
$ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
}
Elseif ($ BMP ['bits _ per_pixel '] = 1)
{
$ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1 ));
If ($ P * 8) % 8 = 0) $ COLOR [1] = $ COLOR [1]> 7;
Elseif ($ P * 8) % 8 = 1) $ COLOR [1] = ($ COLOR [1] & 0x40)> 6;
Elseif ($ P * 8) % 8 = 2) $ COLOR [1] = ($ COLOR [1] & 0x20)> 5;
Elseif ($ P * 8) % 8 = 3) $ COLOR [1] = ($ COLOR [1] & 0x10)> 4;
Elseif ($ P * 8) % 8 = 4) $ COLOR [1] = ($ COLOR [1] & 0x8)> 3;
Elseif ($ P * 8) % 8 = 5) $ COLOR [1] = ($ COLOR [1] & 0x4)> 2;
Elseif ($ P * 8) % 8 = 6) $ COLOR [1] = ($ COLOR [1] & 0x2)> 1;
Elseif ($ P * 8) % 8 = 7) $ COLOR [1] = ($ COLOR [1] & 0x1 );
$ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
}
Else
Return FALSE;
Imagesetpixel ($ res, $ X, $ Y, $ COLOR [1]);
$ X ++;
$ P + = $ BMP ['bytes _ per_pixel '];
}
$ Y --;
$ P + = $ BMP ['demo'];
}
Fclose ($ f1 );
Return $ res;
}
// Copy the website bkjia. COM to reject malicious collection of liehuo.net.
Function ImageToJPG ($ srcFile, $ dstFile, $ towidth, $ toheight)
{
$ Quality = 80;
$ Data = @ GetImageSize ($ srcFile );
Switch ($ data ['2'])
{
Case 1:
$ Im = imagecreatefromgif ($ srcFile );
Break;
Case 2:
$ Im = imagecreatefromjpeg ($ srcFile );
Break;
Case 3:
$ Im = imagecreatefrompng ($ srcFile );
Break;
Case 6:
$ Im = ImageCreateFromBMP ($ srcFile );
Break;
}
// $ DstX = $ srcW = @ ImageSX ($ im );
// $ DstY = $ srcH = @ ImageSY ($ im );
$ SrcW = @ ImageSX ($ im );
$ SrcH = @ ImageSY ($ im );
$ DstX = $ towidth;
$ DstY = $ toheight;
$ Ni = @ imageCreateTrueColor ($ dstX, $ dstY );
@ ImageCopyResampled ($ ni, $ im, 0, 0, 0, $ dstX, $ dstY, $ srcW, $ srcH );
@ ImageJpeg ($ ni, $ dstFile, $ quality );
@ Imagedestroy ($ im );
@ Imagedestroy ($ ni );
}
// Usage:
// ImageToJPG ('source file name', 'target file name', target width, and target height );
?>
...