/*
图像切割&放大函数 参数说明:
$src_img 操作图像地址
$dst_w 目标图像宽度
$dst_h 目标图像高度
$text 图像文字
$horz 文字水平对齐方式[可选值:left,center,right]
$vert 文字先垂直对齐方式[可选值:top,middle,bottom]
$text_color 文字颜色[可选值:black,white,bule,red,yellow,green,cyan,purple]
$path 图像保存路径 返回值:
*/
function Imgerebulider($src_img,$dst_w,$dst_h,$text,$horz,$vert,$text_color,$path){
$src_size=GetImageSize($src_img);
$src_w=$src_size[0]; //取出图像的高
$src_h=$src_size[1]; //取出图像的宽
$src_type=$src_size[2]; //取出图像的类型 //图像类型判断//
switch ($src_type){
case 1:
$img = ImageCreateFromGif($src_img);
$ex_name="gif";
break;
case 2:
$img = ImageCreateFromJpeg($src_img);
$ex_name="jpg";
break;
case 3:
$img = ImageCreateFromPng($src_img);
$ex_name="png";
break;
} $newimg=imageCreateTrueColor($dst_w,$dst_h);//建立新图像 if ($dst_w<=$src_w && $dst_h<=$src_h){ //图像放大,切割判断 //取出切割图像的X,Y坐标//
$scaleX_star=floor(($src_w-$dst_w)/2);
$scaleX_end=$src_w-$scaleX_star;
$scaleY_star=floor(($src_h-$dst_h)/2); $scaleY_end=$src_h-$scaleY_star;
for($x=$scaleX_star;$x<$scaleX_end;$x++){
for($y=$scaleY_star;$y<$scaleY_end;$y++){ //取出X,Y坐标对像的颜色索引值并对新图描点//
$color=ImageColorsForIndex($img,ImageColorAt($img,$x,$y));
//echo "$color[red].$color[green].$color[blue]<br>";
ImageSetPixel ($newimg,$x-$scaleX_star,$y-$scaleY_star,ImageColorClosest ($newimg,$color[red],$color[green],$color[blue])); };
};
}else{
ImageCopyResized($newimg,$img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h); //调速图像大小
} //图像输出文字//
if ($text!=""){
$text_length=strlen($text);
//设定文字在图像上输出的X坐标//
switch ($horz){
case "left":
$textScaleX=1;
break;
case "center":
$textScaleX=floor(($dst_w-$text_length)/2)-20;
break;
case "right":
$textScaleX=$dst_w-$text_length-$text_length*6;
break;
default:
$textScaleX=$dst_w-$text_length-$text_length*6;
break;
};
//设定文字在图像上输出的Y坐标//
switch ($vert){
case "top":
$textScaleY=1;
break;
case "middle":
$textScaleY=floor($dst_h/2);
break;
case "buttom":
$textScaleY=$dst_h-15;
break;
default:
$textScaleY=$dst_h-15;
break;
}; //字符串颜色设置//
switch($text_color){
case "black":
$text_color =ImageColorAllocate($newimg, 0,0,0);
break;
case "white":
$text_color =ImageColorAllocate($newimg, 255,255,255);
break;
case "yellow":
$text_color =ImageColorAllocate($newimg, 251,247,9);
break;
case "bule":
$text_color =ImageColorAllocate($newimg, 27,99,246);
break;
case "red":
$text_color =ImageColorAllocate($newimg, 250,22,43);
break;
case "green":
$text_color =ImageColorAllocate($newimg, 0,163,55);
break;
case "cyan":
$text_color =ImageColorAllocate($newimg, 41,241,163);
break;
case "purple":
$text_color =ImageColorAllocate($newimg, 218,27,246);
break;
default:
$text_color=ImageColorAllocate($newimg, 0,0,0);
break;
} //往图像写入字符串//
imagestring($newimg,3,$textScaleX, $textScaleY, $text, $text_color);
}; //输出图像名设定//
$filename="S".date(YmdHis).".".$ex_name;
//生成图像并保存//
switch ($ex_name){
case "gif":
ImageJPEG($newimg,$path.$filename,100);
break;
case "jpg":
ImageJPEG($newimg,$path.$filename,100);
break;
case "png":
ImagePNG($newimg,$path.$filename,100);
break;
} //释放内存//
imagedestroy($img); return $filename;
}
图像切割&放大函数 参数说明:
$src_img 操作图像地址
$dst_w 目标图像宽度
$dst_h 目标图像高度
$text 图像文字
$horz 文字水平对齐方式[可选值:left,center,right]
$vert 文字先垂直对齐方式[可选值:top,middle,bottom]
$text_color 文字颜色[可选值:black,white,bule,red,yellow,green,cyan,purple]
$path 图像保存路径 返回值:
*/
function Imgerebulider($src_img,$dst_w,$dst_h,$text,$horz,$vert,$text_color,$path){
$src_size=GetImageSize($src_img);
$src_w=$src_size[0]; //取出图像的高
$src_h=$src_size[1]; //取出图像的宽
$src_type=$src_size[2]; //取出图像的类型 //图像类型判断//
switch ($src_type){
case 1:
$img = ImageCreateFromGif($src_img);
$ex_name="gif";
break;
case 2:
$img = ImageCreateFromJpeg($src_img);
$ex_name="jpg";
break;
case 3:
$img = ImageCreateFromPng($src_img);
$ex_name="png";
break;
} $newimg=imageCreateTrueColor($dst_w,$dst_h);//建立新图像 if ($dst_w<=$src_w && $dst_h<=$src_h){ //图像放大,切割判断 //取出切割图像的X,Y坐标//
$scaleX_star=floor(($src_w-$dst_w)/2);
$scaleX_end=$src_w-$scaleX_star;
$scaleY_star=floor(($src_h-$dst_h)/2); $scaleY_end=$src_h-$scaleY_star;
for($x=$scaleX_star;$x<$scaleX_end;$x++){
for($y=$scaleY_star;$y<$scaleY_end;$y++){ //取出X,Y坐标对像的颜色索引值并对新图描点//
$color=ImageColorsForIndex($img,ImageColorAt($img,$x,$y));
//echo "$color[red].$color[green].$color[blue]<br>";
ImageSetPixel ($newimg,$x-$scaleX_star,$y-$scaleY_star,ImageColorClosest ($newimg,$color[red],$color[green],$color[blue])); };
};
}else{
ImageCopyResized($newimg,$img,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h); //调速图像大小
} //图像输出文字//
if ($text!=""){
$text_length=strlen($text);
//设定文字在图像上输出的X坐标//
switch ($horz){
case "left":
$textScaleX=1;
break;
case "center":
$textScaleX=floor(($dst_w-$text_length)/2)-20;
break;
case "right":
$textScaleX=$dst_w-$text_length-$text_length*6;
break;
default:
$textScaleX=$dst_w-$text_length-$text_length*6;
break;
};
//设定文字在图像上输出的Y坐标//
switch ($vert){
case "top":
$textScaleY=1;
break;
case "middle":
$textScaleY=floor($dst_h/2);
break;
case "buttom":
$textScaleY=$dst_h-15;
break;
default:
$textScaleY=$dst_h-15;
break;
}; //字符串颜色设置//
switch($text_color){
case "black":
$text_color =ImageColorAllocate($newimg, 0,0,0);
break;
case "white":
$text_color =ImageColorAllocate($newimg, 255,255,255);
break;
case "yellow":
$text_color =ImageColorAllocate($newimg, 251,247,9);
break;
case "bule":
$text_color =ImageColorAllocate($newimg, 27,99,246);
break;
case "red":
$text_color =ImageColorAllocate($newimg, 250,22,43);
break;
case "green":
$text_color =ImageColorAllocate($newimg, 0,163,55);
break;
case "cyan":
$text_color =ImageColorAllocate($newimg, 41,241,163);
break;
case "purple":
$text_color =ImageColorAllocate($newimg, 218,27,246);
break;
default:
$text_color=ImageColorAllocate($newimg, 0,0,0);
break;
} //往图像写入字符串//
imagestring($newimg,3,$textScaleX, $textScaleY, $text, $text_color);
}; //输出图像名设定//
$filename="S".date(YmdHis).".".$ex_name;
//生成图像并保存//
switch ($ex_name){
case "gif":
ImageJPEG($newimg,$path.$filename,100);
break;
case "jpg":
ImageJPEG($newimg,$path.$filename,100);
break;
case "png":
ImagePNG($newimg,$path.$filename,100);
break;
} //释放内存//
imagedestroy($img); return $filename;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货