PHP修改图片
这篇是关于修改图片的效果,主要还是用到php中的GD库中的函数,没想到php还有这凶残能力,出乎我的预料。 先看代码upload_image.php,主要是一个上传控件,用来选择图片 html head title / title style type ="text/css" / style / head body form action ="
这篇是关于修改图片的效果,主要还是用到php中的GD库中的函数,没想到php还有这凶残能力,出乎我的预料。
先看代码upload_image.php,主要是一个上传控件,用来选择图片
<span><span>html</span><span>></span> <span><span>head</span><span>></span> <span><span>title</span><span>></span><span>title</span><span>></span> <span><span>style </span><span>type</span><span>="text/css"</span><span>></span><span>style</span><span>></span> <span></span><span>head</span><span>></span> <span><span>body</span><span>></span> <span><span>form </span><span>action</span><span>="check_image.php"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span> <span><span>table</span><span>></span> <span><span>tr</span><span>></span> <span><span>td</span><span>></span>Your username<span></span><span>td</span><span>></span> <span><span>td</span><span>><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="username"</span> <span>/></span><span>td</span><span>></span> <span></span><span>tr</span><span>></span> <span><span>tr</span><span>></span> <span><span>td</span><span>></span>Upload image*<span></span><span>td</span><span>></span> <span><span>td</span><span>><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="uploadfile"</span><span>/></span><span>td</span><span>></span> <span></span><span>tr</span><span>></span> <span><span>tr</span><span>></span> <span><span>td </span><span>colspan</span><span>="2"</span><span>></span> <span><span>small</span><span>><span>em</span><span>></span> * Acceptable image formats include: GIF, JPG/JPEG and PNG.<span></span><span>em</span><span>></span><span>small</span><span>></span> <span></span><span>td</span><span>></span> <span></span><span>tr</span><span>></span> <span><span>tr</span><span>></span> <span><span>td</span><span>></span>Image Caption<span></span><span>td</span><span>></span> <span><span>td</span><span>><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="caption"</span><span>/></span><span>td</span><span>></span> <span></span><span>tr</span><span>></span> <span><span>tr</span><span>></span> <span><span>td </span><span>colspan</span><span>="2"</span><span> style</span><span>="text-align:center;"</span><span>></span> <span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="Upload"</span> <span>/></span> <span></span><span>td</span><span>></span> <span></span><span>tr</span><span>></span> <span></span><span>table</span><span>></span> <span></span><span>form</span><span>></span> <span></span><span>body</span><span>></span> <span></span><span>html</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
然后是上传和处理图片的逻辑check_image.php
<span>php </span><span>//</span><span>修改图片效果</span> <span>$db</span> = <span>mysql_connect</span>('localhost','root','Ctrip07185419') or <span>die</span>('can not connect to database'<span>); </span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>//</span><span>上传文件的路径</span> <span>$dir</span> = 'D:\Serious\phpdev\test\images'<span>; </span><span>//</span><span>upload_image.php页面传递过来的参数,如果是上传图片</span> <span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>) { </span><span>if</span>(<span>$_FILES</span>['uploadfile']['error'] !=<span> UPLOAD_ERR_OK) { </span><span>switch</span>(<span>$_FILES</span>['uploadfiel']['error'<span>]) { </span><span>case</span> UPLOAD_ERR_INI_SIZE: <span>die</span>('The uploaded file exceeds the upload_max_filesize directive'<span>); </span><span>break</span><span>; </span><span>case</span> UPLOAD_ERR_FORM_SIZE: <span>die</span>('The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'<span>); </span><span>break</span><span>; </span><span>case</span> UPLOAD_ERR_PARTIAL: <span>die</span>('The uploaded file was only partially uploaded'<span>); </span><span>break</span><span>; </span><span>case</span> UPLOAD_ERR_NO_FILE: <span>die</span>('No file was uploaded'<span>); </span><span>break</span><span>; </span><span>case</span> UPLOAD_ERR_NO_TMP_DIR: <span>die</span>('The server is missing a temporary folder'<span>); </span><span>break</span><span>; </span><span>case</span> UPLOAD_ERR_CANT_WRITE: <span>die</span>('The server fail to write the uploaded file to the disk'<span>); </span><span>break</span><span>; </span><span>case</span> UPLOAD_ERR_EXTENSION: <span>die</span>('The upload stopped by extension'<span>); </span><span>break</span><span>; } } </span><span>$image_caption</span> = <span>$_POST</span>['caption'<span>]; </span><span>$image_username</span> = <span>$_POST</span>['username'<span>]; </span><span>$image_date</span> = <span>date</span>('Y-m-d'<span>); </span><span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$_FILES</span>['uploadfile']['tmp_name'<span>]); </span><span>$error</span> = 'The file you upload is not a supported filetype'<span>; </span><span>switch</span>(<span>$type</span><span>) { </span><span>case</span> IMAGETYPE_GIF: <span>$image</span> = imagecreatefromgif(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>(<span>$error</span><span>); </span><span>break</span><span>; </span><span>case</span> IMAGETYPE_JPEG: <span>$image</span> = imagecreatefromjpeg(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>(<span>$error</span><span>); </span><span>break</span><span>; </span><span>case</span> IMAGETYPE_PNG: <span>$image</span> = imagecreatefrompng(<span>$_FILES</span>['uploadfile']['tmp_name']) or <span>die</span>(<span>$error</span><span>); </span><span>break</span><span>; </span><span>default</span>: <span>break</span><span>; } </span><span>$query</span> = 'insert into images(image_caption,image_username,image_date) values("'.<span>$image_caption</span>.'" , "'.<span>$image_username</span>.'","'.<span>$image_date</span>.'")'<span>; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span>,<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>$last_id</span> = <span>mysql_insert_id</span><span>(); </span><span>//</span><span> $imagename = $last_id.'.jpg'; // imagejpeg($image,$dir.'/'.$imagename); // imagedestroy($image);</span> <span>$image_id</span> = <span>$last_id</span><span>; imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$image_id</span>.'.jpg'<span>); imagedestroy(</span><span>$image</span><span>); } </span><span>else</span> <span>//</span><span>如果图片已经上传,则从数据库中取图片名字</span> <span>{ </span><span>$query</span> = 'select image_id,image_caption,image_username,image_date from images where image_id='.<span>$_POST</span>['id'<span>]; </span><span>$result</span> = <span>mysql_query</span>(<span>$query</span>,<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>)); </span><span>extract</span>(<span>mysql_fetch_assoc</span>(<span>$result</span><span>)); </span><span>list</span>(<span>$width</span>,<span>$height</span>,<span>$type</span>,<span>$attr</span>) = <span>getimagesize</span>(<span>$dir</span>.'/'.<span>$image_id</span>.'.jpg'<span>); } </span><span>//</span><span>如果是保存图片</span> <span>if</span>(<span>$_POST</span>['submit'] == 'Save'<span>) { </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['id']) && <span>ctype_digit</span>(<span>$_POST</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>)) { </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>); } </span><span>else</span><span> { </span><span>die</span>('invalid image specified'<span>); } </span><span>$effect</span> = (<span>isset</span>(<span>$_POST</span>['effect'])) ? <span>$_POST</span>['effect'] : -1<span>; </span><span>switch</span>(<span>$effect</span><span>) { </span><span>case</span> IMG_FILTER_NEGATE:<span> imagefilter(</span><span>$image</span> , IMG_FILTER_NEGATE); <span>//</span><span>将图像中所有颜色反转</span> <span>break</span><span>; </span><span>case</span> IMG_FILTER_GRAYSCALE:<span> imagefilter(</span><span>$image</span> , IMG_FILTER_GRAYSCALE); <span>//</span><span>将图像转换为灰度的</span> <span>break</span><span>; </span><span>case</span> IMG_FILTER_EMBOSS:<span> imagefilter(</span><span>$image</span> , IMG_FILTER_EMBOSS); <span>//</span><span>使图像浮雕化</span> <span>break</span><span>; </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span> imagefilter(</span><span>$image</span> , IMG_FILTER_GAUSSIAN_BLUR); <span>//</span><span>用高斯算法模糊图像</span> <span>break</span><span>; } imagejpeg(</span><span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg' , 100<span>); </span>?> <title>Here is your pic!</title> <h1 id="Your-image-has-been-saved">Your image has been saved!</h1> <img src="images/<?php%20echo%20<span>%24_POST</span>%5B'id'%5D;?>.jpg" alt=""> <span>php } </span><span>else</span><span> { </span>?> <title>Here is your pic!</title> <h1 id="So-how-does-it-feel-to-be-famous">So how does it feel to be famous?</h1> <p>Here is the picture you just uploaded to your servers:</p> <!--<img src="images/<?php echo <span alt="PHP修改图片" >$imagename</span>;?>" alt="" style="max-width:90%" />--> <span>php </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>) { </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.'.jpg'<span>; } </span><span>else</span><span> { </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>]; } </span>?> <img src="<?php%20echo%20<span>%24imagename</span>;?>" style="max-width:90%" alt="">
Image save as: | $image_id?> |
Height: | echo $height;?> |
Widht: | echo $width;?> |
Upload date: | echo $image_date;?> |
You may apply a special effect to your image from the list of option below. Note:saving an image with any of the filters applied can be undone
php } ?>最后是一个预览效果的页面image_effect.php
<span>php </span><span>$dir</span> = 'D:\Serious\phpdev\test\images'<span>; </span><span>if</span>(<span>isset</span>(<span>$_GET</span>['id']) && <span>ctype_digit</span>(<span>$_GET</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_GET</span>['id'].'.jpg'<span>)) { </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_GET</span>['id'].'.jpg'<span>); } </span><span>else</span><span> { </span><span>die</span>('invalid image specified'<span>); } </span><span>$effect</span> = (<span>isset</span>(<span>$_GET</span>['e'])) ? <span>$_GET</span>['e'] : -1<span>; </span><span>switch</span>(<span>$effect</span><span>) { </span><span>case</span> IMG_FILTER_NEGATE:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_NEGATE); </span><span>break</span><span>; </span><span>case</span> IMG_FILTER_GRAYSCALE:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GRAYSCALE); </span><span>break</span><span>; </span><span>case</span> IMG_FILTER_EMBOSS:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_EMBOSS); </span><span>break</span><span>; </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GAUSSIAN_BLUR); </span><span>break</span><span>; } </span><span>header</span>('Content-Type:image/jpeg'<span>); imagejpeg(</span><span>$image</span> , '' , 100<span>); </span>?>
第二个image_check.php有点乱,在这个页面中有上传图片,处理图片,还有预览图片的部分逻辑,注意下面这段
<span>php </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>) { </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.'.jpg'<span>; } </span><span>else</span><span> { </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>]; } </span>?> <img src="<?php%20echo%20<span>%24imagename</span>;?>" style="max-width:90%" alt="">
如果是上传直接访问图片,如果是预览则从image_effect.php中读取图片,这里是从内存中读取图片并根据选择的处理效果来展示图片。如下
<span>switch</span>(<span>$effect</span><span>) { </span><span>case</span> IMG_FILTER_NEGATE:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_NEGATE); </span><span>break</span><span>; </span><span>case</span> IMG_FILTER_GRAYSCALE:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GRAYSCALE); </span><span>break</span><span>; </span><span>case</span> IMG_FILTER_EMBOSS:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_EMBOSS); </span><span>break</span><span>; </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span> imagefilter(</span><span>$image</span> ,<span> IMG_FILTER_GAUSSIAN_BLUR); </span><span>break</span><span>; }</span>
当使用imagefilter方法处理图片之后会把图片输出到页面,这里要注意imagejpeg方法的第二个参数是空字符串,这样它就不会写入到硬盘中了,如果第二个参数设置了会覆盖原有的图片,这样可以让用户在保存图片之前随意的预览效果,如下:
<span>header</span>('Content-Type:image/jpeg'<span>); imagejpeg(</span><span>$image</span> , '' , 100);
在check_image.php中有调用到类似的方法,但是这里指定了第二个参数,就是用来保存图片的:
imagejpeg(<span>$image</span> , <span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg' , 100);
注意这里哦我们只写了三种处理效果,这个只是所有枚举中的一部分,我们来看所有的处理方式:
IMG_FILTER_NEGATE:将图像中所有颜色反转。
IMG_FILTER_GRAYSCALE:将图像转换为灰度的。
IMG_FILTER_BRIGHTNESS:改变图像的亮度。用 arg1 设定亮度级别。
IMG_FILTER_CONTRAST:改变图像的对比度。用 arg1 设定对比度级别。
IMG_FILTER_COLORIZE:与 IMG_FILTER_GRAYSCALE 类似,不过可以指定颜色。用 arg1,arg2 和 arg3 分别指定 red,blue 和 green。每种颜色范围是 0 到 255。
IMG_FILTER_EDGEDETECT:用边缘检测来突出图像的边缘。
IMG_FILTER_EMBOSS:使图像浮雕化。
IMG_FILTER_GAUSSIAN_BLUR:用高斯算法模糊图像。
IMG_FILTER_SELECTIVE_BLUR:模糊图像。
IMG_FILTER_MEAN_REMOVAL:用平均移除法来达到轮廓效果。
IMG_FILTER_SMOOTH:使图像更柔滑。用 arg1 设定柔滑级别。
是不是很惊艳,php很强大的。

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

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

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

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,

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

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.
