Add watermarks & proportional thumbnails & fixed height & fixed width in PHP

Source: Internet
Author: User
Add watermark & amp; proportional thumbnail & amp; fixed height & amp; fixed width class in PHP
PHP adds watermark & proportional thumbnails & fixed height & fixed width class.
When using foreach loop processing, you need to set a time for sleep or follow the returned value after processing. Otherwise, the processing will not end.

Download: http://pan.baidu.com/s/1ntKAfFF

  1. // File name: image_process.class.php
  2. Class Image_process {
  3. Public $ source; // source image
  4. Public $ source_width; // width
  5. Public $ source_height; // high
  6. Public $ source_type_id;
  7. Public $ orign_name;
  8. Public $ orign_dirname;
  9. // Input the 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. // Judge and process, 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 watermarkImage ($ 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;
  44. $ Y = $ this-> source_height-$ logo_height;
  45. ImageCopy ($ sourceHandle, $ logoHandle, $ x, $ y, 0, $ logo_width, $ logo_width) or die ("fail to combine ");
  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. // Fix width
  53. // Height = true fixed top height
  54. // Width = true fixed top width
  55. Public function fixSizeImage ($ width, $ height ){
  56. If ($ width> $ this-> source_width) $ this-> source_width;
  57. If ($ height> $ this-> source_height) $ this-> source_height;
  58. If ($ width = false ){
  59. $ Width = floor ($ this-> source_width/($ this-> source_height/$ height ));
  60. }
  61. If ($ height = false ){
  62. $ Height = floor ($ this-> source_height/($ this-> source_width/$ width ));
  63. }
  64. $ This-> tinyImage ($ width, $ height );
  65. }
  66. // Proportional scaling
  67. // Scale the scale of $ scale
  68. Public function scaleImage ($ scale ){
  69. $ Width = floor ($ this-> source_width * $ scale );
  70. $ Height = floor ($ this-> source_height * $ scale );
  71. $ This-> tinyImage ($ width, $ height );
  72. }
  73. // Create a thumbnail
  74. Private function tinyImage ($ width, $ height ){
  75. $ TinyImage = imagecreatetruecolor ($ width, $ height );
  76. $ Handle = $ this-> judgeType ($ this-> source_type_id, $ this-> orign_url );
  77. If (function_exists ('imagecopyresampled ')){
  78. Imagecopyresampled ($ tinyImage, $ handle, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height );
  79. } Else {
  80. Imagecopyresized ($ tinyImage, $ handle, 0, 0, 0, $ width, $ height, $ this-> source_width, $ this-> source_height );
  81. }
  82. $ NewPic = time (). '_'. $ width. '_'. $ height. '.'. $ this-> typeList [$ this-> source_type_id];
  83. $ NewPic = $ this-> orign_dirname. '\ thumb _'. $ newPic;
  84. If ($ this-> saveImage ($ tinyImage, $ newPic )){
  85. Imagedestroy ($ tinyImage );
  86. Imagedestroy ($ handle );
  87. }
  88. }
  89. // Save the image
  90. Private function saveImage ($ image, $ url ){
  91. If (ImageJpeg ($ image, $ url )){
  92. Return true;
  93. }
  94. }
  95. }

  1. // Use
  2. Include ('image _ process. class. php ');
  3. $ M = array (
  4. 'D: \ myspace \ test \ image_process \ 1.jpg ',
  5. 'D: \ myspace \ test \ image_process \ 2.jpg ',
  6. 'D: \ myspace \ test \ image_process \ 3.jpg ',
  7. 'D: \ myspace \ test \ image_process \ 4.jpg'
  8. );
  9. $ Img = 'd: \ myspace \ test \ image_process \ 1.jpg ';
  10. $ Logo = 'd: \ myspace \ test \ image_process \ logo.png ';
  11. Foreach ($ m as $ item ){
  12. $ S = new Image_process ($ item );
  13. $ S-> watermarkImage ($ logo );
  14. $ S-> scaleImage (0, 0.8 );
  15. $ S-> fixSizeImage (200, false );
  16. Sleep (1 );
  17. }

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.