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