网站做好了,可客户总是罗唆这个问题,就是小图显示没水印,大图显示有水印.真不知道后台该怎么写,没理由叫客户上传两次一次小图一次大图吧?我想就是上传一次能生成小图一张,大图水印一张.
有没有其它更好的方法呢?

解决方案 »

  1.   

    getimagesize可以得到图片的大小,不过好像要把原图上传以后才行。
    我有一个方法,不过比较麻烦。
    先上传原图,假设文件名为a.jpg
    然后生成小图,文件名为s_a.jpg
    根据a.jpg生成一张新的图片并加上水印,aa.jpg,删除a.jpg
    把aa.jpg更名为a.jpg我不知道能不能根据上传时的临时文件即['tmp_name']生成缩略图和加上水印,没试过,如果能,那就简单了。
      

  2.   

    直接用 ['tmp_name' ] 生成小图跟大图. 只要生成大图时才加水印不就行了吗?
      

  3.   

    $picsL_name = $_FILES["picsL"]["tmp_name"];
    $ori = $_FILES["picsL"]["name"];
    move_uploaded_file( $picsL_name, $path . $picsL ) or die ( "error!" );
    $picsL = resizeimg( $path, $path, $ori, $lang[width1], $lang[height1] );
    $picsS = resizeimg( $path, $paths, $ori, $lang[width2], $lang[height2] );
    /*
    resizeimg
    有水印什么的功能 但是还不完善 自己修改看看吧
    */
    /**
     * 等比例调整图片大小
     * 
     * @param  $ 原始路径 $path_from
     * @param  $ 存放路径 $path_to
     * @param  $ 原有文件名 $name
     * @param  $ 新文件名 $outname
     * @param  $ 新宽度 $width
     * @param  $ 新高度 $height
     * @return string 新文件名
     */
    function resizeimg( $path_from, $path_to, $name , $outname, $width, $height ) {
        if ( !$path_from or !$path_to or !$name or !$outname or !$width or !$height ) {
            return false;
        } 
        // 新文件名
        $outputfile = $path_to . $outname;
        if ( is_file ( $outputfile ) ) {
            return false;
        } 
        // 指定jpg图片压缩率
        $compress = "90"; 
        // 水印开关
        $water = 0; //0 无水印 1 文字水印 2 图片水印 3 所有水印           
        // $waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
        $waterstring = ""; //水印字符串
        $font = 'simsun.ttc'; //文字字体
        $size = 10; //文字水印大小
        $waterimg = ""; //水印图片
        $alpha = 40;    $file_orig = $name; 
        // 获得原始文件名和分辨率
        $pic_url_orig = $path_from . $file_orig;
        list( $width_orig, $height_orig ) = getimagesize( $pic_url_orig ); 
        // 计算新分辨率
        if ( $width_orig > $width ) {
            $height = ( $width * $height_orig ) / $width_orig;
        } else {
            $width = $width_orig;
            $height = $height_orig;
        } 
        // 缩放
        $image_p = imagecreatetruecolor( $width, $height );
        $type = getimagesize ( $pic_url_orig );
        switch ( $type[2] ) {
            case 1:
                $simage = imagecreatefromgif( $pic_url_orig );
                break;
            case 2:
                $simage = imagecreatefromjpeg( $pic_url_orig );
                break;
            case 3:
                $simage = imagecreatefrompng( $pic_url_orig );
                break;
            case 6:
                $simage = imagecreatefromwbmp( $pic_url_orig );
                break;
            default:
                $simage = imagecreatefromjpeg( $pic_url_orig );
        } 
        imagecopyresampled( $image_p, $simage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig );    if ( $water == 1 or $water == 3 ) {
            // 指定颜色
            $black = imagecolorallocate( $image_p, 0, 0, 0 );
            $grey = imagecolorallocate( $image_p, 192, 192, 192 );
            $white = imagecolorexactalpha ( $image_p, 255, 255, 255, 30 );
            $waterstring = $lang[waterstring];
            $waterimg = $lang[waterimg]; 
            // 填充背景色块 左上为原点 左上 至 右下
            imagefilledrectangle( $image_p, 4, $height - $size - 7, 4 + $size * strlen( $waterstring ) / 1.5 , $height - 3, $white ); 
            // 加水印字符串
            // 添加阴影 左上为原点 字符的基线左下角坐标
            // imagettftext( $image_p, $size, 0, 6, $height-4, $grey, $font, iconv("gb2312","utf-8", $waterstring ) );
            imagettftext( $image_p, $size, 0, 6, $height-4, $grey, $font, mb_convert_encoding( $waterstring, "UTF-8", "GB2312" ) );
            imagettftext( $image_p, $size, 0, 5, $height-5, $black, $font, mb_convert_encoding( $waterstring, "UTF-8", "GB2312" ) );
        } 
        if ( $water == 2 or $water == 3 ) {
            $typewater = getimagesize ( $waterimg ); 
            // 加水印图片
            switch ( $typewater[2] ) {
                case 1:
                    $simage1 = imagecreatefromgif( $waterimg );
                    break;
                case 2:
                    $simage1 = imagecreatefromjpeg( $waterimg );
                    break;
                case 3:
                    $simage1 = imagecreatefrompng( $waterimg );
                    break;
                case 6:
                    $simage1 = imagecreatefromwbmp( $waterimg );
                    break;
                default:
                    $simage1 = imagecreatefromjpeg( $waterimg );
            } 
            imagecopymerge( $image_p, $simage1, 0, 0, 0, 0, $typewater[0], $typewater[1] , $alpha );
            imagedestroy( $simage1 );
        } 
        // 输出
        switch ( $type[2] ) {
            case 1:
                imagegif( $image_p, $outputfile );
                break;
            case 2:
                imagejpeg( $image_p, $outputfile , $compress );
                break;
            case 3:
                imagepng( $image_p, $outputfile );
                break;
            case 6:
                imagewbmp( $image_p, $outputfile );
                break;
            default:
                imagejpeg( $image_p, $outputfile , $compress );
        } 
        return $outname;
    }