- Class Thumb
- {
- Public function create ($ srcPath, $ dstPath, $ dstWidth, $ dstHeight)
- {
- If (! File_exists ($ srcPath )){
- Return false;
- }
- @ $ SrcSize = getimagesize ($ srcPath );
- If (empty ($ srcSize )){
- Return false;
- }
- $ SrcWith = intval ($ srcSize [0]);
- $ SrcHeight = intval ($ srcSize [1]);
- // If the size of the original image is greater than the size of the specified thumbnail, a thumbnail is generated. Otherwise, the original file is copied.
- If ($ srcWith <= $ dstWidth & $ srcHeight <= $ dstHeight ){
- Return copy ($ srcPath, $ dstPath );
- }
- // Read original image resources
- @ $ SrcImage = imagecreatefromjpeg ($ srcPath );
- If (empty ($ srcImage )){
- @ $ SrcImage = imagecreatefromgif ($ srcPath );
- }
- If (empty ($ srcImage )){
- @ $ SrcImage = imagecreatefrompng ($ srcPath );
- }
- If (empty ($ srcImage )){
- @ $ SrcImage = $ this-> _ imageCreateFromBMP ($ srcPath );
- }
- If (empty ($ srcImage )){
- Return false;
- }
- // Obtain the thumbnail size and generate a new image accordingly
- $ DstSize = $ this-> _ getDstSize (
- $ SrcWith, $ srcHeight, $ dstWidth, $ dstHeight
- );
- @ $ DstImage = imagecreatetruecolor (
- $ DstSize ['width'], $ dstSize ['height']
- );
- @ Imagecopyresampled (
- $ DstImage, $ srcImage, 0, 0, 0, 0,
- $ DstSize ['width'], $ dstSize ['height'],
- $ SrcWith, $ srcHeight
- );
- Return @ imagepng ($ srcPath, $ dstPath );
- }
- Private function _ imageCreateFromBMP ($ filePath)
- {
- $ FileHandle = fopen ($ filePath, 'RB ');
- If (empty ($ fileHandle )){
- Return false;
- }
- $ File = unpack (
- 'Vfile _ type/Vfile_size/Vreserved/Vbitmap_offset ',
- Fread ($ fileHandle, 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 ($ fileHandle, 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 ['desc'] = 4 ){
- $ Bmp ['desc'] = 0;
- }
- $ Palette = array ();
- If ($ bmp ['Colors '] <16777216 ){
- $ Palette = unpack (
- 'V'. $ bmp ['Colors '],
- Fread ($ fileHandle, $ bmp ['Colors '] * 4)
- );
- }
- $ Image = fread ($ fileHandle, $ 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 ($ image, $ p, 3). $ vide );
- } Else if ($ bmp ['bits _ per_pixel '] = 16 ){
- $ Color = unpack ('N', substr ($ image, $ p, 2 ));
- $ Color [1] = $ palette [$ color [1] + 1];
- } Else if ($ bmp ['bits _ per_pixel '] = 8 ){
- $ Color = unpack ('N', $ vide. substr ($ image, $ p, 1 ));
- $ Color [1] = $ palette [$ color [1] + 1];
- } Else if ($ bmp ['bits _ per_pixel '] = 4 ){
- $ Color = unpack ('N', $ vide. substr ($ image, 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];
- } Else if ($ bmp ['bits _ per_pixel '] = 1 ){
- $ Color = unpack ('N', $ vide. substr ($ image, floor ($ p), 1 ));
- Switch ($ p * 8) % 8 ){
- Case 0:
- $ Color [1] = ($ color [1]> 7 );
- Break;
- Case 1:
- $ Color [1] = ($ color [1] & 0x40)> 6;
- Break;
- Case 2:
- $ Color [1] = ($ color [1] & 0x20)> 5;
- Break;
- Case 3:
- $ Color [1] = ($ color [1] & 0x10)> 4;
- Break;
- Case 4:
- $ Color [1] = ($ color [1] & 0x8)> 3;
- Break;
- Case 5:
- $ Color [1] = ($ color [1] & 0x4)> 2;
- Break;
- Case 6:
- $ Color [1] = ($ color [1] & 0x2)> 1;
- Break;
- Case 7:
- $ Color [1] = ($ color [1] & 0x1 );
- Break;
- }
- $ 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 ($ fileHandle );
- Return $ res;
- }
- Private function _ getDstSize ($ srcWith, $ srcHeight, $ dstWidth, $ dstHeight)
- {
- $ Size = array ('width' => $ srcWith, 'height' => $ srcHeight );
- If ($ dstWidth> 0 & $ dstHeight> 0 ){
- If ($ srcWith> 0 & $ srcHeight> 0 ){
- If ($ srcWith/$ srcHeight> = $ dstWidth/$ dstHeight ){
- If ($ srcWith> $ dstWidth ){
- $ Size ['width'] = $ dstWidth;
- $ Size ['height'] = $ srcHeight * $ dstWidth/$ srcWith;
- }
- } Else {
- If ($ srcHeight> $ dstHeight ){
- $ Size ['width'] = $ srcWith * $ dstHeight/$ srcHeight;
- $ Size ['height'] = $ dstHeight;
- }
- }
- }
- }
- Return $ size;
- }
- }
|