Thumbnails generated by php

Source: Internet
Author: User
Tags imagejpeg


<?
/**
* Generating thumbnails
* $ SrcName ---- Original Image path
* $ NewWidth, $ newHeight ---- maximum width and height of the thumbnails respectively
* $ NewName ---- specifies the thumbnail file name (including path). The default value is to add thumbnail.
* @ Param string $ srcName
* @ Param int $ newWidth
* @ Param int $ newHeight
* @ Param string $ newName
* Return viod
*/
Function resizeImg ($ srcName, $ newWidth, $ newHeight, $ newName = "")
{
If ($ newName = "")
        {
$ NameArr = explode ('.', $ srcName);
$ ExpName = array_pop ($ nameArr);
$ ExpName = 'thumbnail. '. $ expName;
Array_push ($ nameArr, $ expName);
$ NewName = implode ('.', $ nameArr);
        }
$ Info = "";
$ Data = getimagesize ($ srcName, $ info);
Switch ($ data [2])
        {
Case 1:
If (! Function_exists ("imagecreatefromgif")){
Echo "your GD library cannot use images in GIF format. Please use Jpeg or PNG format! Return";
Exit ();
                        }
$ Im = ImageCreateFromGIF ($ srcName);
Break;
Case 2:
If (! Function_exists ("imagecreatefromjpeg")){
Echo "your GD library cannot use images in jpeg format. Please use images in other formats! Return";
Exit ();
                        }
$ Im = ImageCreateFromJpeg ($ srcName);
Break;
Case 3:
$ Im = ImageCreateFromPNG ($ srcName);
Break;
        }
$ SrcW = ImageSX ($ im);
$ SrcH = ImageSY ($ im);
$ NewWidthH = $ newWidth/$ newHeight;
$ SrcWH = $ srcW/$ srcH;
If ($ newWidthH <= $ srcWH){
$ FtoW = $ newWidth;
$ FtoH = $ ftoW * ($ srcH/$ srcW);
        }
Else {
$ FtoH = $ newHeight;
$ FtoW = $ ftoH * ($ srcW/$ srcH);
        }
If ($ srcW> $ newWidth | $ srcH> $ newHeight)
        {
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);
                }
If (function_exists ('imagejpeg ') imagejpeg ($ ni, $ newName);
Else ImagePNG ($ ni, $ newName);
ImageDestroy ($ ni);
        }
ImageDestroy ($ im);
}

ResizeImg ('2014. JPG ', 123);
?>

Related Article

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.