php上传图片---初级版
没有样式,没有淘宝的那种放大截取大小的效果,只是实现了图片上传的功能。 图片超过100k,会出现内部错误服务器错误,需要手动更改配置文件里的MaxRequestLen属性。 下面粘上代码: 1 ? php 2 /* ********************************************************
没有样式,没有淘宝的那种放大截取大小的效果,只是实现了图片上传的功能。
图片超过100k,会出现内部错误服务器错误,需要手动更改配置文件里的MaxRequestLen属性。
下面粘上代码:
<span> 1</span> <span>php </span><span> 2</span> <span>/*</span><span>***************************************************************************** </span><span> 3</span> <span> 4</span> <span>参数说明: </span><span> 5</span> <span>$max_file_size : 上传文件大小限制, 单位BYTE </span><span> 6</span> <span>$destination_folder : 上传文件路径 </span><span> 7</span> <span>$watermark : 是否附加水印(1为加水印,其他为不加水印); </span><span> 8</span> <span> 9</span> <span>*****************************************************************************</span><span>*/</span> <span> 10</span> <span> 11</span> <span>//</span><span>上传文件类型列表</span> <span> 12</span> <span>$uptypes</span>=<span>array</span><span>( </span><span> 13</span> 'image/jpg', <span> 14</span> 'image/jpeg', <span> 15</span> 'image/png', <span> 16</span> 'image/pjpeg', <span> 17</span> 'image/gif', <span> 18</span> 'image/bmp', <span> 19</span> 'image/x-png' <span> 20</span> <span>); </span><span> 21</span> <span> 22</span> <span>$max_file_size</span>=102400; <span>//</span><span>上传文件大小限制, 单位BYTE</span> <span> 23</span> <span>$destination_folder</span>="uploadimg/"; <span>//</span><span>上传文件路径</span> <span> 24</span> <span>$watermark</span>=1; <span>//</span><span>是否附加水印(1为加水印,其他为不加水印);</span> <span> 25</span> <span>$watertype</span>=1; <span>//</span><span>水印类型(1为文字,2为图片)</span> <span> 26</span> <span>$waterposition</span>=1; <span>//</span><span>水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);</span> <span> 27</span> <span>$waterstring</span>="http://www.xplore.cn/"; <span>//</span><span>水印字符串</span> <span> 28</span> <span>$waterimg</span>="xplore.gif"; <span>//</span><span>水印图片</span> <span> 29</span> <span>$imgpreview</span>=1; <span>//</span><span>是否生成预览图(1为生成,其他为不生成);</span> <span> 30</span> <span>$imgpreviewsize</span>=1/2; <span>//</span><span>缩略图比例</span> <span> 31</span> ?> <span> 32</span> <span> 33</span> <span> 34</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <span> 35</span> <title>php上传图片</title> <span> 36</span> <span> 37</span> <span> 38</span> <span> 39</span> <span> 40</span>
文件名: ".$destination_folder.$fname."
"; 75 echo " 宽度:".$image_size[0];//宽 76 echo " 长度:".$image_size[1];//高 77 echo "
大小:".$file["size"]." bytes";//大小 78 79 if($watermark==1) 80 { 81 $iinfo=getimagesize($destination,$iinfo); 82 $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);//新建一个真彩色图像 83 $white=imagecolorallocate($nimage,255,255,255);//为一幅图像分配颜色,黑色 84 $black=imagecolorallocate($nimage,0,0,0);//为一幅图像分配颜色,白色 85 $red=imagecolorallocate($nimage,255,0,0);//为一幅图像分配颜色 86 imagefill($nimage,0,0,$white);//填充,0,0代表坐标 87 switch ($iinfo[2]) 88 { 89 case 1: 90 $simage =imagecreatefromgif($destination);//由文件或URL创建一个新gif图象 91 break; 92 case 2: 93 $simage =imagecreatefromjpeg($destination);//由文件或URL创建一个新jpeg图象 94 break; 95 case 3: 96 $simage =imagecreatefrompng($destination);//由文件或URL创建一个新png图象 97 break; 98 case 6: 99 $simage =imagecreatefromwbmp($destination);//由文件或URL创建一个新bmp图象 100 break; 101 default: 102 die("不支持的文件类型"); 103 exit; 104 } 105 106 imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 107 imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); 108 109 switch($watertype) 110 { 111 case 1: //加水印字符串 112 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);//col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处 113 break; 114 case 2: //加水印图片 115 $simage1 =imagecreatefromgif("xplore.gif");//由文件或URL创建一个新图象 116 imagecopy($nimage,$simage1,0,0,0,0,85,15);//拷贝图像的一部分 117 imagedestroy($simage1);//销毁一图像 118 break; 119 } 120 121 switch ($iinfo[2]) 122 { 123 //输出图象到浏览器或文件 124 case 1: 125 //imagegif($nimage, $destination); 126 imagejpeg($nimage, $destination); 127 break; 128 case 2: 129 imagejpeg($nimage, $destination); 130 break; 131 case 3: 132 imagepng($nimage, $destination); 133 break; 134 case 6: 135 imagewbmp($nimage, $destination); 136 //imagejpeg($nimage, $destination); 137 break; 138 } 139 140 //覆盖原上传文件 141 imagedestroy($nimage); 142 imagedestroy($simage); 143 } 144 145 if($imgpreview==1) 146 { 147 echo "
图片预览:
"; 148 echo "

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
