php图片上传类同时可生成缩略图与加水印

 代码如下 复制代码
class upimages {
        var $annexfolder = "upload";//附件存放点,默认为:annex
        var $smallfolder = "small";//缩略图存放路径,注:必须是放在 $annexfolder下的子目录,默认为:smallimg
        var $markfolder = "mark";//水印图片存放处
        var $upfiletype = "jpg gif png";//上传的类型,默认为:jpg gif png rar zip
        var $upfilemax = 1024;//上传大小限制,单位是"kb",默认为:1024kb
        var $fonttype;//字体
        var $maxwidth = 500; //图片最大宽度
        var $maxheight = 600; //图片最大高度
        function upimages($annexfolder,$smallfolder,$includefolder) {
                $this->annexfolder = $annexfolder;
                $this->smallfolder = $smallfolder;
                $this->fonttype = $includefolder."/04b_08__.ttf";
        }
        function upload($inputname) {
                $imagename = time();//设定当前时间为图片名称
                if(@empty($_files[$inputname]["name"])) die("没有上传图片信息,请确认");
                $name = explode(".",$_files[$inputname]["name"]);//将上传前的文件以"."分开取得文件类型
                $imgcount = count($name);//获得截取的数量
                $imgtype = $name[$imgcount-1];//取得文件的类型
                if(strpos($this->upfiletype,$imgtype) === false) die(error("上传文件类型仅支持 ".$this->upfiletype." 不支持 ".$imgtype));
                $photo = $imagename.".".$imgtype;//写入数据库教程的文件名
                $uploadfile = $this->annexfolder."/".$photo;//上传后的文件名称
                $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("上传文件超过 ".$this->upfilemax."kb"));
                        }
                } else {
                        die(error("上传图片失败,请确认你的上传文件不超过 $upfilemax kb 或上传时间超时"));
                }
                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;//获得图片源
                $newname = substr($imginfo["name"],0,strrpos($imginfo["name"], "."))."_thumb.jpg";//新图片名称
                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;
        }
}
时间: 2024-10-24 22:29:36

php图片上传类同时可生成缩略图与加水印的相关文章

php 图片上传代码(具有生成缩略图与增加水印功能)

   代码如下 复制代码 class upfile {  public $filepath = "www.111cn.net/"; //上传文件存放文件夹  public $filesize = 1000000; //允许上传的大小  //如果要修改允许上传文件的类型  请搜索 [ switch ($upfiletype) { //文件类型  ]  public $reimagesize = array (   true, //是否生成缩略图   400, //缩略图宽   300,/

PHP+Ajax图片上传并且无刷新生成缩略图预览

XML/HTML Code  <div id="upload-wrapper">  <div align="center">    <form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">  <input name

php实现图片上传并利用ImageMagick生成缩略图_php技巧

使用ImageMagick,您可以轻松,更快地创建缩略图,比使用PHP的容易得多. <?php // Location to upload main image: $mainDir = $_SERVER['DOCUMENT_ROOT'].'/images/l/'; // Location to create the thumb image: $smalDir = $_SERVER['DOCUMENT_ROOT'].'/images/s/'; // Command to use: $command

图片上传的Codebehind(自动生成所略图)

上传 using System;using System.Collections;using System.ComponentModel;using System.Configuration;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web

图片上传的WebForm(自动生成所略图)

web|上传|上传 <%@ Page language="c#" Codebehind="feUploadPic.aspx.cs" AutoEventWireup="false" Inherits="cj168.Web.Mag.Admins.FeData.feUploadPic" %> <%@ Register TagPrefix="cj168" TagName="Header&

yii实现图片上传及缩略图生成的方法_php实例

本文实例讲述了利用yii框架来实现图片上传功能并在上传成功之后自动生成缩略图的方法,分享给大家供大家参考.具体实现方法如下: Action文件: 复制代码 代码如下: <?php /**  * TestController.php  * Created on: 2014-1-26 12:59:36 by Outsider  */ class TestController extends CController {       /**      * 缩略图片生成      * @ path 图片路

如何用 React 完成图片上传功能?

本文讲的是如何用 React 完成图片上传功能?, 下面这篇特邀文章是由 Damon Bauer 完成的,主题是关于一个 web 开发人员非常常见的工作:为用户提供图片上传功能.我想说这并不容易,但是有了一些功能强大的工具来帮忙做一些比较"重"的工作,这个任务会觉得比以前轻松许多.Damon 甚至全程在浏览器中完成了这项任务! 对于 web 开发者来说,让用户能够上传图片是一件很常见的事情.一开始可能看起来小菜一碟,但是当真正创建一个图片上传组件的时候,还是有些问题需要去考虑的.这里有

【JAVA秒会技术之图片上传】基于Nginx及FastDFS,完成图片的上传及展示

基于Nginx及FastDFS,完成商品图片的上传及展示 一.传统图片存储及展示方式      存在问题: 1)大并发量上传访问图片时,需要对web应用做负载均衡,但是会存在图片共享问题 2)web应用服务器的存储空间有限,它是web应用服务器,而不是存储服务器. 3)web应用服务器的本身的io读写性能不高,图片上传下载时,速度偏慢 4)web应用服务器访问图片时,由于图片内容较大,并发量大的时候,会占用web应用服务器的带宽,这样该web应用服务器的其他功能就会受到较大的影响.    解决方

ASP.NET 图片上传工具类 upload image简单好用功能齐全

原文:ASP.NET 图片上传工具类 upload image简单好用功能齐全 使用方法: UploadImage ui = new UploadImage(); /***可选参数***/ ui.SetWordWater = "哈哈";//文字水印 // ui.SetPicWater = Server.MapPath("2.png");//图片水印(图片和文字都赋值图片有效) ui.SetPositionWater = 4;//水印图片的位置 0居中.1左上角.2右