php教程 php手册 php生成水印,包括位置和透明度

php生成水印,包括位置和透明度

Jun 13, 2016 am 10:45 AM
php 한 번 기본 위치 추가하다 그리고 그림 추천하다 양수표 생성하다 ~의 수확고 투명도

 

/*

给大家推荐一下!主要包括图片的裁剪,缩放,加水印等。下面是一些简单的使用方法。

include("imageclass.php");   //这里注意imageclass.php的路径

$imgs=new image();

$imgs->param($tempFile);

if(!$imgs->thumb($targetFile,600,500))die('--图像上传失败!');  //对图片进行缩放

 

$imgs->water($targetFile,$water,$pos=0,$pct=50);主要包括两个参数,$targetFile为已经上传到服务器的文件路径,$water为水印图片,$pos为水印图片位置,$pct为透明度;

$pos位置说明:

0://随机

1://1为顶端居左

2://2为顶端居中

3://3为顶端居右

4://4为中部居左

5://5为中部居中

6://6为中部居右

7://7为底端居左

8://8为底端居中

9://9为底端居右

 

 

将//里边的内容保存在imageclass.php里边,就可以调用了。

///////////////////////////////////////////////////////////////

*/

?>

/* +-------------------------------------------------------------+

* | Copyright (c) 2008-2009 Diqiye.Com All rights reserved.

* +-------------------------------------------------------------+

* | Info : 图像处理类

* +-------------------------------------------------------------+

*/

class image {

        // 当前图片

        protected $img;

        // 图像types 对应表

        protected $types = array(

                        1 => 'gif',

                        2 => 'jpg',

                        3 => 'png',

                        6 => 'bmp'

                    );

 

        // image

        public function __construct($img=''){

                !$img && $this->param($img);

        }

 

        // Info

        public function param($img){

                $this->img = $img;

                return $this;

        }

 

        // imageInfo

        public function getImageInfo($img){

                $info = @getimagesize($img);

                if(isset($this->types[$info[2]])){

                        $info['ext'] = $info['type'] = $this->types[$info[2]];

                } else{

                        $info['ext'] = $info['type'] = 'jpg';

                }

                $info['type'] == 'jpg' && $info['type'] = 'jpeg';

                $info['size'] = @filesize($img);

                return $info;

        }

 

        // thumb(新图地址, 宽, 高, 裁剪, 允许放大)

        public function thumb($filename,$new_w=160,$new_h=120,$cut=0,$big=0){

       

        // 获取原图信息

        $info  = $this->getImageInfo($this->img);

        if(!empty($info[0])) {

            $old_w  = $info[0];

            $old_h  = $info[1];

            $type   = $info['type'];

            $ext    = $info['ext'];

            unset($info);

            // 如果原图比缩略图小 并且不允许放大

            if($old_w

                    return false;

            }

            // 裁剪图片

            if($cut == 0){ // 等比列

                    $scale = min($new_w/$old_w, $new_h/$old_h); // 计算缩放比例

                    $width  = (int)($old_w*$scale); // 缩略图尺寸

                    $height = (int)($old_h*$scale);

                    $start_w = $start_h = 0;

                    $end_w = $old_w;

                    $end_h = $old_h;

            } elseif($cut == 1){ // center center 裁剪

                        $scale1 = round($new_w/$new_h,2);

                        $scale2 = round($old_w/$old_h,2);

                        if($scale1 > $scale2){

                                $end_h = round($old_w/$scale1,2);

                                $start_h = ($old_h-$end_h)/2;

                                $start_w  = 0;

                                $end_w    = $old_w;

                        } else{

                                $end_w  = round($old_h*$scale1,2);

                                $start_w  = ($old_w-$end_w)/2;

                                $start_h = 0;

                                $end_h   = $old_h;

                        }

                        $width = $new_w;

                    $height= $new_h;

                } elseif($cut == 2){ // left top 裁剪

                        $scale1 = round($new_w/$new_h,2);

                    $scale2 = round($old_w/$old_h,2);

                    if($scale1 > $scale2){

                            $end_h = round($old_w/$scale1,2);

                            $end_w = $old_w;

                    } else{

                            $end_w = round($old_h*$scale1,2);

                            $end_h = $old_h;

                    }

                    $start_w = 0;

                    $start_h = 0;

                    $width = $new_w;

                    $height= $new_h;

                }

            // 载入原图

            $createFun  = 'ImageCreateFrom'.$type;

            $oldimg     = $createFun($this->img);

            // 创建缩略图

            if($type!='gif' && function_exists('imagecreatetruecolor')){

                $newimg = imagecreatetruecolor($width, $height);

            } else{

                $newimg = imagecreate($width, $height);

            }

            // 复制图片

            if(function_exists("ImageCopyResampled")){

                    ImageCopyResampled($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);

            } else{

                ImageCopyResized($newimg, $oldimg, 0, 0, $start_w, $start_h, $width, $height, $end_w,$end_h);

            }

 

            // 对jpeg图形设置隔行扫描

            $type == 'jpeg' && imageinterlace($newimg,1);

            // 生成图片

            $imageFun = 'image'.$type;

            !@$imageFun($newimg,$filename,100) && die('保存失败!检查目录是否存在并且可写?');

            ImageDestroy($newimg);

            ImageDestroy($oldimg);

            return $filename;

        }

        return false;

    }

 

