Analysis of image processing skills in PHP introductory tutorial
本文实例讲述了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="/static/imghwm/default1.png" data-src="Demo1.php" class="lazy" 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); ?>
希望本文所述对大家PHP程序设计有所帮助。
更多PHP入门教程之图像处理技巧分析相关文章请关注PHP中文网!

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
