Home Backend Development PHP Tutorial PHP image upload class can generate thumbnails and add watermarks at the same time_PHP tutorial

PHP image upload class can generate thumbnails and add watermarks at the same time_PHP tutorial

Jul 13, 2016 am 10:45 AM
php upload and add Can picture watermark generate kind

PHP image upload class can generate thumbnails and add watermarks at the same time This image upload code can add watermarks to uploaded images, generate small images, and also generate text watermarks.

PHP tutorial image upload class can generate thumbnails and add watermarks at the same time
This image upload code can add watermarks to uploaded images, generate small images, and also generate text watermarks.

class upimages {
        var $annexfolder = "upload";//Attachment storage point, the default is: annex
        var $smallfolder = "small";//Thumbnail storage path, note: it must be a subdirectory under $annexfolder, the default is: smallimg
        var $markfolder = "mark";//Watermark image storage location
            var $upfiletype = "jpg gif png";//Upload type, the default is: jpg gif png rar zip
        var $upfilemax = 1024; //Upload size limit, unit is "kb", default is: 1024kb
        var $fonttype;//Font
        var $maxwidth = 500; //Maximum width of image
        var $maxheight = 600; //Maximum height of the picture
Function upimages($annexfolder,$smallfolder,$includefolder) {
$this->annexfolder = $annexfolder;
$this->smallfolder = $smallfolder;
$this->fonttype = $includefolder."/04b_08__.ttf";
}
         function upload($inputname) {
                            $imagename = time();//Set the current time as the image name
If(@empty($_files[$inputname]["name"])) die("No image information has been uploaded, please confirm");
                 $name = explode(".",$_files[$inputname]["name"]);//Separate the files before uploading with "." to obtain the file type
                     $imgcount = count($name);//Get the number of interceptions
                    $imgtype = $name[$imgcount-1];//Get the type of file
if(strpos($this->upfiletype,$imgtype) === false) die(error("The upload file type only supports ".$this->upfiletype." does not support ".$imgtype));
                     $photo = $imagename.".".$imgtype;//File name for writing database tutorial
                   $uploadfile = $this->annexfolder."/".$photo;//Uploaded file name
                   $upfileok = move_uploaded_file($_files[$inputname]["tmp_name"],$uploadfile);
If($upfileok) {
                             $imgsize = $_files[$inputname]["size"];
                                $ksize = round($imgsize/1024);
If($ksize > ($this->upfilemax*1024)) {
                                                                                                                                                    die(error("The uploaded file exceeds ".$this->upfilemax."kb"));
                                                                                                          }                      } else {
                                                                                                                                  die(error("Failed to upload image, please confirm that your uploaded file does not exceed $upfilemax kb or upload time times out"));
                }
                      return $photo;
}
         function getinfo($photo) {
$photo = $this->annexfolder."/".$photo;
                     $imageinfo = getimagesize($photo);
                     $imginfo["width"] = $imageinfo[0];
                     $imginfo["height"] = $imageinfo[1];
                    $imginfo["type"] = $imageinfo[2];
                    $imginfo["name"] = basename($photo);
                      return $imginfo;
}
function smallimg($photo,$width=128,$height=128) {
                      $imginfo = $this->getinfo($photo);
                    $photo = $this->annexfolder."/".$photo;//Get the picture source
                  $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//New picture name
If($imginfo["type"] == 1) {
                             $img = imagecreatefromgif($photo);
                        } elseif($imginfo["type"] == 2) {
                            $img = imagecreatefromjpeg($photo);
                         } elseif($imginfo["type"] == 3) {
                               $img = imagecreatefrompng($photo);
                     } else {
                               $img = "";
                }
If(empty($img)) return false;
$width = ($width > $imginfo["width"]) ? $imginfo["width"] : $width;
$height = ($height > $imginfo["height"]) ? $imginfo["height"] : $height;
                        $srcw = $imginfo["width"];
                      $srch = $imginfo["height"];
If ($srcw * $width > $srch * $height) {
                                 $height = round($srch * $width / $srcw);
                     } else {
                        $width = round($srcw * $height / $srch);
                }
                if (function_exists("imagecreatetruecolor")) {
                        $newimg = imagecreatetruecolor($width, $height);
                        imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                } else {
                        $newimg = imagecreate($width, $height);
                        imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                }
                if ($this->tofile) {
                        if (file_exists($this->annexfolder."/".$this->smallfolder."/".$newname)) @unlink($this->annexfolder."/".$this->smallfolder."/".$newname);
                        imagejpeg($newimg,$this->annexfolder."/".$this->smallfolder."/".$newname);
                        return $this->annexfolder."/".$this->smallfolder."/".$newname;
                } else {
                        imagejpeg($newimg);
                }
                imagedestroy($newimg);
                imagedestroy($img);
                return $newname;
        }
        function watermark($photo,$text) {
                $imginfo = $this->getinfo($photo);
                $photo = $this->annexfolder."/".$photo;
                $newname = substr($imginfo["name"], 0, strrpos($imginfo["name"], ".")) . "_mark.jpg";
                switch ($imginfo["type"]) {
                        case 1:
                                $img = imagecreatefromgif($photo);
                        break;
                        case 2:
                                $img = imagecreatefromjpeg($photo);
                        break;
                        case 3:
                                $img = imagecreatefrompng($photo);
                        break;
                        default:
                                return false;
                }
                if (empty($img)) return false;
                $width = ($this->maxwidth > $imginfo["width"]) ? $imginfo["width"] : $this->maxwidth;
                $height = ($this->maxheight > $imginfo["height"]) ? $imginfo["height"] : $this->maxheight;
                $srcw = $imginfo["width"];
                $srch = $imginfo["height"];
                if ($srcw * $width > $srch * $height) {
                        $height = round($srch * $width / $srcw);
                } else {
                        $width = round($srcw * $height / $srch);
                }
                if (function_exists("imagecreatetruecolor")) {
                        $newimg = imagecreatetruecolor($width, $height);
                        imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                } else {
                        $newimg = imagecreate($width, $height);
                        imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $imginfo["width"], $imginfo["height"]);
                }
               
                $white = imagecolorallocate($newimg, 255, 255, 255);
                $black = imagecolorallocate($newimg, 0, 0, 0);
                $alpha = imagecolorallocatealpha($newimg, 230, 230, 230, 40);
                imagefilledrectangle($newimg, 0, $height-26, $width, $height, $alpha);
                imagefilledrectangle($newimg, 13, $height-20, 15, $height-7, $black);
                imagettftext($newimg, 4.9, 0, 20, $height-14, $black, $this->fonttype, $text[0]);
                imagettftext($newimg, 4.9, 0, 20, $height-6, $black, $this->fonttype, $text[1]);
                if($this->tofile) {
                        if (file_exists($this->annexfolder."/".$this->markfolder."/".$newname)) @unlink($this->annexfolder."/".$this->markfolder."/".$newname);
                        imagejpeg($newimg,$this->annexfolder."/".$this->markfolder."/".$newname);
                        return $this->annexfolder."/".$this->markfolder."/".$newname;
                } else {
                        imagejpeg($newimg);
                }
                imagedestroy($newimg);
                imagedestroy($img);
                return $newname;
        }
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633040.htmlTechArticlephp图片上传类同时可生成缩略图与加水印这款图片上传代码可以把上传的图片增加水印,生成小图片,同时还可以生成文字水印。 php教程图...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles