In-depth discussion on how to generate thumbnails in PHP _ PHP Tutorial

Source: Internet
Author: User
This article discusses how to generate thumbnails in PHP. We are using PHP to generate the thumbnail HTML code! DOCTYPEHTMLPUBLIC-W3CDTDHTML4.0TransitionalENHTMLHEADTITLE Upload image TITLEMETANAMEGeneratorCONTENT we are using

HTML code for generating thumbnails in PHP

  1. <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
  2. < HTML>
  3. < HEAD>
  4. < TITLE>Upload images</TITLE>
  5. < METANAME = "Generator" CONTENT = "EditPlus">
  6. < METANAME = "Author" CONTENT = "">
  7. < METANAME = "Keywords" CONTENT = "">
  8. < METANAME = "Description" CONTENT = "">
  9. < MetaHttp-equiv = "Content-Type" content = "text/html; charset = gbk"/>
  10. </HEAD>
  11. < BODY>
  12. < FORMMETHOD = "POST" ACTION = "tu. php? Act = upload "enctype =" multipart/form-data">
  13. < P> < INPUTTYPE = "file" NAME = "file"/> </P>
  14. < P> < INPUTTYPE = "submit" value = "hao"/> < INPUTTYPE = "reset"/> </P>
  15. </FORM>
  16. </BODY>
  17. </HTML>


PHP code for generating thumbnails

 
  1. <? Php
  2. /* Background login interface
  3. * Learning and writing
  4. * Start time:
  5. * Updated on: 2009/11/24
  6. */
  7. Require_once '../xmphp/init. php ';
  8. // Full-site configuration file
  9. Require_once XMPHP_COMMON. '/smarty. php ';
  10. // Smarty function
  11. Require_once XMPHP_COMMON. '/mysql. php ';
  12. // Mysql functions
  13. Require_once XMPHP_COMMON. '/func. php ';
  14. // Common functions
  15. If (isset ($ _ GET ['AC']) & $ _ GET ['AC']
    = 'Upload '){
  16. $ File = $ _ FILES ['file'];
  17. $ Info = pathinfo ($ file ['name']);
  18. $ A = array ('jpg ', 'GIF', 'PNG ');
  19. If (! In_array ($ info ['extension'], $ )){
  20. Alert ("Select Images in jpg, gif, and png formats.
    Upload ");
  21. Exit;
  22. }
  23. If ($ file ['name']! = ''){
  24. $ Dirname = date ('ym ');
  25. $ Dirname = '../upload/'. $ dirname;
  26. // Original image path
  27. $ Xin = $ dirname. '/s'; // PHP generates a thumbnail path.
  28. If (! File_exists ($ xin )){
  29. @ Mkdir ($ xin );
  30. }
  31. $ Image1 = $ info ['filename']. '000000_000000 '.'.'
    . $ Info ['extension']; // Thumbnail name
  32. $ Image2 = $ info ['filename']. '000000_000000 '.'.'
    . $ Info ['extension']; // Thumbnail name
  33. List ($ imagw, $ imagh) = getimagesize ($ file
    ['Tmp _ name']); // Obtain the width and height of the source image (in addition, $ file
    ['Tmp _ name'] is only a temporary file path, and sometimes
    An error is reported. If an error is reported, you only need to change the temporary file path to your Upload path.
    The path of the source image)
  34. // Figure 120 determines various situations to obtain the width and height of the thumbnail
  35. If ($ imagw <= 120 & $ imagh <= 120 ){
  36. $ M120w = $ imagw;
  37. $ M120h = $ imagh;
  38. } Elseif ($ imagw> $ imagh)
  39. {
  40. $ M120w = 120;
  41. $ M120h = intval ($ m120w/number_format (
    $ Imagw/$ imagh, 2); // divide the width of the thumbnail by (original
    Width divided by height of the source image) number_format query manual
  42. }
  43. Else
  44. {
  45. $ M120h = 300;
  46. $ M120w = intval ($ m120h/number_format (
    $ Imagh/$ imagw, 2); // divide the height of the thumbnail by (original image
    Height divided by the width of the source image) number_format query manual
  47. }
  48. // Figure 300 determines various situations to obtain the width and height of the thumbnail
  49. If ($ imagw <= 300 & $ imagh <= 300 ){
  50. $ M300w = $ imagw;
  51. $ M300h = $ imagh;
  52. } Elseif ($ imagw> $ imagh ){
  53. $ M300w = 300;
  54. $ M300h = intval ($ m300w/number_format (
    $ Imagw/$ imagh, 2); // divide the width of the thumbnail by (original
    Width divided by height of the source image) number_format query manual
  55. } Else {
  56. $ M300h = 300;
  57. $ M300w = intval ($ m300h/number_format (
    $ Imagh/$ imagw, 2); // divide the height of the thumbnail by (original
    Height of an image divided by the width of the source image) number_format query manual
  58. }
  59. If ($ info ['extension'] = 'jpg '){
  60. $ Info ['extension'] = 'jpeg '; // determines the Image class.
    If the image is JPG, convert it to JPEG.
    When processing JPG, the processing function uses JPEG instead of JPG.
  61. }
  62. $ Iml = 'imagecreatefrom '. $ info ['extension'];
  63. // Obtain different functions of different image formats
  64. $ Yuan = $ iml ($ file ['tmp _ name']);
  65. // Identify the source image based on the different functions obtained earlier
  66. $ Mu1 = imagecreatetruecolor ($ m120w, $ m120h );
  67. // ID of the thumbnail to be generated
  68. $ Mu2 = imagecreatetruecolor ($ m300w, $ m300h );
  69. // ID of the thumbnail to be generated
  70. $ C = 'image'. $ info ['extension'];
  71. // The function for generating the output thumbnail, which cannot be written separately; otherwise, an error is returned.
  72. Imagecopyresampled ($ mu1, $ yuan, 0, 0,
    0, $ m120w, $ m120h, $ imagw, $ imagh); // scale 20
  73. $ Res = $ c ($ mu1, $ xin. '/'. $ image1 );
  74. // Store thumbnails
  75. If (! $ Res ){
  76. Alert ('generating 120 thumbnails failed ');
  77. Exit;
  78. }
  79. Imagecopyresampled ($ mu2, $ yuan, 0, 0
    , 0, 0, $ m300w, $ m300h, $ imagw, $ imagh );
  80. // Thumbnails generated by PHP are reduced by 20
  81. $ Res = $ c ($ mu2, $ xin. '/'. $ image2 );
  82. // Store thumbnails
  83. If ($ res ){
  84. Alert ('Success in generating 300 thumbnails ');
  85. Exit;
  86. }
  87. }
  88. }
  89. $ Tpl-> display ("tu/tu.html ");
  90. ?>

The above code example is related to the implementation of generating thumbnails in PHP.


PHP generates the thumbnail HTML code! DOCTYPEHTMLPUBLIC "-// W3C // DTDHTML4.0Transitional // EN"> HTML> HEAD> TITLE> upload image/TITLE> meta name = "Generator" CONTENT =...

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.