当前位置:编程学习 > php >>

编程PHP生成圆角的缩略图,适合头像和相册图片

<?php
/**
* 把图片缩略,并生成圆角
*/

function roundCorner($im) {
    if(!$im) {
        return;
    }
    //取得当前图片大小
    $width = imagesx($im);
    $height = imagesy($im);

    $lefttop = imagecreatefromgif('lefttop.gif');
    $leftbottom = imagecreatefromgif('leftbottom.gif');
    $righttop = imagecreatefromgif('righttop.gif');
    $rightbottom = imagecreatefromgif('rightbottom.gif');

    // four corner have the same size
    $cornerx = imagesx($lefttop);
    $cornery = imagesy($lefttop);

    imagecopymerge($im, $lefttop, 0, 0, 0, 0, $cornerx, $cornery, 100);
    imagecopymerge($im, $leftbottom, 0, $height-$cornery, 0, 0, $cornerx, $cornery, 100);
    imagecopymerge($im, $righttop, $width-$cornerx, 0, 0, 0, $cornerx, $cornery, 100);
    imagecopymerge($im, $rightbottom, $width-$cornerx, $height-$cornery, 0, 0, $cornerx, $cornery, 100);

    return $im;
}
function thumbImage($im) {
    if(!$im) {
        return;
    }
    $maxwidth = 180;
    $maxheight = 180;
    //取得当前图片大小
    $width = imagesx($im);
    $height = imagesy($im);
    //生成缩略图的大小
    if(($width > $maxwidth) || ($height > $maxheight)){
        $widthratio = $maxwidth/$width;
        $heightratio = $maxheight/$height;
        if($widthratio < $heightratio){
            $ratio = $widthratio;
        }else{
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;

        if(function_exists("imagecopyresampled")){
            $newim = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }else{
            $newim = imagecreate($newwidth, $newheight);
            imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        return $newim;
    }else{
        return $im;
    }
}

function thumbRoundCorner($path, $newpath=null) {
    if(!($info = getimagesize($path))) {
        return;
    }

    if($info['mime'] == "image/pjpeg" || $info['mime'] == "image/jpg" || $info['mime'] == "image/jpeg"){
        $im = imagecreatefromjpeg($path);
    }elseif($info['mime'] == "image/png" || $info['mime'] == "image/x-png"){
        $im = imagecreatefrompng($path);
    }elseif($info['mime'] == "image/gif"){
        $im = imagecreatefromgif($path);
    }
    if($im){
        if(!empty($newpath)) {
            $newpath .= '.jpg';
        }
        if(file_exists($newpath)){
            unlink($newpath);
        }
        $im = thumbImage($im);
        $im = roundCorner($im);
        if($im) {
            if(!empty($newpath)){
                ImageJpeg ($im, $newpath);
            }else{
                header("Content-type: image/jpeg");
                ImageJpeg($im);
            }
        }
        ImageDestroy ($im);
    }
}
?>
<?php
if (!empty($_FILES['pic'])){
    $path = $_FILES['pic']['tmp_name'];
    thumbRoundCorner($path);
}else{
?>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<h2><center>PHP生成圆角的缩略图,适合头像和相册</center></h2>
<P><center>by www.zzzyk.com</center></P>
<div>
<img src="../d1.jpg" /> 转换后 <img src="../d2.jpg" />
</div>
<br />
<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="pic" /> <input type="submit" />
</form>
<?php
    exit;
}?>
 

演过测试:带圆角边框的图片头像在线制作http://www.zhaoxi.org/h/20.htm

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,