


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 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;
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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.

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
