图片操作类(文字水印,图片水印,非等比缩放),还有哪些操作?准备将其他的操作补齐作为工具类
<?php
/*********************
* pictureDeal.class.php
* 图片处理类
*
*********************/
class PictureDeal{
private $imagePathName;
public function __construct($filepath){
$this->init($filepath);
}
public function init($filepath){
if(is_string($filepath) && file_exists($filepath) && (getimagesize($filepath)!==false)){
$this->imagePathName=$filepath;
}
else{
//return false;
exit("不是图片或文件不存在!");
}
}
/*
* @function 给图片添加文字水印
* @param string $savePathName 保存路径及文件名(不含后缀名), string $content 文字内容, int $fontSize 字体大小, string $font 字体路径, array $fontColor RGB对应的int值0-255, int $transparency 透明度, int $x 相距原图0,0点的横向距离, int $y 相距原图0,0点的纵向距离
*/
public function addTextWaterMark($savePathName,$content,$fontSize,$font,$fontColor,$transparency,$x,$y){
$imageInfo=$this->getImageInfo($this->imagePathName);
$imageExt=$this->getImageExt($imageInfo);
$this->operateTextCarbon($savePathName,$imageExt,$content,$fontSize,$font,$fontColor,$transparency,$x,$y);//图片打开,操作,输出,销毁
}
/*
* @function 给图片添加图片水印
* @param string $savePathName 保持路径及文件名(不含后缀名), string $waterImagePath 水印图片的全路径, int $transparency 透明度, int $x 相距原图0,0点的横向距离, int $y 相距原图0,0点的纵向距离
*/
public function addPicWaterMark($savePathName,$waterImagePath,$transparency,$x,$y){
$imageInfo=$this->getImageInfo($this->imagePathName);
$imageExt=$this->getImageExt($imageInfo);
$waterImageInfo=getimagesize($waterImagePath);
$waterImageExt=$this->getImageExt($waterImageInfo);
$this->operateImageCarbon($savePathName,$imageExt,$imageInfo,$waterImagePath,$waterImageExt,$waterImageInfo,$transparency,$x,$y);
}
/*
* @function 压缩图片(非等比缩放)
* @param string $savePathName 保存路径及文件名(不含后缀名), int $width 缩放后的宽度, int $height 缩放后的高度
*/
public function thumbImage($savePathName,$width,$height){
$imageInfo=$this->getImageInfo($this->imagePathName);
$imageExt=$this->getImageExt($imageInfo);
$this->operateThumbImage($savePathName,$imageExt,$imageInfo,$width,$height);
}
/*
* @function 获取图片信息
* @param
* @return array
*/
public function getImageInfo($file){
$info=getimagesize($file);
return $info;
}
/*
* @function 获得图像后缀名
* @param array $imageInfo
* @return string
*/
public function getImageExt($imageInfo){
return image_type_to_extension($imageInfo[2],false);
}
/*
* @function 对图片添加文字水印操作并生成新图片
* @param string $imageExt 原图片的后缀名
* @notice 包含打开,操作,输出,销毁图片的操作
*/
public function operateTextCarbon($savePathName,$imageExt,$content,$fontSize,$font,$fontColor,$transparency,$x,$y){
$tmp_handler=$this->getImageResource($imageExt,$this->imagePathName);//打开资源
$color=imagecolorallocatealpha($tmp_handler,$fontColor["R"],$fontColor["G"],$fontColor["B"],$transparency);
imagettftext($tmp_handler, $fontSize, 0, $x, $y, $color, $font, $content);//操作图片
$this->createImageByResource($imageExt,$tmp_handler,$savePathName);//输出图片
imagedestroy($tmp_handler);//销毁图片
}
/*
* @function 对图片添加图片水印操作并生成新图片
* @param string $imageExt 原图片的后缀名, array $imageInfo 原图片信息, string $waterImagePath 水印图片全路径
* @notice 包含打开,操作,输出,销毁图片的操作
*/
public function operateImageCarbon($savePathName,$imageExt,$imageInfo,$waterImagePath,$waterImageExt,$waterImageInfo,$transparency,$x,$y){
$this->copyResourceImage($imageExt,$imageInfo,$savePathName);//复制图片
$carbon_filename=$savePathName.".".$imageExt;//获得复制后的图片全路径名
$tmp_carbon_handler=$this->getImageResource($imageExt,$carbon_filename);//打开副本图片资源
$tmp_water_handler=$this->getImageResource($waterImageExt,$waterImagePath);//打开水印图片资源
//操作图片
imagecopymerge($tmp_carbon_handler, $tmp_water_handler, $x, $y, 0, 0, $waterImageInfo[0], $waterImageInfo[1], $transparency);
//header("content-type:image/jpeg");
//imagejpeg($tmp_carbon_handler);
$this->createImageByResource($imageExt,$tmp_carbon_handler,$savePathName);
imagedestroy($tmp_water_handler);//销毁图片
imagedestroy($tmp_carbon_handler);//销毁图片
}
/*
* @function 复制原图,以副本进行图像操作
* @param string $imageExt 原图后缀名, array $imageInfo 原图信息, string $savePathName 保存路径及文件名(不含后缀名)
*/
public function copyResourceImage($imageExt,$imageInfo,$savePathName){
$tmp_handler=$this->getImageResource($imageExt,$this->imagePathName);
$tmp_carbon_handler=imagecreatetruecolor($imageInfo[0],$imageInfo[1]);
imagecopy($tmp_carbon_handler,$tmp_handler,0,0,0,0,$imageInfo[0],$imageInfo[1]);
$this->createImageByResource($imageExt,$tmp_carbon_handler,$savePathName);
imagedestroy($tmp_handler);
imagedestroy($tmp_carbon_handler);
}
public function operateThumbImage($savePathName,$imageExt,$imageInfo,$width,$height){
$tmp_dst_handler=imagecreatetruecolor($width,$height);
$tmp_handler=$this->getImageResource($imageExt,$this->imagePathName);
imagecopyresampled ($tmp_dst_handler, $tmp_handler, 0, 0, 0, 0, $width, $height, $imageInfo[0], $imageInfo[1]);
$this->createImageByResource($imageExt,$tmp_dst_handler,$savePathName);
imagedestroy($tmp_handler);
imagedestroy($tmp_dst_handler);
}
/*
* @function 根据后缀名以不同的形式获取资源句柄
* @param string $imageExt 原图后缀名, string $file 原图全路径
* @return resource
*/
public function getImageResource($imageExt,$file){
$handler=null;
if($imageExt=="jpeg"){
$handler=imagecreatefromjpeg($file);
}
if($imageExt=="png"){
$handler=imagecreatefrompng($file);
}
if($imageExt=="gif"){
$handler=imagecreatefromgif($file);
}
return $handler;
}
/*
* @function 根据后缀名生成格式不同的图片
* @param string $imageExt 原图后缀名, resource $handler 图片资源句柄, string $savePathName 保存路径及文件名(不含后缀名)
*/
public function createImageByResource($imageExt,$handler,$savePathName){
if($imageExt=="jpeg"){
imagejpeg($handler,$savePathName.".".$imageExt);
}
if($imageExt=="png"){
imagepng($handler,$savePathName.".".$imageExt);
}
if($imageExt=="gif"){
imagegif($handler,$savePathName.".".$imageExt);
}
}
}
/*
//文字水印的测试
$pathname="./resource/Nico_cool_r342.jpg";
$picDeal=new PictureDeal($pathname);
$savePathName="./resource/new_image_text";
$content="にっこにっこにーー!";
$fontSize=20;
$font="./resource/msyh.ttc";
$fontColor=array("R"=>0,"G"=>0,"B"=>0);
$transparency=50;
$x=55;
$y=20;
$picDeal->addTextWaterMark($savePathName,$content,$fontSize,$font,$fontColor,$transparency,$x,$y);
*/
/*
//图片水印的测试
$pathname="./resource/Nico_cool_r342.jpg";
$picDeal=new PictureDeal($pathname);
$savePathName="./resource/new_image_img";
$waterImagePath="./resource/getimage_2.jpg";
$transparency=50;
$x=10;
$y=20;
$picDeal->addPicWaterMark($savePathName,$waterImagePath,$transparency,$x,$y);
*/
//非等比缩放的测试
$pathname="./resource/Nico_cool_r342.jpg";
$picDeal=new PictureDeal($pathname);
$savePathName="./resource/new_image_thumb";
$width=60;
$height=80;
$picDeal->thumbImage($savePathName,$width,$height);
?>本人菜鸟一个,各位大大们看看,有写的不好的地方请指出,个人不喜欢动态调用方法,所以直接就用多重if了。这几天在赶项目等空下来,准备将图片的其余操作给补全了,作为将来可以拿来作为工具类。