Home Backend Development PHP Tutorial PHP converts the image format to JPG and automatically scales it to the specified size_PHP tutorial

PHP converts the image format to JPG and automatically scales it to the specified size_PHP tutorial

Jul 21, 2016 pm 02:57 PM
jpg php for Revise picture size Small designation hour friend Format Zoom website automatic Convert

When I was modifying a website for a friend recently, the editor of Bangkejia found that the pictures on some websites were in PNG format, and the picture files were very large, almost more than 200KB, while JPG pictures of the same pixels were much smaller. , so I searched for tutorials on the Internet, and now I will share with you the method of converting the specified image format to JPG and automatically scaling it to the specified size using PHP. It supports JPG, BMP, PNG and GIF formats.

Don’t say anything else, just upload the code. This code cannot be directly copied to your website for use. You need to know some PHP knowledge. If you don’t understand PHP, it is recommended to read the PHP Chinese manual provided by Agni.

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.bkjia.com] <?php


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 [ '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 ( $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 [ 'decal' ];
}

fclose ( $f1 );
return $res ;
}

// 烈火網 bkjia.COM 欢迎复制,拒绝恶意采集 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,0,$dstX,$dstY,$srcW,$srcH);
@ImageJpeg($ni,$dstFile,$quality);
@ imagedestroy($im);
@imagedestroy($ni);

}



//Usage:

//ImageToJPG(' Source file name', 'target file name', target width, target height);

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363942.htmlTechArticleRecently when modifying the website for a friend, the editor of Liehuo.com found that the pictures on some websites are in PNG format, and the picture files Very large, almost more than 200KB, while JPG pictures of the same pixels will be smaller...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles