<?php
// 获取图片宽度、高度和类型
$img='abc.jpeg';
print_r(getImageInfo($img));
echo '<br/>';
function getImageInfo($img){
$info= getimagesize($img);
$imageinfo=pathinfo($img);
$getImageInfo=array(
'width' =>$info[0],
'height'=>$info[1],
'type'=>$imageinfo['extension'],
);
return $getImageInfo;
}
water('abc.jpeg','du.png');
function water($img,$water,$pos=9,$tm=100){
$info=getImageInfo($img);
$logo=getImageInfo($water);
$dst=openImg($img,$info['type']);
$src=openImg($water,$logo['type']);
switch($pos){
case 1:
$x=0;
$y=0;
break;
case 2:
$x=ceil(($info['width']-$logo['width'])/2);
$y=0;
break;
case 3:
$x=$info['width']-$logo['width'];
$y=0;
break;
case 4:
$x=0;
$y=ceil(($info['height']-$logo['height'])/2);
break;
case 5:
$x=ceil(($info['width']-$logo['width'])/2);
$y=ceil(($info['height']-$logo['height'])/2);
break;
case 6:
$x=$info['width']-$logo['width'];
$y=ceil(($info['height']-$logo['height'])/2);
break;
case 7:
$x=0;
$y=$info['height']-$logo['height'];
break;
case 8:
$x=ceil(($info['width']-$logo['width'])/2);
$y=$info['height']-$logo['height'];
break;
case 9:
$x=$info['width']-$logo['width'];
$y=$info['height']-$logo['height'];
break;
case 0:
default:
$x=mt_rand(0,$info['width']-$logo['width']);
$y=mt_rand(0,$y=$info['height']-$logo['height']);
break;
}
imagecopymerge($dst,$src,$x,$y,0,0,$logo['width'],$logo['height'],$tm);
header ('Content-Type:image/jpeg');
imagejpeg($dst);
imagedestory($dst);
imagedestory($src);
}
function openImg($path,$type){
switch($type){
case 'jpeg':
case 'jpg':
case 'pjpeg':
$img=imagecreatefromjpeg($path);
break;
case 'png':
case 'x-png':
$img=imagecreatefrompng($path);
break;
case 'gif':
$img=imagecreatefromgif($path);
break;
case 'wbmp':
$img=imagecreatefromwbmp($path);
break;
default:
exit('图片类型不支持');
}
return $img;
}
?>
#
如果圖片直接改後綴名,會導致這種情況,可透過PS修改後綴名,希望回答有所幫助。
//取得圖片寬度、高度與型別
// $img='time.jpg';
// print_r(getImageInfo($img ) );
// echo '
';
function getImageInfo($img){
$info= getimagesize($img);
$imageinfo=pathinfo($img);
$getImageInfo=array(
'width' =>$info[0],
# '' height'= >$info[1],
'type'=>$imageinfo['extension'],
);
return $getImageInfo;
# }
water('photo.jpeg','watermark.png');
函數水($img,$water,$pos=9,$tm= 100){
$info=getImageInfo($img);
$logo=getImageInfo($water);
$dst=openImg($img,$info[ 'type']) ;
$src=openImg($water,$logo['type']);
switch($pos){
情境1:
$x=0;
$y=0;
break ' width']-$logo['width'])/2);
$y=0;
break;
break;
: $x=$info['width']-$logo['width'];
$y=0;
## break;
break;
# case
$x=0;
$y=ceil(($info['height']-$logo['height'])/2);
# #
# 情況5:
$x=ceil(($info['width']-$logo['width'])/2);
# $ ($info['height']-$logo['height'])/2);
break;
情境6:
# _o= $ 'width']-$logo['width'];
$y=ceil(($info['height']-$logo['height'])/2);
# 中斷;
情境 7:
$x=0;
$x=0;
he $
# break;
情境8:
### $x=ceil(($info['width']-$logo['width'#)/'width'# 2 ### $y=$info['height']-$logo['height'];###### 中斷;
情境 9:
$x=$info['width']-$logo['width'];
']-$logo['height'];
Break;
case 0:
預設值:
# $ info['width']-$logo['width']);
$y=mt_rand(0,$y=$info['height']-$logo['height']);
中斷;
}
imagecopymerge($dst,$src,$x,$y,0,0,$logo['寬度'],$logo[ ' height'],$tm);
header ('Content-Type:image/jpeg');
imagejpeg($dst);
# imagedestory($dst ) ;
imagedestory($src);
}
function openImg($path,$type){
switch($type){
case 'jpeg':
# case 'jpg':
($path);
break;
# case 'png':
# case 'x-png'
# case 'x-png'
# #m $path);
break ;
case 'gif':
$img=imagecreatefromgif c case 'wbmp':
$img=imagecreatefromwbmp($path);
c exit('圖片類型不支援');
}
return $img;
}
?>
另外課程上的方法(親測可用),不同的getImageInfo函數。
<?php
header("內容類型:text/html;charset=utf-8");
water('photo.jpeg','watermark.jpeg');
function getImageInfo($path) {
$info = getimagesize($path);
$data['width'] = $info[0];
$data['height'] = $info[1];
$data['type'] = $info['mime'];
return $data;
}
function openImg($path,$type){
switch($type){
case 'image/jpeg':##o 1 ase case 'image/jpeg':## 1 ase 2 ase ' 'image/pjpeg':
$img=imagecreatefromjpeg($path);
break;
ase 'image/x-png':
$img =imagecreatefrompng($path);
break;
case 'image/gif':
break;
case 'image/wbmp':
$img=imagecreatefromwbmp($path);
break;
;
}
return $img;
}
//$water = 'watermark.jpeg';
#function water($img,$water,$pos=9,$tm=100){
$info=getImageInfo($img ) ;
$logo=getImageInfo($water);
$dst=openImg($img,$info['type']);
$src=openImg($water,$logo['type ') ]);
switch($pos){
情境1:
$x=0;
$ $x =ceil(($info['width']-$logo['width'])/2);
$y=0;
# =$ info['width']-$logo['width'];
$y=0;
break;
案例4: break;
$y= ceil( ($info['height']-$logo['height'])/2);
break;
情境 5:
$x=ceil(($info['width']-$logo['width'])/2);
$y=ceil(($info['height']-$logo['height'] )/2);
break;
# 情境6:
$x=$info['width']-$logo['width'] info $x=$ 'height']-$logo['height'])/2);
Break;
情境7:
$x=0;## $logo['height'];
break;
case 8:
$x=ceil(($info['width']-$logo['wid)' # $y=$info['height']-$logo['height'];
break;
案例為 9:##)$50 $ '];
$y=$info['height']-$logo['height'];
break;
( 0 ,$info['width']-$logo['width']);
$y=mt_rand(0,$y=$info['height']-$logo['height']);
break;
}
imagecopymerge($dst,$src,$x,$y,0,0,$logo['width'],$logo['height'],$tm);
header ('Content-Type:image/jpeg');
imagejpeg($dst);
imagedestory($dst);
# imagedestory($src);
#? > ;