Php notes: Use of Image Processing in articles

Source: Internet
Author: User

Array_diff ($ arr1, $ arr2)
One of the php array functions is used to calculate the array's difference set.
Regular Expression matching html image tags
Delete images added with sinaeditor
Usage 1: This evening, when I published an article in the sina editor.
This function is used.

Problem description:

There are several images in the article. The images will be automatically uploaded to the image directory of the website during the process of adding the article.
If you delete an image when you modify the document, the image is stored in the database in the code );
Labels of the deleted data, such as . But the image file still exists in
On the website. Some processing is required at this time.

Solution:

First, get the original article content from the database.
Get the image file name from it
The regular expression is used.

The method is as follows:

Copy codeThe Code is as follows: 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 <count ($ temp); $ I ++)
{
$ Imgs [$ I] = pathinfo ($ temp [$ I]);
$ Imgs [$ I] = $ imgs [$ I] ['basename'];
}
Return $ imgs;
}
Else
{
Return false;
}
}

To explain the regular expression, match the http four letters first and then match the non-numeric characters. Match the numeric characters
One fewer match point (.). The match ends with jpg, gif, or png and searches for it from $ congtent. The result is saved to $ temp.
Save the images in the original data in the database in the array. Name it $ oldimgs
In this case, I think it should be improved to save it and print it out as a two-dimensional array.
Note: My image name is similar to this: "201111291322589013.jpg"

Step 2:
Find all the image methods from the content submitted by the user as above. Get array 2 named $ newimgs
The following method is used to evaluate the difference set between arr1 and arr2:
-- If the image in the original data does not exist in the content submitted by the user, the image will be deleted.

Copy codeThe Code is as follows: $ 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 <count ($ diff); $ I ++)
{
$ This-> delimg ($ diff [$ I], ARTICLE_IMG_DIR );
}
}
}

The method for deleting an image is as follows.
Copy codeThe Code is as follows: public function delimg ($ imgname, $ dir)
{
@ Unlink ($ dir. '/'. $ imgname );
Return true;
}

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

In the method of deleting an image, you can use realpath (_ FILE _) to add various "./" ../"to show the directory of the image relative to the website.
It is not very good to get the path in html. It is still to be studied. Recently, I found a regular book. It is very good.
Familiar with regular expressions, translated by Jeffrey E.F. Friedl, cheng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.