Home  >  Article  >  Backend Development  >  The use of image processing in PHP articles_PHP tutorial

The use of image processing in PHP articles_PHP tutorial

WBOY
WBOYOriginal
2016-07-22 09:02:33836browse

array_diff($arr1,$arr2)

One of php array functions, used to calculate the difference set of arrays.
Regularly match html image tags
Image deletion operation added with sinaeditor
One of the usages is in the process of publishing an article using Sina editor tonight.
This function is used

Problem description:

There are several pictures in the article. They will be automatically uploaded to the picture directory of the website during the process of adding the article
In the process of modifying the article, if you perform related deletion operations on the pictures, then although it is in the code (already stored in the database);
The data tags have been deleted. Tags similar to . But the image files still exist in
On the website. This requires certain processing

How to deal with it:

First: get the original article content from the database
Get the file name of the image from it
Regular rules are used

The method is as follows

code show as below:
public function getimgsinarticle($content)
{
$temp = array();
$imgs = array();
preg_match_all('/http[^d]*[d]+[.](jpg|gif|png)/',$content,$temp);
$temp = $temp[0];
if(!empty($temp[0]))
{
for($i=0;$i {
$imgs[$i] = pathinfo($temp[$i]);
$imgs[$i] = $imgs[$i]['basename'];
}
return $imgs;
}
else
{
return false;
}
}


Explain the regular rules. First match the four letters of http and then match several non-numeric characters. Match numeric characters up to
One less, one matching dot (.), the match ends with jpg or gif or png and is searched from $congtent. The result is stored in $temp.
Save the pictures in the original data in the database in an array. Name it $oldimgs
I think this place should be improved. After saving, it prints out a two-dimensional array. It is a bit cumbersome to use
Note: My picture name is similar to this: "201111291322589013.jpg"

Step 2:
The method to find all the images from the content submitted by the user is as above. The second array is obtained and named $newimgs
The method to find the difference set between arr1 and arr2 is as follows
--That is to say, if the picture in the original data does not exist in the user's newly submitted content, then the picture will be deleted.

code show as below:
$oldimgs = $this->getimgsinarticle($oldarticledata['article_content']);
$newimgs = $this->getimgsinarticle($data['articlecontent']);
//print_r($newimgs);
$newimgs = empty($newimgs)?array():$newimgs;
if($oldimgs!=false)
{
$diff = array_diff($oldimgs,$newimgs);
$diff = array_values($diff);
if(!empty($diff))
{
for($i=0;$i {
$this->delimg($diff[$i],ARTICLE_IMG_DIR);
}
}
}


The method to delete pictures is as follows. It’s very simple.

code show as below:
public function delimg($imgname,$dir)
{
@unlink($dir.'/'.$imgname);
return true;
}


In this way, my purpose is achieved. When a user edits an article with a picture, if the picture is deleted, the corresponding picture will also be deleted from the website
The method of getting the name of the picture in the article can also be applied to the process of deleting the article.

In the method of deleting pictures, you can use realpath(__FILE__) and add various "./""../" to $dir to give the picture directory relative to the website directory
The regular rules here are not very good for getting the path in HTML. It needs to be studied. I recently found a regular book. It’s very good

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/371921.htmlTechArticle array_diff($arr1,$arr2) is one of the php array functions, used to calculate the difference set of arrays. Regularly matching html image tags is one of the image deletion operations added by sinaeditor. I am using Sina tonight...
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