Example of generating image thumbnails using php,

Source: Internet
Author: User

Example of generating image thumbnails using php,

This example describes how to use php to generate image thumbnails. We will share this with you for your reference. The details are as follows:

The complete code is as follows (for ease of reading, the code is formatted using http://tools.jb51.net/code/phpformat ):

<? Php/** Created on 2011-3-18 ** To change the template for this generated file go to * Window-Preferences-PHPeclipse-PHP-Code Templates * // * constructor-generate thumbnail + watermark, parameter description: $ srcFile-image file name, $ dstFile-save another file name, $ markwords-watermark text, $ markimage-watermark image, $ dstW-image save width, $ dsomething-image storage height, $ rate-Image Storage Quality */makethumb ("1.jpg"," aa/B .jpg "," 50 "," 50 "); function makethumb ($ srcFile, $ dstFile, $ dstW, $ dsomething, $ rate = 100, $ markwords = null, $ mar Kimage = null) {$ data = GetImageSize ($ srcFile); switch ($ data [2]) {case 1: $ im = @ ImageCreateFromGIF ($ srcFile); break; case 2: $ im = @ ImageCreateFromJPEG ($ srcFile); break; case 3: $ im = @ ImageCreateFromPNG ($ srcFile); break;} if (! $ Im) return False; $ srcW = ImageSX ($ im); $ srcH = ImageSY ($ im); $ dstX = 0; $ dstY = 0; if ($ srcW * $ DTH> $ srcH * $ dstW) {$ fdth = round ($ srcH * $ dstW/$ srcW ); $ dstY = floor ($ DTH-$ fdth)/2); $ fdstW = $ dstW;} else {$ fdstW = round ($ srcW * $ DTH/$ srcH ); $ dstX = floor ($ dstW-$ fdstW)/2); $ fdth = $ dsomething;} $ ni = ImageCreateTrueColor ($ dstW, $ DTH ); $ dstX = ($ dstX <0 )? 0: $ dstX; $ dstY = ($ dstX <0 )? 0: $ dstY; $ dstX = ($ dstX> ($ dstW/2 ))? Floor ($ dstW/2): $ dstX; $ dstY = ($ dstY> ($ dsomething/2 ))? Floor ($ dsomething/s): $ dstY; $ white = ImageColorAllocate ($ ni, 255,255,255); $ black = ImageColorAllocate ($ ni, 0, 0, 0 ); imagefilledrectangle ($ ni, 0, 0, $ dstW, $ DTH, $ white); // fill in the background color ImageCopyResized ($ ni, $ im, $ dstX, $ dstY, 0, 0, $ fdstW, $ fdsomething, $ srcW, $ srcH); if ($ markwords! = Null) {$ markwords = iconv ("gb2312", "UTF-8", $ markwords); // convert text encoding ImageTTFText ($ ni, 20, 30,450,560, $ black, "simhei. ttf ", $ markwords); // write a text watermark // The parameters are, text Size | deflection | abscissa | ordinate | text color | text type | text content} elseif ($ markimage! = Null) {$ wimage_data = GetImageSize ($ markimage); switch ($ wimage_data [2]) {case 1: $ wimage = @ ImageCreateFromGIF ($ markimage); break; case 2: $ wimage = @ ImageCreateFromJPEG ($ markimage); break; case 3: $ wimage = @ ImageCreateFromPNG ($ markimage); break;} imagecopy ($ ni, $ wimage, 500,560, 0, 0, 88, 31); // write the image watermark. The default image size is 88*31 imagedestroy ($ wimage);} ImageJpeg ($ ni, $ dstFile, $ rate ); imageJpeg ($ ni, $ SrcFile, $ rate); imagedestroy ($ im); imagedestroy ($ ni) ;}?>

PS: Here are some useful image processing tools for your reference:

Online image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64

ICO icon generation tool:
Http://tools.jb51.net/aideddesign/ico_img

Online Email icon creation tools:
Http://tools.jb51.net/email/emaillogo

Online image format conversion (jpg/bmp/gif/png) tool:
Http://tools.jb51.net/aideddesign/picext

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.