Chinaunix首页 | 论坛 | 博客
  • 博客访问: 547330
  • 博文数量: 107
  • 博客积分: 5229
  • 博客等级: 大校
  • 技术积分: 1158
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-08 10:50
文章分类
文章存档

2015年(1)

2014年(1)

2012年(5)

2011年(22)

2010年(12)

2009年(13)

2008年(34)

2007年(19)

我的朋友

分类:

2011-09-21 16:04:29

此代码支持png jpeg png图片 在代码运行时可以增加判断

function overlayjpg($imgsrc,$imgdst,$width,$height="")
{
//$imgsrc jpg格式图像路径 $imgdst jpg格式图像保存文件名 $imgwidth要改变的宽度 $imgheight要改变的高度
//取得图片的宽度,高度值
$arr = getimagesize($imgsrc);
//计算图片X轴位置
$img_X = ($width - $arr[0])/2;
if($height == ""){
    $heights = $arr[1];
    $img_Y = 0;
}
else{
    if($height <= $arr[1]){
        $heights = $arr[1];
        $img_Y = 0;
    }
    else{
        $heights = $height;
        $img_Y = ($height - $arr[1])/2;
    }
}

//$w = $arr[0];
//$h = $arr[1];
// Create image and define colors
$image = imagecreatetruecolor($width,$heights); //创建一个彩色的底图
$bg = imagecolorallocate($image, 255, 255, 255);
imagefill($image,0,0,$bg);
$imgsrc = LoadIMG($imgsrc,$arr['mime']);
imagecopy($image,$imgsrc,$img_X,$img_Y,0,0,$arr[0],$arr[1]);
imagejpeg($image,$imgdst,90);
//imagedestroy($image);
}

function LoadIMG($imgname,$mime)
{
    if($mime == "image/gif"){
        $im = @imagecreatefromgif($imgname); /* Attempt to open */
    }
    elseif ($mime == "image/png"){
        $im = @imagecreatefrompng($imgname); /* Attempt to open */
    }
    else{
        $im = @imagecreatefromjpeg($imgname); /* Attempt to open */
    }
    if(!$im) { /* See if it failed */
        $im = imagecreatetruecolor(150, 30); /* Create a blank image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}


阅读(2356) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~