PHP Add Watermark & Proportional thumbnail image & Fixed height & Fixed width class.

Source: Internet
Author: User
  1. PHP add watermark & scale thumbnail & fixed height & fixed width class.
  2. Class image_process{
  3. Public $source; Original
  4. Public $source _width; Original width
  5. Public $source _height; Original height
  6. Public $source _type_id;
  7. Public $orign _name;
  8. Public $orign _dirname;
  9. Incoming original 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 code that is recognized by PHP
  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. Create a watermark Picture
  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,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 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. Scale pictures in equal proportion
  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 image
  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, 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 picture
  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\14061907445601.jpg ');
  93. $imgHandle->watermakeimage (' D:\AppServ\www\test\getimg\shougongke.png '); Create a watermark Picture
  94. $imgHandle->fixsizeimage (200,150); Fixed-length pictures
  95. $imgHandle->scaleimage (0.2); Equal scale Scaling
  96. ?>
Copy Code
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.