<?php
class
Image{
private
$path
;
public
function
__construct(
$path
){
$this
->path =
$path
;
}
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
);
}
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
);
}
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
);
}
}