PHP image upload class can also generate thumbnails and watermark _php Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP image upload class can also generate thumbnails and watermark This image upload code can upload images to add watermarks, generate small images, but also can generate text watermark.

PHP Tutorial Picture Upload class can also generate thumbnails and watermark
This image upload code can be uploaded images to add watermarks, generate small images, but also can generate text watermark.

Class Upimages {
var $annexfolder = "Upload";//Attachment storage point, default: Annex
var $smallfolder = "small";//thumbnail storage path, note: Must be placed under the $annexfolder subdirectory, default: Smallimg
var $markfolder = "Mark";//Watermark Picture Storage Place
var $upfiletype = "jpg gif png";//upload type, default: JPG gif png rar zip
var $upfilemax = 1024;//upload size limit in KB, default: 1024KB
var $fonttype;//font
var $maxwidth = 500; Max width of picture
var $maxheight = 600; Maximum height of picture
function Upimages ($annexfolder, $smallfolder, $includefolder) {
$this->annexfolder = $annexfolder;
$this->smallfolder = $smallfolder;
$this->fonttype = $includefolder. " /04b_08__.ttf ";
}
function upload ($inputname) {
$imagename = time ();//set current times as picture names
if (@empty ($_files[$inputname ["name"]) die ("Do not upload picture information, please confirm");
$name = Explode (".", $_files[$inputname ["name"]);//upload the file before uploading to "." Get file types separately
$imgcount = count ($name);//Get the number of intercepts
$imgtype = $name [$imgcount -1];//Gets the type of the file
if (Strpos ($this->upfiletype, $imgtype) = = = False) die (Error ("Upload file type only". $this->upfiletype. "Not supported". $imgtype));
$photo = $imagename. ".". $imgtype;//file name of the Write database tutorial
$uploadfile = $this->annexfolder. " /". $photo;//file name after uploading
$upfileok = Move_uploaded_file ($_files[$inputname ["Tmp_name"], $uploadfile);
if ($upfileok) {
$imgsize = $_files[$inputname] ["size"];
$ksize = Round ($imgsize/1024);
if ($ksize > ($this->upfilemax*1024)) {
@unlink ($uploadfile);
Die (Error ("Upload file exceeds". $this->upfilemax. " KB "));
}
} else {
Die (Error ("Upload image failed, please verify that your upload file does not exceed $upfilemax KB or upload time timeout"));
}
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 a 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, $height -20, $height-7, $black);
Imagettftext ($newimg, 4.9, 0, $height -14, $black, $this->fonttype, $text [0]);
Imagettftext ($newimg, 4.9, 0, $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;
}
}

http://www.bkjia.com/PHPjc/633040.html www.bkjia.com true http://www.bkjia.com/PHPjc/633040.html techarticle PHP image upload class can also generate thumbnails and watermark This image upload code can upload images to add watermarks, generate small images, but also can generate text watermark. PHP Tutorial Diagram ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.