PHP convert picture format to JPG and automatically scale to the specified size _php tutorial

Source: Internet
Author: User
Tags fread unpack
Recently to a friend to modify the site, to help guests of the small to find some of the site's image is a PNG format, and the picture file is very large, almost all over 200KB, and the same pixel jpg pictures will be much smaller, so in the Internet to find a tutorial, PHP now converts the specified image format to JPG and automatically scales to a specified size to support JPG, BMP, PNG, and GIF formats.

Do not say anything else, directly on the code, this code can not be copied directly to your site to use, you need to know some PHP knowledge, if you do not understand PHP, it is recommended to read the Fire supply PHP Chinese manual.

Copy to ClipboardWhat to refer to: [www.bkjia.com]

function Imagecreatefrombmp ($filename)
{
if (! $f 1 = fopen ($filename, "RB")) return FALSE;

$FILE = Unpack ("Vfile_type/vfile_size/vreserved/vbitmap_offset", Fread ($f 1, 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 ($f 1, 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 [' decal '] = ($BMP [' width '] * $BMP [' bytes_per_pixel ']/4);
$BMP [' decal ']-= Floor ($BMP [' width '] * $BMP [' bytes_per_pixel ']/4);
$BMP [' decal '] = 4-(4 * $BMP [' decal ']);
if ($BMP [' decal '] = = 4) $BMP [' decal '] = 0;

$PALETTE = Array ();
if ($BMP [' Colors '] < 16777216)
{
$PALETTE = Unpack (' V '. $BMP [' colors '], fread ($f 1, $BMP [' colors '] * 4));
}

$IMG = Fread ($f 1, $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 [' decal '];
}

Fclose ($f 1);
return $res;
}

Fire Network bkjia.com Welcome copy, refuse to collect liehuo malicious. 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,0, $dstX, $dstY, $srcW, $srcH);
@ImageJpeg ($ni, $dstFile, $quality);
@imagedestroy ($im);
@imagedestroy ($ni);

}



Usage:

Imagetojpg (' source filename ', ' target filename ', target width, target high);

?>

http://www.bkjia.com/PHPjc/363942.html www.bkjia.com true http://www.bkjia.com/PHPjc/363942.html techarticle recently to friends to modify the site, the fire net small to find some Web site pictures are PNG format, and the picture file is very large, almost all over 200KB, and the same pixel jpg pictures will be small ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.