php给图片加文字
在图片上加文字是论坛,博客,新闻网站上最喜欢用的功能,防止盗图。这里看看代码是如何实现的。 首先还是upload_image.php这个文件,注意这里的caption文本框中输入的内容最终会写到图片上面 ? php // 修改图片效果 $db = mysql_connect ('localhost','root
在图片上加文字是论坛,博客,新闻网站上最喜欢用的功能,防止盗图。这里看看代码是如何实现的。
首先还是upload_image.php这个文件,注意这里的caption文本框中输入的内容最终会写到图片上面
<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>设置环境变量 //putenv('GDFONTPATH='.'C:\Windows\Fonts');</span> <span>$font</span> = "C:\Windows\Fonts\arial.ttf"<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>['uploadfile']['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>; } </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>])) { imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</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="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F152205020151940.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3731018.html" class="lazy" id alt="php给图片加文字" >.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="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F152205020151940.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3731018.html" class="lazy" 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><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>])) { </span><span>$imagename</span> .= '&capt='.<span>urlencode</span>(<span>$image_caption</span><span>); } } </span>?> <img src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F152205020151940.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3731018.html" class="lazy" 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 } ?>注意这里有个问题,选择字体的时要加上绝对路径并且带上后缀.tff,否则的话是看不到图片的,下面的代码是image_effect.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>设置环境变量 //putenv('GDFONTPATH='.'C:\Windows\Fonts');</span> <span>$font</span> = "C:\Windows\Fonts\arial.ttf"<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>['uploadfile']['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>; } </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>])) { imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</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="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F152205020151940.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3731018.html" class="lazy" id alt="php给图片加文字" >.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="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F152205020151940.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3731018.html" class="lazy" 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><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>])) { </span><span>$imagename</span> .= '&capt='.<span>urlencode</span>(<span>$image_caption</span><span>); } } </span>?> <img src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F152205020151940.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3731018.html" class="lazy" 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 } ?>这里如果我们写成下面这样
<span>putenv</span>('GDFONTPATH='.'C:\Windows\Fonts'<span>); </span><span>$font</span> = "arial";
是看不到效果的,这里顺便提一下,火狐是最佳的开发工具,怎么讲呢,看看下面的对比吧。
火狐的提示是这样的:
图片没有截完整提示是:The image “http://localhost:81/test/image_effect.php?id=31&e=-1&capt=you+are+big+bitch” cannot be displayed because it contains errors.
谷歌浏览器的显示如下:
一个未能正确显示的图片,什么信息都没有。
再来看看大IE的,如下:
也能看到错误信息,但是一大堆乱码啊!
所以说开发人员好帮手还是火狐。

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











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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
