Dedecms scale down the cropped thumbnail by Aspect ratio

Source: Internet
Author: User
Because the dededemsv5.6 thumbnail generation method is scaled down proportionally, as with the previous version issue, the dededecms official may not have noticed, or they think this is the best way to generate thumbnails, if the width and height ratio of the image are not suitable, the thumbnail will be compressed very poorly. here, I first scale down the image because the DEDECMSv5.6 thumbnails are generated proportionally, as with previous versions, the dedecms official team may not have noticed it at all, or they think this is the best way to generate thumbnails.
In this case, if the image's width and height ratio are not suitable, the thumbnail will be compressed very poorly. here, I first scale down the image by width or height, when the size of the thumbnail is exceeded, the displayed thumbnail is much better than the original one.
Default dedecms5.6 default generated thumbnail, thumbnail size: 100*100 (directly reduce the image)
Modify as follows:
Open the/uplodes/image. func. php file,
// Auto-generated function for image shrinking. the source supports bmp, gif, jpg, and png.
// The generated small image only uses jpg or png format.
Find row 44th function ImageResize
To
// Obtain code between GD versions
Use the following code to overwrite (please note that backup is required ).
Function ImageResize ($ srcFile, $ toW, $ toH, $ toFile = "")
{
Global $ pai_photo_type;
If ($ toFile = "")
{
$ ToFile = $ srcFile;
}
$ Info = "";
$ SrcInfo = GetImageSize ($ srcFile, $ info );
Switch ($ srcInfo [2])
{
Case 1:
If (! $ Pai_photo_type ['GIF'])
{
Return false;
}
$ Im = imagecreatefromgif ($ srcFile );
Break;
Case 2:
If (! $ Pai_photo_type ['jpeg '])
{
Return false;
}
$ Im = imagecreatefromjpeg ($ srcFile );
Break;
Case 3:
If (! $ Pai_photo_type ['PNG '])
{
Return false;
}
$ Im = imagecreatefrompng ($ srcFile );
Break;
Case 6:
If (! $ Pai_photo_type ['bmp '])
{
Return false;
}
$ Im = imagecreatefromwbmp ($ srcFile );
Break;
}
$ SrcW = ImageSX ($ im );
$ SrcH = ImageSY ($ im );
If ($ srcW
{
Return true;
}
// Scale down to generate and crop
$ NewW = $ toH * $ srcW/$ srcH;
$ NewH = $ toW * $ srcH/$ srcW;
If ($ newH >=$ toH)
{
$ FtoW = $ toW;
$ FtoH = $ newH;
}
Else
{
$ FtoW = $ newW;
$ FtoH = $ toH;
}
If ($ srcW> $ toW | $ srcH> $ toH)
{
If (function_exists ("imagecreatetruecolor "))
{
@ $ Ni = imagecreatetruecolor ($ ftoW, $ ftoH );
If ($ ni)
{
Imagecopyresampled ($ ni, $ im, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );
}
Else
{
$ Ni = imagecreate ($ ftoW, $ ftoH );
Imagecopyresized ($ ni, $ im, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );
}
}
Else
{
$ Ni = imagecreate ($ ftoW, $ ftoH );
Imagecopyresized ($ ni, $ im, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH );
}
// Crop the image to a standard thumbnail $ new_imgx = imagecreatetruecolor ($ toW, $ toH );
If ($ newH >=$ toH)
{
Imagecopyresampled ($ new_imgx, $ ni, 0, 0, ($ newH-$ toH)/2, $ toW, $ toH, $ toW, $ toH );
}
Else
{
Imagecopyresampled ($ new_imgx, $ ni, ($ newW-$ toW)/, $ toW, $ toH, $ toW, $ toH );
}
Switch ($ srcInfo [2])
{
Case 1:
Imagegif ($ new_imgx, $ toFile );
Break;
Case 2:
Imagejpeg ($ new_imgx, $ toFile, 85 );
Break;
Case 3:
Imagepng ($ new_imgx, $ toFile );
Break;
Case 6:
Imagebmp ($ new_imgx, $ toFile );
Break;
Default:
Return false;
}
Imagedestroy ($ new_imgx );
Imagedestroy ($ ni );
}
Imagedestroy ($ im );
Return true;
}

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.