PHP adds watermark & amp; proportional thumbnails & amp; fixed height & amp; fixed width class.

Source: Internet
Author: User
PHP adds watermark & amp; proportional thumbnail & amp; fixed height & amp; fixed width class.

  1. // Add watermark & proportional thumbnail & fixed height & fixed width in PHP.
  2. Class Image_process {
  3. Public $ source; // source image
  4. Public $ source_width; // source image width
  5. Public $ source_height; // source image height
  6. Public $ source_type_id;
  7. Public $ orign_name;
  8. Public $ orign_dirname;
  9. // Input the source image path
  10. Public function _ construct ($ source ){
  11. $ This-> typeList = array (1 => 'GIF', 2 => 'jpg ', 3 => 'PNG ');
  12. $ Ginfo = getimagesize ($ source );
  13. $ This-> source_width = $ ginfo [0];
  14. $ This-> source_height = $ ginfo [1];
  15. $ This-> source_type_id = $ ginfo [2];
  16. $ This-> orign_url = $ source;
  17. $ This-> orign_name = basename ($ source );
  18. $ This-> orign_dirname = dirname ($ source );
  19. }
  20. // Determine the format of the image file and return the PHP identifiable code
  21. Public function judgeType ($ type, $ source ){
  22. If ($ type = 1 ){
  23. Return imagecreatefromgif ($ source); // gif
  24. } Else if ($ type = 2 ){
  25. Return imagecreatefromjpeg ($ source); // jpg
  26. } Else if ($ type = 3 ){
  27. Return imagecreatefrompng ($ source); // png
  28. } Else {
  29. Return false;
  30. }
  31. }
  32. // Generate a watermark image
  33. Public function waterMakeImage ($ logo ){
  34. $ Linfo = getimagesize ($ logo );
  35. $ Logo_width = $ linfo [0];
  36. $ Logo_height = $ linfo [1];
  37. $ Logo_type_id = $ linfo [2];
  38. $ SourceHandle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url );
  39. $ LogoHandle = $ this-> judgeType ($ logo_type_id, $ logo );
  40. If (! $ SourceHandle |! $ LogoHandle ){
  41. Return false;
  42. }
  43. $ X = ($ this-> source_width-$ logo_width)/2;
  44. $ Y = ($ this-> source_height-$ logo_height)/2;
  45. Imagecopy ($ sourceHandle, $ logoHandle, $ x, $ y, 0, $ logo_width, $ logo_height );
  46. $ NewPic = $ this-> orign_dirname. '\ water _'. time (). '.'. $ this-> typeList [$ this-> source_type_id];
  47. If ($ this-> saveImage ($ sourceHandle, $ newPic )){
  48. Imagedestroy ($ sourceHandle );
  49. Imagedestroy ($ logoHandle );
  50. }
  51. }
  52. // Fixed height and width
  53. Public function fixSizeImage ($ width, $ height ){
  54. If ($ width> $ this-> source_width) $ this-> source_width;
  55. If ($ height> $ this-> source_height) $ this-> source_height;
  56. If ($ width = false ){
  57. $ Width = floor ($ this-> source_width/($ this-> source_height/$ height ));
  58. }
  59. If ($ height = false ){
  60. $ Height = floor ($ this-> source_height/($ this-> source_width/$ width ));
  61. }
  62. $ This-> tinyImage ($ width, $ height );
  63. }
  64. // Proportional scaling
  65. Public function scaleImage ($ scale ){
  66. $ Width = floor ($ this-> source_width * $ scale );
  67. $ Height = floor ($ this-> source_height * $ scale );
  68. $ This-> tinyImage ($ width, $ height );
  69. }
  70. // Create a thumbnail
  71. Public function tinyImage ($ width, $ height ){
  72. $ TinyImage = imagecreatetruecolor ($ width, $ height );
  73. $ Handle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url );
  74. If (function_exists ('imagecopyresampled ')){
  75. Imagecopyresampled ($ tinyImage, $ handle, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height );
  76. } Else {
  77. Imagecopyresized ($ tinyImage, $ handle, 0, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height );
  78. }
  79. $ NewPic = $ this-> orign_dirname. '\ thumb _'. time (). '_'. $ width. "_". $ height. ". ". $ this-> typeList [$ this-> source_type_id];
  80. If ($ this-> saveImage ($ tinyImage, $ newPic )){
  81. Imagedestroy ($ tinyImage );
  82. Imagedestroy ($ handle );
  83. }
  84. }
  85. // Save the image
  86. Private function saveImage ($ image, $ url ){
  87. If (imagejpeg ($ image, $ url )){
  88. Return true;
  89. }
  90. }
  91. }
  92. $ ImgHandle = new Image_process ('d: \ AppServ \ www \ test \ getimg \ 14063797445601.jpg ');
  93. // $ ImgHandle-> waterMakeImage ('d: \ AppServ \ www \ test \ getimg \ shougongke.png '); // Generate a watermark image
  94. // $ ImgHandle-> fixSizeImage (200,150); // fixed-length image
  95. $ ImgHandle-> scaleImage (0.2); // proportional scaling
  96. ?>


PHP, amp

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.