Home  >  Article  >  Backend Development  >  How to create and manipulate images with PHP

How to create and manipulate images with PHP

小云云
小云云Original
2018-03-15 09:39:364745browse

I studied the built-in function library of php and drew images. I originally came to this piece of knowledge with sadness and perseverance. After all, SVGdrawing is a big deal for me. After a brief look at the results, I found that it is still very simple. After all, many methods are built-in in PHP. You only need to understand the parameters and you can use them directly.

First of all, drawing a picture is divided into several steps:



Then proceed step by step according to the process of creating pictures. The source code is as follows

         $img = ImageCreate(400,60);   //创建一个宽400 高60的图片
    
         //创建以bg.jpg为背景的图片
         //$img = ImageCreateFromJpeg('./bg.jpg');

         //创建颜色
         $black = imagecolorallocate($img, 100, 116, 163); //创建颜色
         $red = imagecolorallocate($img,255,0,0);
         $white = imagecolorallocate($img,255,255,255);

         //绘制了矩形的轮廓
         imagerectangle($img, 10, 10, 30, 30, $white);

         //填充矩形
         imagefilledrectangle($img, 20, 20, 40, 40, $white);
     
         //填写文字
         imagettftext($img, 12, 0, 80, 35, $white, 'love.ttf', '大家好,这是我的网站!欢迎光临!');

         //生成图片
         header('Content-type:image/png');
         ImagePng($img);
         ImageDestroy($img);

Note: ImagePng($img,'my.png'); You can generate a picture of my.png under the current server path

The generated picture display effect is:



Of course, if a picture is the background ($img = ImageCreateFromJpeg('./bg.jpg') ) The display effect is:




This achieves the effect of direct manipulation of images through PHP~~ ~ Isn’t it very powerful? If you don’t have PS installed, you can just stretch out your little hand and type in a few lines of PHP code and it will be done. Haha~~~

handsomehas a fart use! In the end, he was eaten by pawns!

Related recommendations:

Create image thumbnails with PHP

##JS operation of images (add, delete, change) Example_ javascript skills

JS daily question-JS for small demo to achieve seamless switching of images up, down, left, and right through keyboard arrow keys

The above is the detailed content of How to create and manipulate images with PHP. For more information, please follow other related articles on the PHP Chinese website!

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