Home > Backend Development > PHP Tutorial > PHP add watermark & ​​proportional thumbnail & fixed height & fixed width class

PHP add watermark & ​​proportional thumbnail & fixed height & fixed width class

WBOY
Release: 2016-07-25 08:47:29
Original
845 people have browsed it
PHP adds watermark & ​​proportional thumbnails & fixed height & fixed width classes.
When using foreach loop processing, you need to set a sleep time or follow the return value after processing, otherwise the processing will not be completed.

Download: http://pan.baidu.com/s/1ntKAfFF
  1. //File name: image_process.class.php
  2. class Image_process{
  3. public $source;//Original image
  4. public $source_width;//Width
  5. public $source_height;//Height
  6. public $source_type_id;
  7. public $orign_name;
  8. public $orign_dirname;
  9. //Pass in 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, 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 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,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 scaling 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 the image
  90. private function saveImage($image,$url){
  91. if(ImageJpeg($image,$url)){
  92. return true;
  93. }
  94. }
  95. }
Copy code
  1. //使用
  2. include('image_process.class.php');
  3. $m = array(
  4. 'D:myspacetestimage_process1.jpg',
  5. 'D:myspacetestimage_process2.jpg',
  6. 'D:myspacetestimage_process3.jpg',
  7. 'D:myspacetestimage_process4.jpg'
  8. );
  9. $img = 'D:myspacetestimage_process1.jpg';
  10. $logo = 'D:myspacetestimage_processlogo.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. }
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template