Home Backend Development PHP Tutorial How to implement php code to process images

How to implement php code to process images

May 16, 2018 am 11:59 AM
php picture deal with

This article mainly introduces how to implement php code to process images. Interested friends can refer to it. I hope it will be helpful to everyone.

1. Image scaling code

Non-proportional image scaling code

$fileNames='./images/hee.jpg';
list($s_w,$s_h)=getimagesize($fileNames);
$per=0.3;
$n_w=$s_w*$per;
$n_h=$s_h*$per;
$s_img=imagecreatefromjpeg($fileNames);
$n_img=imagecreatetruecolor($n_w,$n_h);
imagecopyresized($n_img,$s_img,0,0,0,0,$n_w,$n_h,$s_w,$s_h);
header("Content-Type:image/jpeg");
imagejpeg($n_img,'./images/hee2.jpg');
imagedestroy($n_img);
imagedestroy($s_img);
function sf($backgroung,$bl,$location){
list($width,$height)=getimagesize($backgroung);
$n_w=$width*$bl;
$n_h=$height*$bl;
$s_img=imagecreatefromjpeg($backgroung);
$n_img=imagecreatetruecolor($n_w,$n_h);
imagecopyresized($n_img,$s_img,0,0,0,0,$n_w,$n_h,$width,$height);
header("Content-Type:image/jpeg");
imagejpeg($n_img,$location);
imagedestroy($s_img);
imagedestroy($n_img);
}
//sf('./images/hee.jpg',0.3,'./images/hee9.jpg');
Copy after login

Equal proportion picture scaling (and adding transparent color processing)

