欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类。

程序员文章站 2022-05-25 19:04:39
...
  1. //PHP 添加水印 & 比例缩略图 & 固定高度 & 固定宽度 类。
  2. class Image_process{
  3. public $source; //原图
  4. public $source_width; //原图宽度
  5. public $source_height; //原图高度
  6. public $source_type_id;
  7. public $orign_name;
  8. public $orign_dirname;
  9. //传入原图路径
  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. //判断图片的文件的格式,返回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. //生成水印图片
  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. //固定高度宽度
  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. //等比例缩放图片
  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. //创建缩略图
  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. //保存图片
  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'); //生成水印图片
  94. //$imgHandle->fixSizeImage(200,150); //固定长度图片
  95. $imgHandle->scaleImage(0.2); //等比例缩放
  96. ?>
复制代码

PHP, amp