PHP image upload class can also generate thumbnails and watermark

Source: Internet
Author: User
Tags imagejpeg
The code is as follows Copy Code
Class Upimages {


var $annexfolder = "Upload";//Attachment storage point, default: Annex


var $smallfolder = "small";//thumbnail store path, note: Must be a subdirectory under the $annexfolder, default is: smallimg


var $markfolder = "Mark";//Watermark Image Store


var $upfiletype = "jpg gif png";//type of upload, default: JPG gif png rar zip


var $upfilemax = 1024;//Upload size limit, the unit is "KB" and the default is: 1024KB


var $fonttype;//font


var $maxwidth = 500; Picture Maximum width


var $maxheight = 600; Picture Max height


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 to picture name


if (@empty ($_files[$inputname] ["name"]) die ("Do not upload picture information, please confirm");


$name = Explode (".", $_files[$inputname] ["name"]);//will be uploaded before the file with "." Get file types separately


$imgcount = count ($name);//number of intercepts obtained


$imgtype = $name [$imgcount -1];//gets the type of file


if (Strpos ($this->upfiletype, $imgtype) = = False) die (Error ("Upload file type support only". $this->upfiletype. "Unsupported". $imgtype));


$photo = $imagename. ". $imgtype//file name written to the database tutorial


$uploadfile = $this->annexfolder. " /". $photo;//file name after upload


$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 over". $this->upfilemax. " KB "));


}


} else {


Die ("Upload image failed, please make sure 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 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;


}


}

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.