今天网上学习了一段PHP创建缩略图还有打水印的代码,如下:
其中将图片的路径作为参数传给函数,打水印的过程就是首先获取图片和logo的参数信息,然后将logo图片拷贝到原图的某个位置,然后保存,水印打入完毕。
创建缩略图就是将缩放比例传入函数,然后调整元图片的宽度和高度,进行保存。
感觉这段代码当中比较巧妙地就是用到了图片的路径保存还有就是这个大量运用到PHP提供的图片处理函数,这点是非常重要的,没见过的函数要多查查手册,大体就能理解功能了 。
图片处理类:
<?php class Image_process{ public $source;//原图 public $source_width;//宽 public $source_height;//高 public $source_type_id;//原图的类型 public $orign_name;//文件名 public $orign_dirname;//路径名 public function __construct($source){ //传入图片路径作为参数 $this->typeList = array(1=>‘gif‘,2=>‘jpg‘,3=>‘png‘); $ginfo = getimagesize($source); //获取图片的参数 $this->source_width = $ginfo[0]; $this->source_height = $ginfo[1]; $this->source_type_id= $ginfo[2]; $this->orign_url = $source; $this->orign_name = basename($source); $this->orign_dirname = dirname($source); } public function judgeType($type,$source){ //判断并处理,返回PHP可识别编码 if($type==1){ return ImageCreateFromGIF($source);//gif }else if($type==2){ return ImageCreateFromJPEG($source);//jpg }else if($type==3){ return ImageCreateFromPNG($source);//png }else{ return false; } } public function watermarkImage($logo){ //生成水印图 $linfo = getimagesize($logo); $logo_width = $linfo[0]; $logo_height = $linfo[1]; $logo_type_id = $linfo[2]; $sourceHandle = $this->judgeType($this->source_type_id,$this->orign_url); $logoHandle = $this->judgeType($logo_type_id,$logo); if( !$sourceHandle || ! $logoHandle ){ return false; } $x = $this->source_width - $logo_width; $y = $this->source_height- $logo_height; ImageCopy($sourceHandle,$logoHandle,$x,$y,0,0,$logo_width,$logo_width) or die("fail to combine"); $newPic = $this->orign_dirname .‘\water_‘. time().‘.‘. $this->typeList[$this->source_type_id]; //将小logo作为水印拷贝到原图片的某个位置 if( $this->saveImage($sourceHandle,$newPic)){ //保存将水印写入 imagedestroy($sourceHandle); imagedestroy($logoHandle); } } // fix 宽度 // height = true 固顶高度 // width = true 固顶宽度 public function fixSizeImage($width,$height){ if( $width > $this->source_width) $this->source_width; if( $height > $this->source_height ) $this->source_height; if( $width === false){ $width = floor($this->source_width / ($this->source_height / $height)); } if( $height === false){ $height = floor($this->source_height / ($this->source_width / $width)); } $this->tinyImage($width,$height); } //比例缩放 // $scale 缩放比例 public function scaleImage($scale){ $width = floor($this->source_width * $scale); //按照scale比例将原图进行缩放 $height = floor($this->source_height * $scale); $this->tinyImage($width,$height); } //创建略缩图 private function tinyImage($width,$height){ $tinyImage = imagecreatetruecolor($width, $height ); $handle = $this->judgeType($this->source_type_id,$this->orign_url); if(function_exists(‘imagecopyresampled‘)){ imagecopyresampled($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height); }else{ imagecopyresized($tinyImage,$handle,0,0,0,0,$width,$height,$this->source_width,$this->source_height); } $newPic = time().‘_‘.$width.‘_‘.$height.‘.‘. $this->typeList[$this->source_type_id]; $newPic = $this->orign_dirname .‘\thumb_‘. $newPic; if( $this->saveImage($tinyImage,$newPic)){ imagedestroy($tinyImage); imagedestroy($handle); } } //保存图片 private function saveImage($image,$url){ if(ImageJpeg($image,$url)){ return true; } } } ?>
调用以上类代码:
<?php include(‘image_process.class.php‘); $m = array( ‘D:\localhost\1.jpg‘, ‘D:\localhost\2.jpg‘, ‘D:\localhost\3.jpg‘, ‘D:\localhost\4.jpg‘ ); //$img = ‘D:\localhost\1.jpg‘; $logo = ‘D:\localhost\logo.jpg‘; foreach( $m as $item){ $s = new Image_process( $item ); $s->watermarkImage($logo);//生成水印 $s->scaleImage(0.8);//生成缩略图 $s->fixSizeImage(200,false); sleep(1); echo $m." is done"."<br/>"; } ?>
以上参考:http://www.oschina.net/code/snippet_163553_37508#55208
时间: 2024-10-09 22:54:10