PHP-GD库

<?php
    $position = 0;//如果为0表示任意位置,如果为1表示左上角如果为2表示右上脚,如果为3表示左下角,如果为4右下角,如果为5中间。
    //创建要加水印图片的资源
    $dImg = imagecreatefromjpeg('./4557409ea6c0c2483126d3f8a113369d.jpg');

    //水印图片的资源
    $wImg = imagecreatefrompng('./3.png');

    //获得水印图片的宽度和高度
    $wImgWidth = imagesx($wImg);
    $wImgHeight = imagesy($wImg);

    //获得要加水印图片的宽度和高度
    $dImgWidth = imagesx($dImg);
    $dImgHeight = imagesy($dImg);
    //判断位置
    if(!$position){//如果位置为0,那么随机
        $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
    //1、要处理的图片名字
    $fileName = './xxoo.jpg';

    //2、将要缩放的宽度和高度
    $newWidth = 200;
    $newHeight = 200;

    //3、获得原来图片的信息
    $oldInfo = getimagesize($fileName);
    $oldWidth = $oldInfo[0];//得到的老宽度
    $oldHeight = $oldInfo[1];//得到的老高度


    var_dump($oldInfo);

    //4、获得要缩放的图片的类型
    $types = array(1=>'gif',2=>'jpeg',3=>'png');

    //5、获得创建函数的字符串
    $imagecreate = 'imagecreatefrom'.$types[$oldInfo[2]];//$types[2]
    //6、获得缩放后保存的函数的字符串
    $image = 'image'.$types[$oldInfo[2]];


    //7、创建新图片资源
    $newImg = imagecreatetruecolor($newWidth,$newHeight);
    //8、获得老图片的资源
    $oldImg = $imagecreate($fileName);


    imagecopyresampled($newImg,$oldImg,0,0,0,0,$newWidth,$newHeight,$oldWidth,$oldHeight);

    //$newFileName = md5(date('YmdHis')).rand(1000,9999).'.'.$types[$oldInfo[2]];
    $sufFix = explode('.',$fileName);
    $sufFix = array_pop($sufFix);
    $newFileName = md5(date('YmdHis')).rand(1000,9999).'.'.$sufFix;
    //9、保存图像
    $image($newImg,'./'.$newFileName);

    imagedestroy($newImg);
    imagedestroy($oldImg);

    //程序不应该是写的多,而应该是写的巧。

?>
<!---------------------------------------------------------------------->
<?php
    header('content-type:image/jpeg');
    /*
     *1、生成一个随机背景图片(宽150,高50)  背景颜色随机的
     *2、画干扰像素点,像素点位置应该也是随机的。颜色也是随机的
     *3、写字,写什么字应该是随机出来的组合,字体的大小也是随机的。位置也是随机的,个数定在4个。
     */

    //1、创建画布
    $img = imagecreatetruecolor(150,50);

    //2、设置画布的颜色
    $bgColor = imagecolorallocate($img,rand(126,255),rand(126,255),rand(126,255));
    //3、填充画布
    imagefill($img,0,0,$bgColor);

    //6、画像素点
    for($i = 1; $i<= 500; $i++){
        $pixelX = rand(5,145);//随机的x
        $pixelY = rand(5,45);//随机的y
        $pixelColor = imagecolorallocate($img,rand(0,125),rand(0,125),rand(0,125));
        //画点
        imagesetpixel($img,$pixelX,$pixelY,$pixelColor);
    }

    /*加横线,加着玩的
    for($k = 1 ; $k <= 20 ; $k++){
        $lineColor = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
        imageline($img,rand(5,75),rand(5,45),rand(76,145),rand(5,45),$lineColor);
    }*/

    $code = '0123456789abcdefghijklmnopqrstuvwxyz';
    //7、还得往上面+字  4   ===》
    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)];

        //计算x和y的位置
        $fontX = (100 / 4) * $i;
        $fontY = 30;

        //echo "<h1>{$text}</h1>";
        imagettftext($img,$fontSize,0,$fontX,$fontY,$fontColor,'./arial.ttf',$text);
    }


    //4、输出画布
    imagejpeg($img);

    //5、销毁资源
    imagedestroy($img);
?>

<!-------------------------------------------------------------------------------->
<?php
    header('content-type:image/jpeg');

    $say = array(1=>'地球的外面是什么?','香飘飘奶茶','......');

    //1、有画布
    $img = imagecreatefromjpeg('./bf9700adcbef76096125c9b82cdda3cc7cd99e4a.jpg');

    //2、写字
    $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);
    }

    //3、输出或保存图像
    imagejpeg($img);
    //4、销毁画布
    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指的是相对于原来应该在的位置,向左上角进行偏移。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值