Copy codeThe Code is as follows: <? Php
Error_reporting (E_ALL );
// Test
Imagezoom('1.jpg ', '2.jpg', 400,300, '# ffff ');
/*
Php thumbnail function:
Proportional lossless compression, can be filled with complementary colors author: huatai
Host format:
Bmp, jpg, gif, and png
Param:
@ Srcimage: The image to be reduced
@ Dstimage: The image to be saved
@ Dst_width: narrowing down the width
@ Dst_height: zoom in and out
@ Backgroundcolor: complementary colors such as: # FFFFFF supports 6 bits and does not support 3 bits
*/
Function imagezoom ($ srcimage, $ dstimage, $ dst_width, $ dst_height, $ backgroundcolor ){
// The file name is garbled
If (PHP_ OS = 'winnt '){
$ Srcimage = iconv ('utf-8', 'gbk', $ srcimage );
$ Dstimage = iconv ('utf-8', 'gbk', $ dstimage );
}
$ Dstimg = imagecreatetruecolor ($ dst_width, $ dst_height );
$ Color = imagecolorallocate ($ dstimg
, Hexdec (substr ($ backgroundcolor, 1, 2 ))
, Hexdec (substr ($ backgroundcolor, 3, 2 ))
, Hexdec (substr ($ backgroundcolor, 5, 2 ))
);
Imagefill ($ dstimg, 0, 0, $ color );
If (! $ Arr = getimagesize ($ srcimage )){
Echo "the file to generate the thumbnail does not exist ";
Exit;
}
$ Src_width = $ arr [0];
$ Src_height = $ arr [1];
$ Srcimg = null;
$ Method = getcreatemethod ($ srcimage );
If ($ method ){
Eval ('$ srcimg ='. $ method .';');
}
$ Dst_x = 0;
$ Dst_y = 0;
$ Dst_w = $ dst_width;
$ Dst_h = $ dst_height;
If ($ dst_width/$ dst_height-$ src_width/$ src_height)> 0 ){
$ Dst_w = $ src_width * ($ dst_height/$ src_height );
$ Dst_x = ($ dst_width-$ dst_w)/2;
} Elseif ($ dst_width/$ dst_height-$ src_width/$ src_height) <0 ){
$ Dst_h = $ src_height * ($ dst_width/$ src_width );
$ Dst_y = ($ dst_height-$ dst_h)/2;
}
Imagecopyresampled ($ dstimg, $ srcimg, $ dst_x
, $ Dst_y, 0, 0, $ dst_w, $ dst_h, $ src_width, $ src_height );
// Save the format
$ Arr = array (
'Jpg '=> 'imagejpeg'
, 'Jpeg '=> 'imagejpeg'
, 'Png '=> 'imagepng'
, 'Gif' => 'imagegif'
, 'Bmp '=> 'imagebmp'
);
$ Suffix = strtolower (array_pop (explode ('.', $ dstimage )));
If (! In_array ($ suffix, array_keys ($ arr ))){
Echo "An error occurred while saving the file name ";
Exit;
} Else {
Eval ($ arr [$ suffix]. '($ dstimg, "'. $ dstimage .'");');
}
Imagejpeg ($ dstimg, $ dstimage );
Imagedestroy ($ dstimg );
Imagedestroy ($ srcimg );
}
Function getcreatemethod ($ file ){
$ Arr = array (
'000000' => "imagecreatefromgif ('$ file ')"
, 'Ffd8ff' => "imagecreatefromjpeg ('$ file ')"
, 'Mongod' => "imagecreatefrombmp ('$ file ')"
, '89504e '=> "imagecreatefrompng (' $ file ')"
);
$ Fd = fopen ($ file, "rb ");
$ Data = fread ($ fd, 3 );
$ Data = str2hex ($ data );
If (array_key_exists ($ data, $ arr )){
Return $ arr [$ data];
} Elseif (array_key_exists (substr ($ data, 0, 4), $ arr )){
Return $ arr [substr ($ data, 0, 4)];
} Else {
Return false;
}
}
Function str2hex ($ str ){
$ Ret = "";
For ($ I = 0; $ I <strlen ($ str); $ I ++ ){
$ Ret. = ord ($ str [$ I])> = 16? Strval (dechex (ord ($ str [$ I])
: '0'. strval (dechex (ord ($ str [$ I]);
}
Return strtoupper ($ ret );
}
// The BMP creation function does not exist in 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 ['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;
}
// BMP save function, which is not available in php
Function imagebmp ($ im, $ fn = false)
{
If (! $ Im) return false;
If ($ fn = false) $ fn = 'php: // output ';
$ F = fopen ($ fn, "w ");
If (! $ F) return false;
$ BiWidth = imagesx ($ im );
$ BiHeight = imagesy ($ im );
$ BiBPLine = $ biWidth * 3;
$ BiStride = ($ biBPLine + 3 )&~ 3;
$ BiSizeImage = $ biStride * $ biHeight;
$ BfOffBits = 54;
$ BfSize = $ bfOffBits + $ biSizeImage;
Fwrite ($ f, 'bm ', 2 );
Fwrite ($ f, pack ('vvvv ', $ bfSize, 0, 0, $ bfOffBits ));
Fwrite ($ f, pack ('vvvvvvvvvvv ', 40, $ biWidth, $ biHeight, 1, 24, 0, $ biSizeImage, 0, 0, 0, 0 ));
$ Numpad = $ biStride-$ biBPLine;
For ($ y = $ biHeight-1; $ y> = 0; -- $ y)
{
For ($ x = 0; $ x <$ biWidth; ++ $ x)
{
$ Col = imagecolorat ($ im, $ x, $ y );
Fwrite ($ f, pack ('V', $ col), 3 );
}
For ($ I = 0; $ I <$ numpad; ++ $ I)
Fwrite ($ f, pack ('C', 0 ));
}
Fclose ($ f );
Return true;
}
?>