search
HomeBackend DevelopmentPHP TutorialAnalysis of image processing skills in PHP introductory tutorial

本文实例讲述了PHP图像处理。分享给大家供大家参考,具体如下:

Demo1.php

<?php
  //一般生成的图像可以是 png,jpg,gif,bmp
  //jpeg,wbmp
  //第一步,设置文件MIME类型,输出类型 text/html 类型是网页类型,默认可以不写
  //将输出类型改成图像流
  header(&#39;Content-Type:image/png;&#39;);
  //第二步,创建一个图形区域,图像背景
  //有两种创建方式,资源类型,一般要加上 @ 符号,防止出错
  //imagecreatetruecolor 返回的是一个资源句柄
  //这个函数创建了一个图像的区域,没有进行填充的时候,背景默认是黑色的
  $im = imagecreatetruecolor(200,200);
  //第三步,有空白图像区域,绘制颜色,文字叫,线条啊。。。
  //填充色换掉,首先要有个颜色填充器
  //imagecolorallocate -- 为一幅图像分配颜色
  $blue = imagecolorallocate($im,0,102,255);
  //将这个 blue 颜色填充到背景上去
  //imagefill -- 区域填充
  imagefill($im,0,0,$blue);
  //第四部,在蓝色的背景上输入一些线条,文字等
  $white = imagecolorallocate($im,255,255,255);
  //imageline -- 画一条线段
  imageline($im,0,0,200,200,$white);
  imageline($im,200,0,0,200,$white);
  //imagestring -- 水平地画一行字符串
  imagestring($im,5,80,20,&#39;Mr.One&#39;,$white);
  //第五步,输出最终图形
  //以 PNG 格式将图像输出到浏览器或文件
  imagepng($im);
  //第六步,我要将所有的资源全部清空
  imagedestroy($im);
?>

   

Demo2.php

<?php
  //src 可以插入各样类型的图片
  //Demo1.php 其实就是一张 png 图片
  header(&#39;Content-Type:text/html; charset=gbk&#39;);
  echo &#39;<img src="/static/imghwm/default1.png"  data-src="Demo1.php"  class="lazy"   alt="图形"/>&#39;;
?>

   

Demo3.php

<?php
  //简单的验证码
  //随机数
  //为什么要循环 0-15 之间的数呢?
  //因为要实现最简单的字母和数字混搭
  //十六进制 0-9 a-f
  //dechex -- 十进制转换为十六进制
  //创建一个四位的验证码
  for($i=0;$i<4;$i++){
    $nmsg .= dechex(mt_rand(0,15));
  }
  //echo $nmsg;
  header(&#39;Content-Type:image/png;&#39;);
  $im = imagecreatetruecolor(75,25);
  $blue = imagecolorallocate($im,0,102,255);
  $white = imagecolorallocate($im,255,255,255);
  imagefill($im,0,0,$blue);
  imagestring($im,5,20,5,$nmsg,$white);
  imagepng($im);
  imagedestroy($im);
?>

   

Demo4.php

<?php
  define(&#39;__DIR__&#39;,dirname(__FILE__).&#39;\\&#39;);
  //加载已有的图像
  header(&#39;Content-Type:image/png;&#39;);
  //header(&#39;Content-Type:image/jpeg;&#39;);
  //imagecreatefrompng -- 从 PNG 文件或 URL 新建一图像
  //用 image 载入图像,是可以编辑图像
  //在载入的图像中,加入一个小水印
  $im = imagecreatefrompng(__DIR__.&#39;ss.png&#39;);
  //$im = imagecreatefromjpeg(&#39;xx.jpg&#39;);
  $white = imagecolorallocate($im,255,255,255);
  imagestring($im,5,10,10,&#39;http://www.oneStopWeb.cn&#39;,$white);
  imagepng($im);
  //imagejpeg($im);
  imagedestroy($im);
?>

   

Demo5.php

<?php
  define(&#39;__DIR__&#39;,dirname(__FILE__).&#39;\\&#39;);
  //加载已有的图像
  header(&#39;Content-Type:image/png;&#39;);
  //header(&#39;Content-Type:image/jpeg;&#39;);
  //imagecreatefrompng -- 从 PNG 文件或 URL 新建一图像
  //用 image 载入图像,是可以编辑图像
  //在载入的图像中,加入一个小水印
  $im = imagecreatefrompng(__DIR__.&#39;ss.png&#39;);
  //$im = imagecreatefromjpeg(&#39;xx.jpg&#39;);
  $white = imagecolorallocate($im,255,255,255);
  imagestring($im,5,10,10,&#39;http://www.oneStopWeb.cn&#39;,$white);
  //font 字体还必须支持中文
  $font = &#39;C:\WINDOWS\Fonts\SIMHEI.TTF&#39;;
  //字体文件
  $text = iconv(&#39;gbk&#39;,&#39;utf-8&#39;,&#39;阅谁问君诵&#39;);
  //采用系统提供的字体
  //第二参数,是字体的大小,第三个参数是旋转角度,4,5参数是坐标
  imagettftext($im,20,10,50,100,$white,$font,$text);
  imagepng($im);
  //imagejpeg($im);
  imagedestroy($im);
?>

   

Demo6.php

<?php
  //微缩图,不但表面的大小改变了,容量也改变了
  //是真的改变了,不是表面的缩小
  define(&#39;__DIR__&#39;,dirname(__FILE__).&#39;\\&#39;);
  header(&#39;Content-Type:image/png;&#39;);
  //getimagesize -- 取得图像大小
  //获取到了原图的长度和高度
  list($width,$height) = getimagesize(__DIR__.&#39;ss.png&#39;);
  //将原图缩放成 40%
  $_width = $width * 0.4;
  $_height = $height * 0.4;
  //创建一个新图
  $im = imagecreatetruecolor($_width,$_height);
  //下面的工作是,载入原图,将原图复制到新图上去
  //载入原图
  $_im = imagecreatefrompng(__DIR__.&#39;ss.png&#39;);
  //将原图重新采样,拷贝到新图上,最后按 0.4 的比例输出
  //imagecopyresampled -- 重采样拷贝部分图像并调整大小
  imagecopyresampled($im,$_im,0,0,0,0,$_width,$_height,$width,$height);
  //将新图输出
  imagepng($im);
  //第二个参数不需要,直接 null 过度
  //第三个参数,是 0-100 来调节 JPG 的清晰度
  //如果是 imagepng,那么全部都是高清
  //imagejpeg($im,null,50);
  //销毁
  imagedestroy($im);
  imagedestroy($_im);
?>

   

希望本文所述对大家PHP程序设计有所帮助。


更多PHP入门教程之图像处理技巧分析相关文章请关注PHP中文网!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.