<?php
$position = 0;
$dImg = imagecreatefromjpeg('./4557409ea6c0c2483126d3f8a113369d.jpg');
$wImg = imagecreatefrompng('./3.png');
$wImgWidth = imagesx($wImg);
$wImgHeight = imagesy($wImg);
$dImgWidth = imagesx($dImg);
$dImgHeight = imagesy($dImg);
if(!$position){
$position = rand(1,5);
}
switch($position){
case 1:
$x = 0;
$y = 0;
break;
case 2:
$x = $dImgWidth - $wImgWidth;
$y = 0;
break;
case 3:
$x = 0;
$y = $dImgHeight - $wImgHeight;
break;
case 4:
$x = $dImgWidth - $wImgWidth;
$y = $dImgHeight - $wImgHeight;
break;
case 5:
$x = ($dImgWidth - $wImgWidth) / 2;
$y = ($dImgHeight - $wImgHeight) / 2;
break;
}
imagecopy($dImg,$wImg,$x,$y,0,0,$wImgWidth,$wImgHeight);
imagejpeg($dImg,'./xxooxxooni.jpg');
imagedestroy($dImg);
imagedestroy($wImg);
?>
<?php
$code = '0123456789';
echo $code[rand(0,strlen($code)-1)];
?>
<?php
$fileName = './xxoo.jpg';
$newWidth = 200;
$newHeight = 200;
$oldInfo = getimagesize($fileName);
$oldWidth = $oldInfo[0];
$oldHeight = $oldInfo[1];
var_dump($oldInfo);
$types = array(1=>'gif',2=>'jpeg',3=>'png');
$imagecreate = 'imagecreatefrom'.$types[$oldInfo[2]];
$image = 'image'.$types[$oldInfo[2]];
$newImg = imagecreatetruecolor($newWidth,$newHeight);
$oldImg = $imagecreate($fileName);
imagecopyresampled($newImg,$oldImg,0,0,0,0,$newWidth,$newHeight,$oldWidth,$oldHeight);
$sufFix = explode('.',$fileName);
$sufFix = array_pop($sufFix);
$newFileName = md5(date('YmdHis')).rand(1000,9999).'.'.$sufFix;
$image($newImg,'./'.$newFileName);
imagedestroy($newImg);
imagedestroy($oldImg);
?>
<?php
header('content-type:image/jpeg');
$img = imagecreatetruecolor(150,50);
$bgColor = imagecolorallocate($img,rand(126,255),rand(126,255),rand(126,255));
imagefill($img,0,0,$bgColor);
for($i = 1; $i<= 500; $i++){
$pixelX = rand(5,145);
$pixelY = rand(5,45);
$pixelColor = imagecolorallocate($img,rand(0,125),rand(0,125),rand(0,125));
imagesetpixel($img,$pixelX,$pixelY,$pixelColor);
}
$code = '0123456789abcdefghijklmnopqrstuvwxyz';
for($i = 1 ; $i <= 4 ; $i++){
$fontSize = rand(15,20);
$fontColor = imagecolorallocate($img,rand(0,125),rand(0,125),rand(0,125));
$text = $code[rand(0,strlen($code)-1)];
$fontX = (100 / 4) * $i;
$fontY = 30;
imagettftext($img,$fontSize,0,$fontX,$fontY,$fontColor,'./arial.ttf',$text);
}
imagejpeg($img);
imagedestroy($img);
?>
<?php
header('content-type:image/jpeg');
$say = array(1=>'地球的外面是什么?','香飘飘奶茶','......');
$img = imagecreatefromjpeg('./bf9700adcbef76096125c9b82cdda3cc7cd99e4a.jpg');
$imgHeight = imagesy($img) / 3;
$imgWidth = imagesx($img);
$color = imagecolorallocate($img,255,255,0);
foreach($say as $key=>$value){
imagettftext($img,20,0,5,($imgHeight*$key)-10,$color,'./SIMYOU.TTF',$value);
}
imagejpeg($img);
imagedestroy($img);
?>
<?php
var_dump(getimagesize('./bf9700adcbef76096125c9b82cdda3cc7cd99e4a.jpg'));
?>
使用图像背景
imagecreatefromjpeg();创建一个以jpeg图像为背景的资源
imagecreatefrompng();
imagecreatefromgif();
imagesx();获得图像的宽度
imagesy();获得图像的高度
getimagesize();返回一个图像的信息的数组
注意:
1、getimagesize不用传进来一个资源只要一个文件名就可以。
2、得到的数组中0下标得到的是宽度,1下标得到的是高度。
2下标得到的是类型
1=>gif
2=>jpeg
3=>png
图像缩放:
imagecopyresampled(新图片,老图片,新x,新y,老x,老y,新宽,新高,老宽,老高);
注意:
1、新y,缩放后的图像离画布的上边缘的距离。
2、新x,缩放的图像离画布的左边缘的距离。
3、老x,和老y决定了什么?将老的x和老y都离开了指定的像素之后,然后再进行缩放。右下角中和新画布右下角的距离。
等比例缩放:
if(新宽 && (旧宽 < 旧高)){
新宽 = (新高 / 旧高) * 旧宽
}else{
新高 = (新宽 /旧宽) * 旧高
}
注意:
1、等比例缩放时,肯定会更新的宽度或新的高度。
添加水印
bool imagecopy ( 要图, 水图, 要图x , 要图y , 水图x , 水图y , 水图宽 , 水图高 )
要图x,要图y指的是水印图片应该在要加水印图片的哪个位置。
水图x,水图y指的是相对于原来应该在的位置,向左上角进行偏移。