function imagesf($backgroung,$n_w,$n_h,$newFiles){
list($s_w,$s_h,$imagetype)=getimagesize($backgroung);
//等比例缩放固定算法
if ($n_w && ($s_w < $s_h)) {
$n_w = ($n_h / $s_h) * $s_w;
} else {
$n_h = ($n_w / $s_w) * $s_h;
}
if($imagetype==2){
$s_img=imagecreatefromjpeg($backgroung);
}elseif($imagetype==3){
$s_img=imagecreatefrompng($backgroung);
}elseif($imagetype==1){
$s_img=imagecreatefromgif($backgroung);
}else{
echo "php 不支持这中图片类型!!";
}
$n_img=imagecreatetruecolor($n_w,$n_h);
//取得原图形的索引
$img_index=imagecolortransparent($s_img);
//判断原图形索引是否在图形调色板的数目范围内(调色板数目 0~255);
if($img_index>=0 && $img_index<imagecolorstotal($s_img)){
//返回一个具有 red,green,blue 和 alpha 的键名的关联数组,包含了指定颜色索引的相应的值
$trans=imagecolorsforindex($s_img,$img_index);
//print_r($trans); Array ( [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 )
//取原图的颜色,给新图分配颜色
$n_color=imagecolorallocate($n_img,$trans[&#39;red&#39;],$trans[&#39;green&#39;],$trans[&#39;blue&#39;]);
imagefill($n_img,0,0,$n_color);
//把获取到的颜色,设置为新图的透明色
imagecolortransparent($n_img,$n_color);
}
//imagecopyresampled($n_img,$s_img,0,0,0,0,$n_w,$n_h,$s_w,$s_h);
if(imagetypes()& IMG_JPG){
header("Content-Type:image/jpeg");
imagejpeg($n_img,$newFiles);
}elseif(imagetypes()& IMG_PNG){
header("Content-Type:image/png");
imagepng($n_img,$newFiles);
}elseif(imagetypes()& IMG_GIF){
header("Content-Type:image/gif");
imagegif($n_img,$newFiles);
}else{
echo "php 不支持这中图片类型!!";
}
imagedestroy($s_img);
imagedestroy($n_img);
}
imagesf(&#39;./images/map.gif&#39;,200,200,&#39;./images/map2.gif&#39;);
Copy after login





2. Image cropping code

Picture cropping

function imagecut($backgroung,$cut_x,$cut_y,$cut_width,$cut_hight,$fileNames){
list(,,$imagetype)=getimagesize($backgroung);
if($imagetype==1){
$s_img=imagecreatefromgif($backgroung);
}elseif($imagetype==2){
$s_img=imagecreatefromjpeg($backgroung);
}elseif($imagetype==2){
$s_img=imagecreatefrompng($backgroung);
}else{
echo "php 不支持这种图片类型!!";
}
$n_img=imagecreatetruecolor($cut_width,$cut_hight);
imagecopyresized($n_img,$s_img,0,0,$cut_x,$cut_y,$cut_width,$cut_hight,$cut_width,$cut_hight);
if(imagetypes() & IMG_GIF){
header("Content-Type:image/gif");
imagegif($n_img,$fileNames);
}elseif(imagetypes() & IMG_JPG){
header("Content-Type:image/jpeg");
imagejpeg($n_img,$fileNames);
}elseif(imagetypes() & IMG_PNG){
header("Content-Type:image/png");
imagepng($n_img,$fileNames);
}else{
echo "php 不支持这种图片类型!!";
}
}
//imagecut(&#39;./images/hee.jpg&#39;,52, 47, 345, 330,&#39;./images/hee4.jpg&#39;);
function cut($backgroung,$x,$y,$cut_width,$cut_hight,$location){
$s_img=imagecreatefromjpeg($backgroung);
$n_img=imagecreatetruecolor($cut_width,$cut_hight);
imagecopy($n_img,$s_img,0,0,$x,$y,$cut_width,$cut_hight);
header("Content-Type:image/jpeg");
imagejpeg($n_img,$location);
imagedestroy($s_img);
imagedestroy($n_img);
}
//cut(&#39;./images/hee.jpg&#39;,52, 47, 345, 330,&#39;./images/hee8.jpg&#39;);
Copy after login



##Crop the image and scale the cropped image, instead of scaling the image proportionally (that is, copy part of the image and resize it) imagecopyresized() — Copy part of the image and resize it

imagecopyresampled() — Resample and resize part of the image copy

function cutsf($backgroung,$x,$y,$cut_width,$cut_hight,$n_w,$n_h,$location){
$s_img=imagecreatefromjpeg($backgroung);
$n_img=imagecreatetruecolor($n_w,$n_h);
imagecopyresized($n_img,$s_img,0,0,$x,$y,$n_w,$n_h,$cut_width,$cut_hight);
header("Content-Type:image/jpeg");
imagejpeg($n_img,$location);
imagedestroy($s_img);
imagedestroy($n_img);
}
cutsf(&#39;./images/hee.jpg&#39;,52, 47, 345, 330,200,200,&#39;./images/hee10.jpg&#39;);
Copy after login



##三,
Add watermark to picture: text watermark and image watermark code

Add text watermark to picture
function mark_text($backgroung,$x,$y,$text,$fileNames){
list(,,$imagetype)=getimagesize($backgroung);
if($imagetype == 1){
$s_img=imagecreatefromgif($backgroung);
}elseif($imagetype == 2){
$s_img=imagecreatefromjpeg($backgroung);
}elseif($imagetype == 3){
$s_img=imagecreatefrompng($backgroung);
}
$green=imagecolorallocate($s_img,0,255,0);
imagettftext($s_img,20,0,$x,$y,$green,&#39;./font/msyh.ttf&#39;,$text);
if(imagetypes() & IMG_GIF){
header("Content-Type:image/gif");
imagegif($s_img,$fileNames);
}elseif(imagetypes() & IMG_JPG){
header("Content-Type:image/jpeg");
imagejpeg($s_img,$fileNames);
}elseif(imagetypes() & IMG_PNG){
header("Content-Type:image/png");
imagepng($s_img,$fileNames);
}
imagedestroy($s_img);
}
$text=iconv(&#39;gb2312&#39;,&#39;utf-8&#39;,&#39;细说PHP&#39;);
//mark_text(&#39;./images/hee.jpg&#39;,150,250,$text,&#39;./images/hee5.jpg&#39;);
Copy after login


Add image watermark to picture

function mark_pic($backgroung,$x,$y,$waterpic,$fileNames){
$s_img=imagecreatefromjpeg($backgroung);
$waterpic_img=imagecreatefromgif($waterpic);
$waterpic_w=imagesx($waterpic_img);
$waterpic_h=imagesy($waterpic_img);
imagecopy( $s_img,  $waterpic_img, $x,$y, 0,0, $waterpic_w,$waterpic_h);
header("Content-Type:image/jpeg");
imagejpeg($s_img,$fileNames);
imagedestroy($s_img);
imagedestroy($waterpic_img);
}
//mark_pic(&#39;./images/hee.jpg&#39;,50,200,&#39;./images/gaolf.gif&#39;,&#39;./images/hee6.jpg&#39;);
Copy after login





4. Image rotation code

##Image rotation resource imagerotate() — Rotate the image with a given angle , return the new resource
function imagexz($backgroung,$angle,$location){
$s_img=imagecreatefromjpeg($backgroung);
$n_img=imagerotate($s_img,$angle,0);
header("Content-Type:image/jpeg");
imagejpeg($n_img,$location);
imagedestroy($s_img);
imagedestroy($n_img);
}
imagexz(&#39;./images/hee.jpg&#39;,360,&#39;images/hee10.jpg&#39;); //90 180 270 360
Copy after login



##5. Image flipping



Flipping the image along the Y-axis means swapping left and right

function turn_y($backgroung,$newFiles){
$s_img=imagecreatefromjpeg($backgroung);
$width=imagesx($s_img);
$height=imagesy($s_img);
$n_img=imagecreatetruecolor($width,$height);
for( $x=0; $x<$width; $x++ ){
imagecopy($n_img,$s_img, $width-$x-1,0, $x,0, 1,$height);
}
header("Content-Type:image/jpeg");
imagejpeg($n_img,$newFiles);
imagedestroy($s_img);
imagedestroy($n_img);
}
//turn_y(&#39;./images/hee.jpg&#39;,&#39;./images/hee11.jpg&#39;);
Copy after login


## Flip the picture along the x-axis, that is, flip it up and down


function turn_x($backgroung,$newFiles){
$s_img=imagecreatefromjpeg($backgroung);
$width=imagesx($s_img);
$height=imagesy($s_img);
$n_img=imagecreatetruecolor($width,$height);
for( $y=0; $y<$height; $y++ ){
imagecopy($n_img,$s_img, 0,$height-$y-1, 0,$y, $width,1);
}
header("Content-Type:image/jpeg");
imagejpeg($n_img,$newFiles);
imagedestroy($s_img);
imagedestroy($n_img);
}
//turn_x(&#39;./images/hee.jpg&#39;,&#39;./images/hee12.jpg&#39;);
Copy after login

6. Image sharpening



function sharp($background, $degree, $save){
$back=imagecreatefromjpeg($background);
$b_x=imagesx($back);
$b_y=imagesy($back);
$dst=imagecreatefromjpeg($background);
for($i=$b_x-1; $i>0; $i--){
for($j=$b_y-1; $j>0; $j--){
$b_clr1=imagecolorsforindex($back, imagecolorat($back, $i-1, $j-1));
$b_clr2=imagecolorsforindex($back, imagecolorat($back, $i, $j));
$r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"]));
$g=intval($b_clr2["green"]+$degree*($b_clr2["green"]-$b_clr1["green"]));
$b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]-$b_clr1["blue"]));
$r=min(255, max($r, 0));
$g=min(255, max($g, 0));
$b=min(255, max($b, 0));
if(($d_clr=imagecolorexact($dst, $r, $g, $b))==-1){
$d_clr=Imagecolorallocate($dst, $r, $g, $b);
}
imagesetpixel($dst, $i, $j, $d_clr);
}
}
imagejpeg($dst, $save);
imagedestroy($back);
imagedestroy($dst);
}
sharp("./images/hee.jpg", 20, "./images/hee15.jpg");
Copy after login


#Image sharpening (analytical principle) (first obtain the color of the original image, then sharpen the color of the original image, and finally assign the sharpened color to the original image)

#解析开始:
#1 创建要锐化的图片资源
#eg:$s_img=imagecreatefromjpeg('./images/hee.jpg');
#2 获取图片资源的宽、高,创建两层循环(外层控制宽 内层控制高) 对每一个像素进行锐化
#eg:$width=imagesx($s_img);
# $height=imagesy($s_img);
# for($i=$width-1; $i>0; $i--){
# for($j=$height-1; $j>0; $j--){
#
# }
# }
#3 在内层循环里对每一个像素颜色进行锐化(写在内层循环):
#3.1 用函数$color=imagecolorsforindex()取得图片索引的颜色,即取得当前颜色和取得上一个颜色
#eg: $s_img_color1=imagecolorsforindex($s_img , imagecolorat($s_img , $i-1, $j-1));//上一个颜色
# $s_img_color2=imagecolorsforindex($s_img , imagecolorat($s_img , $i, $j));//当前颜色
#3.2 固定算法对取得的颜色进行锐化 比如锐化20:$degree=20
#eg:$r=intval($color["red"]+$degree*($color["red"]-$color["red"]));
# $g=intval($s_img_color2["green"]+$degree*($s_img_color2["green"]-$s_img_color1["green"]));
# $b=intval($s_img_color2["blue"]+$degree*($s_img_color2["blue"]-$s_img_color1["blue"]));
#3.3 把取得的RGB颜色控制在0~255正常范围内
#$r=min(255, max($r, 0));
#$g=min(255, max($g, 0));
#$b=min(255, max($b, 0));
#4 把锐化后的每一个像素颜色重新赋给原图的每一个像素颜色(写在内层循环)
#4.1 取得锐化后颜色的索引:$rh_color
#eg:if( ($rh_color=imagecolorexact($s_img,$r,$g,$b)) ==-1){
# $rh_color=imagecolorallocate($s_img,$r,$g,$b);
# }
#4.2 把锐化后的每一个像素颜色重新赋给原图的每一个像素颜色
eg:imagesetpixel($s_img, $i, $j, $rh_color);
#5 保存图片资源(写在循环外)
#eg:imagejpeg($s_img'./images/hee16.jpg');
#6 关闭图片资源(写在循环外)
#eg:imagedestroy($s_img);
#解析结束


根据解决在写一遍图片锐化代码

function sharp($background, $degree, $location){
#step 1 创建图片资源
$s_img=imagecreatefromjpeg($background);
#step 2 获取图片资源的宽高
$b_x=imagesx($s_img);
$b_y=imagesy($s_img);
#step 3 两层循环进行锐化  外层控制宽 内层控制高
for( $i=$b_x-1; $i>0; $i-- ){
for( $j=$b_y-1; $j>0; $j-- ){
#step 4 取得图片索引的颜色:当前颜色和上一个颜色
$s_img_color1=imagecolorsforindex($s_img, imagecolorat($s_img, $i-1, $j-1));
$s_img_color2=imagecolorsforindex($s_img, imagecolorat($s_img, $i, $j));
#step 5 固定算法对取得的颜色进行锐化
$r=intval($s_img_color2["red"]+$degree*($s_img_color2["red"]-$s_img_color1["red"]));
$g=intval($s_img_color2["green"]+$degree*($s_img_color2["green"]-$s_img_color1["green"]));
$b=intval($s_img_color2["blue"]+$degree*($s_img_color2["blue"]-$s_img_color1["blue"]));
#step 6 把取得的RGB颜色控制在0~255正常范围内
$r=min(255, max($r, 0));
$g=min(255, max($g, 0));
$b=min(255, max($b, 0));
#step 7 取得锐化后颜色的索引
if( ($d_clr=imagecolorexact($s_img,$r,$g,$b)) ==-1){
$d_clr=imagecolorallocate($s_img,$r,$g,$b);
}
#step 8 把锐化后的颜色重新赋给图片资源的每一个像素
imagesetpixel($s_img, $i, $j, $d_clr);
}
}
imagejpeg($s_img,$location);
imagedestroy($s_img);
}
sharp(&#39;./images/hee.jpg&#39;,50,&#39;./images/hee16.jpg&#39;);
Copy after login

相关推荐:

JS的图片处理与合成详解

JavaScript关于图片处理与合成的方法详解

PHP进行批量图片处理时程序执行超时

The above is the detailed content of How to implement php code to process images. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles