您的位置:首页 > 编程语言 > PHP开发

php图片操作

2016-08-26 16:38 232 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">源于猪八戒网上的一次招标,前前后后,学到了不少东西。自己简单整理下。</span>



在图片上写文字

function Xiezi($tt,$width,$height){
$image = ImageCreateFromJPEG( "test.jpg" );
$cor = imagecolorallocate($image, 0, 0, 0);
$font = 'xihei.ttf';
//$tt = '我们的灵魂';
//imagepsslantfont($font, 22.5);
$a = im($image, 12, 0, $width, $height, $cor, $font, $tt,3);
//header('Content-type: image/jpeg');
imagejpeg($image,"test.jpg");
}
function im(&$image, $size, $angle, $start_x, $y, $color, $font, $text,$spancing) {

for ($i=0;$i<mb_strlen($text,'utf8');$i++) {
$t = mb_substr($text, $i,1,'utf8');
$x = $i*($size+$spancing);
imagettftext($image, $size, $angle, $x+$start_x, $y, $color, $font, $t);
}

}


图片的缩放操作

//图片缩放
function thumb($filename,$width=200,$height=200){
list($width_org,$height_org)=getimagesize($filename);
/*if($width && ($width_org < $height_org)){
$width=($height/$height_org)*$width_org;
}
else{
$height=($width/$width_org)*$height_org;
}*/
//如果上面代码没被注释 那么是等比例缩放
$image_p=imagecreatetruecolor($width, $height);
$image=imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_org, $height_org);
imagejpeg($image_p,$filename,100);

imagedestroy($image_p);
imagedestroy($image);
}



加水印函数

//加水印的函数
function watermark($filename,$water){
list($b_w,$b_h)=getimagesize($filename);
list($w_w,$w_h)=getimagesize($water);
$posX=rand(0,($b_w-$w_w));
$posY=rand(0,($b_h-$b_h));
$back=imagecreatefromjpeg($filename);
$water=imagecreatefromjpeg($water);
imagecopy($back, $water, 519, 111, 0, 0, $w_w, $w_h);
imagejpeg($back,"test.jpg");
imagedestroy($back);
imagedestroy($water);
}



生成随机字符串

//生成随机字符串
function getRandChar($length){
$str = null;
$strPol = "0123456789";
$max = strlen($strPol)-1;

for($i=0;$i<$length;$i++){
$str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
}

return $str;
}



字符串逐字拆分函数

function arr_split_zh($tempaddtext){
$tempaddtext = iconv("UTF-8", "gb2312", $tempaddtext);
$cind = 0;
$arr_cont=array();
for($i=0;$i<strlen($tempaddtext);$i++)
{
if(strlen(substr($tempaddtext,$cind,1)) > 0){
if(ord(substr($tempaddtext,$cind,1)) < 0xA1 ){ //如果为英文则取1个字节
array_push($arr_cont,substr($tempaddtext,$cind,1));
$cind++;
}else{
array_push($arr_cont,substr($tempaddtext,$cind,2));
$cind+=2;
}
}
}
foreach ($arr_cont as &$row)
{
$row=iconv("gb2312","UTF-8",$row);
}
return $arr_cont;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 图片