    // water(保存地址,水印图片,水印位置,透明度)

        public function water($filename,$water,$pos=0,$pct=50){

                // 加载水印图片

                $info = $this->getImageInfo($water);

                if(!empty($info[0])){

                        $water_w = $info[0];

                        $water_h = $info[1];

                        $type = $info['type'];

                        $fun  = 'imagecreatefrom'.$type;

                        $waterimg = $fun($water);

                } else{

                        return false;

                }

                // 加载背景图片

                $info = $this->getImageInfo($this->img);

                if(!empty($info[0])){

                        $old_w = $info[0];

                        $old_h = $info[1];

                        $type  = $info['type'];

                        $fun   = 'imagecreatefrom'.$type;

                        $oldimg = $fun($this->img);

                } else{

                        return false;

                }

                // 剪切水印

                $water_w >$old_w && $water_w = $old_w;

                $water_h >$old_h && $water_h = $old_h;

 

                // 水印位置

                switch($pos){

                    case 0://随机

                    $posX = rand(0,($old_w - $water_w));

                    $posY = rand(0,($old_h - $water_h));

                    break;

                case 1://1为顶端居左

                    $posX = 0;

                    $posY = 0;

                    break;

                case 2://2为顶端居中

                    $posX = ($old_w - $water_w) / 2;

                    $posY = 0;

                    break;

                case 3://3为顶端居右

                    $posX = $old_w - $water_w;

                    $posY = 0;

                    break;

                case 4://4为中部居左

                    $posX = 0;

                    $posY = ($old_h - $water_h) / 2;

                    break;

                case 5://5为中部居中

                    $posX = ($old_w - $water_w) / 2;

                    $posY = ($old_h - $water_h) / 2;

                    break;

                case 6://6为中部居右

                    $posX = $old_w - $water_w;

                    $posY = ($old_h - $water_h) / 2;

                    break;

                case 7://7为底端居左

                    $posX = 0;

                    $posY = $old_h - $water_h;

                    break;

                case 8://8为底端居中

                    $posX = ($old_w - $water_w) / 2;

                    $posY = $old_h - $water_h;

                    break;

                case 9://9为底端居右

                    $posX = $old_w - $water_w;

                    $posY = $old_h - $water_h;

                    break;

                default: //随机

                    $posX = rand(0,($old_w - $water_w));

                    $posY = rand(0,($old_h - $water_h));

                    break;

                }

            // 设定图像的混色模式

                imagealphablending($oldimg, true);

                // 添加水印

                imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);

                $fun = 'image'.$type;

                !@$fun($oldimg, $filename,100) && die('保存失败!检查目录是否存在并且可写?');

                  imagedestroy($oldimg);

                  imagedestroy($waterimg);

                  return $filename;

        }

}

 

$imgs=new image();

$tempFile = "./images/hdb.jpg";

$water = "./images/bamiluo.jpg";

 

$imgs->param($tempFile);

//if(!$imgs->thumb($targetFile,600,500))die('fail');

 

$imgs->water($targetFile,$water,0,10);

echo "aaa";

?>

摘自:ws07_byyy的专栏

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles