Generate a thumbnail based on the request and save it to the Linux image server.

Source: Internet
Author: User

<? Php <br/> $ picid = $ _ Get ['imgid']; <br/> $ pictypes = ". ". $ _ Get ['imgtype']; <br/> $ picwidth = $ _ Get ['imgwidth']; <br/> if ($ picid! = "") <Br/>{< br/> // a thumbnail is requested. <br/> if ($ picwidth> 0) {<br/> // If the thumbnail exists <br/> If (file_exists ($ picid. "_". $ picwidth. $ pictypes) <br/>{< br/> outputimg ($ picid. "_". $ picwidth. $ pictypes); <br/>}else <br/>{< br/> If (file_exists ($ picid. "_ 0 ". $ pictypes) {<br/> // generate a thumbnail directly if no thumbnail exists <br/> resizeimg ($ picid. "_ 0 ". $ pictypes, $ picwidth, $ picwidth, $ picid. "_". $ picwidth. $ pictypes); <br/> // output <br/> outputimg ($ picid. "_". $ picwidt H. $ pictypes); <br/>}else <br/>{< br/> // If the larger image does not exist <br/> resizeimg('nodefaultimage.gif ', $ picwidth, $ picwidth, nodefaultimage. "_". $ picwidth. ". GIF "); <br/> // output <br/> outputimg ($ picid. "_". $ picwidth. $ pictypes ); <br/>}< br/> // determines whether a large image exists in the file. <br/> If (file_exists ($ picid. "_ 0 ". $ pictypes) <br/>{< br/> $ img_file = $ picid. "_ 0 ". $ pictypes; <br/> outputimg ($ img_file); <br/>}< br/> else <br/>{< br/> // for Example If no image exists, <br/> $ img_file = 'nodefaultimage.gif '; <br/> outputimg ($ img_file ); <br/>}< br/> // output image <br/> function outputimg ($ img_file) <br/>{< br/> $ fp = fopen ($ img_file, 'rb'); <br/> $ content = fread ($ FP, filesize ($ img_file); // binary data <br/> fclose ($ FP); <br/> header ('content-type: image/gif '); <br/> echo $ content; <br/>}< br/>/** <br/> * generate a thumbnail <br/> * $ srcname ---- path of the original image <br/> * $ newwidth, $ newheight- --- Maximum width and height of the thumbnails respectively <br/> * $ newname ---- the thumbnail file name (including path) <br/> * @ Param string $ srcname <br/> * @ Param int $ newwidth <br/> * @ Param int $ newheight <br/> * @ Param string $ newname <br/> * return viod <br/> */<br/> function resizeimg ($ srcname, $ newwidth, $ newheight, $ newname = "") <br/>{< br/> if ($ newname = "") <br/>{< br/> $ namearr = explode ('. ', $ srcname); <br/> $ expname = array_pop ($ namearr); <br/> $ expname = $ expname; <br/> Array_push ($ namearr, $ expname); <br/> $ newname = implode ('. ', $ namearr); <br/>}< br/> $ info = ""; <br/> $ DATA = getimagesize ($ srcname, $ info ); <br/> switch ($ data [2]) <br/>{< br/> case 1: <br/> If (! Function_exists ("imagecreatefromgif") {<br/> echo "your GD library cannot use images in GIF format. use JPEG or PNG format! Return "; <br/> exit (); <br/>}< br/> $ im = imagecreatefromgif ($ srcname); <br/> break; <br/> case 2: <br/> If (! Function_exists ("imagecreatefromjpeg") {<br/> echo "your GD library cannot use images in JPEG format. Please use images in other formats! Return "; <br/> exit (); <br/>}< br/> $ im = imagecreatefromjpeg ($ srcname); <br/> break; <br/> case 3: <br/> $ im = imagecreatefrompng ($ srcname); <br/> break; <br/>}< br/> $ srcw = imagesx ($ IM); <br/> $ srch = imagesy ($ IM ); <br/> $ newwidthh = $ newwidth/$ newheight; <br/> $ srcwh = $ srcw/$ srch; <br/> if ($ newwidthh <= $ srcwh) {<br/> $ ftow = $ newwidth; <br/> $ ftoh = $ ftow * ($ srch/$ srcw ); <br/>}< br/> else {<br/> $ ftoh = $ newhe Ight; <br/> $ ftow = $ ftoh * ($ srcw/$ srch ); <br/>}< br/> if ($ srcw> $ newwidth | $ srch> $ newheight) <br/>{< br/> If (function_exists ("imagecreatetruecolor") <br/>{< br/>@$ ni = imagecreatetruecolor ($ ftow, $ ftoh ); <br/> if ($ Ni) imagecopyresampled ($ Ni, $ im, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch ); <br/> else <br/>{< br/> $ ni = imagecreate ($ ftow, $ ftoh); <br/> imagecopyresized ($ Ni, $ im, 0, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch); <BR/>}< br/> else <br/>{< br/> $ ni = imagecreate ($ ftow, $ ftoh ); <br/> imagecopyresized ($ Ni, $ im, 0, 0, 0, $ ftow, $ ftoh, $ srcw, $ srch ); <br/>}< br/> If (function_exists ('imagejpeg ') imagejpeg ($ Ni, $ newname); <br/> else imagepng ($ Ni, $ newname); <br/> imagedestroy ($ Ni); <br/>}< br/> imagedestroy ($ IM); <br/>}< br/>?> 

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.