• 2008-03-26

    php版图片加边框和水印 - [技术空间]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/lily64-logs/17724882.html

    为了便于博客图片批量处理,写了一段图片加边框和水印的代码,挺好用。存档。
    (lily注:更新版见http://www.blogbus.com/lily64-logs/24618771.html
    <form action=read.php method=post>
    图片缩小为:<select name=picsize><option value=0.25>25%</option><option value=0.5 selected>50%</option></select>
    边框颜色:<select name=bordercolor><option value=white>white</option><option value=black>black</option><option value=gray>gray</option></select>
    边框宽度:<select name=bordersize><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option><option value=6>6</option></select>
    签名颜色:<select name=inkcolor><option value=white>white</option><option value=black>black</option><option value=gray>gray</option></select>
    签名内容:<input type=text name=ink>
    <input type=submit value=开始转换>
    </form>

    read.php
    <?php    

    $picsize=$_POST["picsize"];
    $bordercolor=$_POST["bordercolor"];
    $bordersize=$_POST["bordersize"];
    $inkcolor=$_POST["inkcolor"];
    $ink=$_POST["ink"];

    if($handle = opendir('./img')) {    

    while (false !== ($file = readdir($handle))) {    

    if(strpos($file,".jpg")!==false||strpos($file,".JPG")!==false)
    {
    echo "$file"."已转换<br>";    

    $image = imagecreatefromjpeg('./img/'.$file);
    $width = ImageSx($image);
    $height = ImageSy($image);
    $x = $width*$picsize;
    $y = $height*$picsize;
    $dst = ImageCreateTrueColor($x,$y);

    Imagecopyresampled($dst,$image,0,0,0,0,$x,$y,$width,$height);

    $white = ImageColorAllocate($dst,0xFF,0xFF,0xFF);
    $black = ImageColorAllocate($dst,0x00,0x00,0x00);
    $gray = ImageColorAllocate($dst,0x88,0x88,0x88);

    for($i=0;$i<$bordersize;$i++)
    ImageRectangle($dst,$i,$i,$x-$i,$y-$i,$$bordercolor);  

    ImageTTFText($dst,20,0,$x-strlen($ink)*10-10,$y-30,$$inkcolor,'Mistral',$ink);

    imagejpeg($dst,'./img2/'.$file);

    }

    }    

    closedir($handle);    

    }    
    ?>

    分享到: