PHP add watermark & scale thumbnail & fixed height & fixed width class

Source: Internet
Author: User
PHP add watermark & scale thumbnail & fixed height & fixed width class.
When using the Foreach Loop, sleep is required to set a time or follow the returned value after processing, otherwise the processing is not complete.

Download: Http://pan.baidu.com/s/1ntKAfFF
  1. File name: image_process.class.php
  2. Class image_process{
  3. public $source;//Original
  4. Public $source _width;//Wide
  5. Public $source _height;//High
  6. Public $source _type_id;
  7. Public $orign _name;
  8. Public $orign _dirname;
  9. Incoming picture 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. Judgment and processing, return PHP recognizable encoding
  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 Watermark Map
  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,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 headspace height
  54. width = true headspace 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 Zoom ratio
  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,0, $width, $height, $this->source_width, $this->source_height);
  79. }else{
  80. Imagecopyresized ($tinyImage, $handle, 0,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 picture
  90. Private Function SaveImage ($image, $url) {
  91. if (imagejpeg ($image, $url)) {
  92. return true;
  93. }
  94. }
  95. }
Copy Code
    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.8);
    15. $s->fixsizeimage (200,false);
    16. Sleep (1);
    17. }
Copy Code
  • 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.