项目链接:http://www.yusian.com/thread-11071-1-1.html
[PHP] 纯文本查看 复制代码 <?php
class Image{
private $path;
// 构造方法,给定图片所在的目录
public function __construct($path){
$this->path = $path;
}
// 1、创建缩略图
public function thumb($filename, $w, $h, $prefix){
$file = $this->path."/".$filename;
list($width, $height, $type) = getimagesize($file);
// 修正比例
$p = 1;
if ($width > $height){
$p = $w / $width;
$h = $p * $height;
}else{
$p = $h / $height;
$w = $p * $width;
}
// 创建图片资源和新图的图片资源
$image = $this->get_image_source($file);
$image_new = imagecreatetruecolor($w, $h);
// 将原图的指定区域绘制到新图的指定区域即可
imagecopyresampled($image_new, $image, 0, 0, 0, 0, $w, $h, $width, $height);
// 输出图片并释放资源
$save_image_method = "image".$this->file_type($file);
$save_image_method($image_new, $this->path."/".$prefix.$filename);
imagedestroy($image);
}
// 2、增加图片水印
public function water_mark($filename, $string, $position, $save_name){
$file = $this->path."/".$filename;
list($width, $height, $type) = getimagesize($file);
$image = $this->get_image_source($file);
$color = imagecolorallocate($image, 128, 128, 128);
$font = 5;
$w = imagefontwidth($font) * strlen($string);
$h = imagefontheight($font);
$x = 10;
$y = 10;
switch($position){
case 1:{$x = 10; $y = 10;}break;
case 4:{$x = 10; $y = ($height - $h)*0.5;}break;
case 7:{$x = 10; $y = ($height - $h) - 10;}break;
case 2:{$x = ($width - $w)*0.5; $y = 10;}break;
case 5:{$x = ($width - $w)*0.5; $y = ($height - $h)*0.5;}break;
case 8:{$x = ($width - $w)*0.5; $y = ($height - $h) - 10;}break;
case 3:{$x = ($width - $w) - 10; $y = 10;}break;
case 6:{$x = ($width - $w) - 10; $y = ($height - $h)*0.5;}break;
default:{$x = ($width - $w) - 10; $y = ($height - $h) - 10;}break;
}
imagestring($image, $font, $x, $y, $string, $color);
// 输出图片并释放资源
$save_image_method = "image".$this->file_type($file);
$new_filename = strlen($save_name) ? $save_name : $filename;
$save_image_method($image, $this->path."/".$new_filename);
imagedestroy($image);
}
// 3、使用unlink函数删除图片
public function delete_image($filename){
$file_path = $this->path."/".$filename;
$thumb_path = $this->path."/thumb_".$filename;
if(file_exists($file_path) && $filename)unlink($file_path);
if(file_exists($thumb_path) && $filename)unlink($thumb_path);
}
/****************私有方法******************/
// 获取图片文件类型
private function file_type($file){
list($width, $height, $type) = getimagesize($file);
$type_array = array(1=>"gif", 2=>"jpeg", 3=>"png");
return $type_array[$type];
}
// 创建图片文件资源
private function get_image_source($file){
$createimage = "imagecreatefrom".$this->file_type($file);
return $createimage($file);
}
}
|