Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26188279
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类:

2009-07-19 23:26:38

/*    参数列表,其中[原图路径][新图路径]一定要有,其它参数可使用默认,如:CreateSmallImage("1.jpg", "2.jpg");
     *    $OldImagePath        原图路径
     *    $NewImagePath        新图路径
     *    $WatermarkStr        水印文字
     *    $WriteInStringSize    水印文字,字体大小
     *    $NewWidth            新图宽度
     *    $NewHeight            新图高度
     *    $fontpath            水印字体路径
    */
    function CreateSmallImage( $OldImagePath, $NewImagePath, $WatermarkStr = "", $WriteInStringSize = 15, $NewWidth=250, $NewHeight=185, $fontpath="upload/STXINWEI.TTF" )
    {

        // 建立目录
        $dirArr = explode("/", $NewImagePath);
        $newPath = $dirArr[0].'/';
        foreach ($dirArr AS $dk => $dv)
        {
            if (($dk < count($dirArr) - 1) && $dk > 0)
            {
                $newPath .= $dv.'/';
                if (!is_dir($newPath)) mkdir($newPath, 0777); chmod($newPath, 0777);
            }
        }

        // 取出原图,获得图形信息getimagesize参数说明:0(宽),1(高),2(1gif/2jpg/3png),3(width="638" height="340"),后面还有点其它信息好像用处不大
        $OldImageInfo = getimagesize($OldImagePath);
        if ( $OldImageInfo[2] == 1 )        $OldImg = @imagecreatefromgif($OldImagePath);
        elseif ( $OldImageInfo[2] == 2 )    $OldImg = @imagecreatefromjpeg($OldImagePath);
        else                                $OldImg = @imagecreatefrompng($OldImagePath);

        // 创建图形,imagecreate参数说明:宽,高
        $NewImg = imagecreatetruecolor( $NewWidth, $NewHeight );

        // 给图形着背景色
        /* 暂时用不着 */

        // 创建色彩,参数:图形,red(0-255),green(0-255),blue(0-255)
        $black = ImageColorAllocate( $NewImg, 0, 0, 0 );        // 黑色
        $white = ImageColorAllocate( $NewImg, 255, 255, 255 );    // 白色
        $red   = ImageColorAllocate( $NewImg, 255, 0, 0 );        // 红色
        $blue  = ImageColorAllocate( $NewImg, 0, 0, 255 );        // 蓝色
        $other = ImageColorAllocate( $NewImg, 0, 255, 0 );        // 不知道叫什么

        // 新图形高宽处理
        $WriteNewWidth  = $NewHeight*$OldImageInfo[0] / $OldImageInfo[1];    // 要写入的高度
        $WriteNewHeight = $NewWidth*$OldImageInfo[1] / $OldImageInfo[0];    // 要写入的宽度
        if ( $WriteNewWidth <= $NewWidth )                                    // 以$NewHeight为基础,如果新宽小于或等于$NewWidth,则成立
        {
            $WriteNewWidth    = $WriteNewWidth;    // 用判断后的大小
            $WriteNewHeight = $NewHeight;        // 用规定的大小
            $WriteX            = floor( ($NewWidth-$WriteNewWidth) / 2 ); // 在新图片上写入的X位置计算
            $WriteY            = 0;
        } else {
            $WriteNewWidth    = $NewWidth;            // 用规定的大小
            $WriteNewHeight = $WriteNewHeight;        // 用判断后的大小
            $WriteX            = 0;
            $WriteY            = floor( ($NewHeight-$WriteNewHeight) / 2 ); // 在新图片上写入的X位置计算
        }

        // 旧图形缩小后,写入到新图形上(复制),imagecopyresized参数说明:新旧, 新xy旧xy, 新宽高旧宽高
        @imagecopyresampled( $NewImg, $OldImg, $WriteX, $WriteY, 0, 0, $WriteNewWidth, $WriteNewHeight, $OldImageInfo[0], $OldImageInfo[1] );

        if( $WatermarkStr != '' )
        {
            // 计算要写入的字符所占区域,ImageTTFBBox参数说明:字形的尺寸,字型的角度,字体路径,字符串内容
            if( $WriteInStringSize == '' )    $WriteInStringSize = 20;
            if( $WriteInStringSize < 5 )    $WriteInStringSize = 5;
            if( $WriteInStringSize > 72 )    $WriteInStringSize = 72;
            $WriteInStringArea    = ImageTTFBBox( $WriteInStringSize, 0, $fontpath, $WatermarkStr );
            while ( $WriteInStringArea[2] > $NewHeight )
            {
                $WriteInStringSize -= 5;
                $WriteInStringArea    = ImageTTFBBox( $WriteInStringSize, 0, $fontpath, $WatermarkStr );
            }
            $WriteInStringX        = $NewWidth - $WriteInStringArea[2] - 5;
            $WriteInStringY        = $NewHeight - $WriteInStringArea[3] - 5;

            // 转换水印编码
            $WatermarkStr = iconv( "gb2312", "UTF-8", $WatermarkStr );

            // 在新图形上写入文字,ImageTTFText参数说明:要写入的图形,字型尺寸,为字型的角度,x,y,颜色参数,字体路径,字符串内容
            ImageTTFText( $NewImg, $WriteInStringSize, 0, $WriteInStringX, $WriteInStringY, $other, $fontpath, $WatermarkStr );
        }

        // 保存文件
        @imagegif( $NewImg, $NewImagePath );

        // 结束图形
        @imagedestroy($NewImg);
    }
阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~