


PHP introductory tutorial image processing skills example sharing
这篇文章主要介绍了PHP入门教程之图像处理技巧,结合实例形式分析了图形图像的创建与显示方法,并附带了创建图形验证码及缩略图等应用的具体示例,需要的朋友可以参考下
Demo1.php
<?php //一般生成的图像可以是 png,jpg,gif,bmp //jpeg,wbmp //第一步,设置文件MIME类型,输出类型 text/html 类型是网页类型,默认可以不写 //将输出类型改成图像流 header('Content-Type:image/png;'); //第二步,创建一个图形区域,图像背景 //有两种创建方式,资源类型,一般要加上 @ 符号,防止出错 //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,'Mr.One',$white); //第五步,输出最终图形 //以 PNG 格式将图像输出到浏览器或文件 imagepng($im); //第六步,我要将所有的资源全部清空 imagedestroy($im); ?>
Demo2.php
<?php //src 可以插入各样类型的图片 //Demo1.php 其实就是一张 png 图片 header('Content-Type:text/html; charset=gbk'); echo '<img src="Demo1.php" alt="图形"/>'; ?>
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('Content-Type:image/png;'); $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('__DIR__',dirname(__FILE__).'\\'); //加载已有的图像 header('Content-Type:image/png;'); //header('Content-Type:image/jpeg;'); //imagecreatefrompng -- 从 PNG 文件或 URL 新建一图像 //用 image 载入图像,是可以编辑图像 //在载入的图像中,加入一个小水印 $im = imagecreatefrompng(__DIR__.'ss.png'); //$im = imagecreatefromjpeg('xx.jpg'); $white = imagecolorallocate($im,255,255,255); imagestring($im,5,10,10,'http://www.oneStopWeb.cn',$white); imagepng($im); //imagejpeg($im); imagedestroy($im); ?>
Demo5.php
<?php define('__DIR__',dirname(__FILE__).'\\'); //加载已有的图像 header('Content-Type:image/png;'); //header('Content-Type:image/jpeg;'); //imagecreatefrompng -- 从 PNG 文件或 URL 新建一图像 //用 image 载入图像,是可以编辑图像 //在载入的图像中,加入一个小水印 $im = imagecreatefrompng(__DIR__.'ss.png'); //$im = imagecreatefromjpeg('xx.jpg'); $white = imagecolorallocate($im,255,255,255); imagestring($im,5,10,10,'http://www.oneStopWeb.cn',$white); //font 字体还必须支持中文 $font = 'C:\WINDOWS\Fonts\SIMHEI.TTF'; //字体文件 $text = iconv('gbk','utf-8','阅谁问君诵'); //采用系统提供的字体 //第二参数,是字体的大小,第三个参数是旋转角度,4,5参数是坐标 imagettftext($im,20,10,50,100,$white,$font,$text); imagepng($im); //imagejpeg($im); imagedestroy($im); ?>
Demo6.php
<?php //微缩图,不但表面的大小改变了,容量也改变了 //是真的改变了,不是表面的缩小 define('__DIR__',dirname(__FILE__).'\\'); header('Content-Type:image/png;'); //getimagesize -- 取得图像大小 //获取到了原图的长度和高度 list($width,$height) = getimagesize(__DIR__.'ss.png'); //将原图缩放成 40% $_width = $width * 0.4; $_height = $height * 0.4; //创建一个新图 $im = imagecreatetruecolor($_width,$_height); //下面的工作是,载入原图,将原图复制到新图上去 //载入原图 $_im = imagecreatefrompng(__DIR__.'ss.png'); //将原图重新采样,拷贝到新图上,最后按 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); ?>
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
The above is the detailed content of PHP introductory tutorial image processing skills example sharing. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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

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.
