PHP adds watermark & ​​proportional thumbnails & fixed height & fixed width classes.

WBOY
Release: 2016-07-25 08:42:52
Original
899 people have browsed it
  1. //PHP Add watermark & ​​proportional thumbnail & fixed height & fixed width class.
  2. class Image_process{
  3. public $source; //Original image
  4. public $source_width; //Original image width
  5. public $source_height; //Original image height
  6. public $source_type_id;
  7. public $orign_name;
  8. public $orign_dirname;
  9. //Pass in the original 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 the picture The format of the file, returns the encoding 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. //Generate 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,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 the image
  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, 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:AppServwwwtestgetimg14061907445601.jpg');
  93. //$imgHandle->waterMakeImage('D:AppServwwwtestgetimgshougongke.png'); //Generate watermark pictures
  94. //$imgHandle->fixSizeImage(200,150); //Fixed length image
  95. $imgHandle->scaleImage(0.2); //Equal scaling
  96. ?>
Copy code

PHP, amp


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