PHP Notes: The use of image processing in the article _php tutorial

Source: Internet
Author: User
Array_diff ($arr 1, $arr 2)
One of the PHP array functions used to calculate the difference set of the array.
Regular matching HTML picture tags
Picture delete operation added with Sinaeditor
Usage, in the process of publishing the article with Sina Editor this evening.
This function was used

Problem Description:

There are several pictures in the article. Automatically upload to the site's image catalog as you add articles
In the process of modifying the article, if the picture is related to the delete operation. So though in the code (has been deposited into the database);
The label for the data has been deleted. A label similar to this. But the picture file still exists in
On the website. This time needs some processing.

Treatment methods:

First: Get the original article content from the database
Get the file name of the picture from the inside
I used the regular.

Methods are as follows
Copy the Code code 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<>
{
$imgs [$i] = PathInfo ($temp [$i]);
$imgs [$i] = $imgs [$i] [' basename '];
}
return $IMGS;
}
Else
{
return false;
}
}

To interpret the regular, first match the HTTP four letters and then match a number of non-numeric characters. Match numeric characters to
One less, matching point (.) One, matching with JPG or GIF or PNG end from $congtent. Results are stored in the $temp.
Saves the pictures in the original data in the database in an array. Named $oldimgs
This place I think should be improved, after the deposit is printed out is a two-dimensional array. It's a little cumbersome to use.
Note: My picture name is similar to the one named: "201111291322589013.jpg"

Step Two:
Find all the image methods from the content submitted by the user. Get array two named $newimgs
The difference set method for arr1 and ARR2 is as follows
-that is, if the image in the original data does not exist in the user's newly submitted content. 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 <>
{
$this->delimg ($diff [$i],article_img_dir);
}
}
}

The way to delete a picture is as simple as this.
Copy the Code code as follows:
Public Function delimg ($imgname, $dir)
{
@unlink ($dir. '/'. $imgname);
return true;
}

That's what I'm going to do. When the user edits an article with a picture. If the picture is deleted. The corresponding image will also be deleted from the website.
The method of getting the name of a picture in an article can also be applied to the process of deleting an article.

In the method of deleting the picture $dir can use Realpath (__file__) plus various "./" ". /"To give the directory of the picture directory relative to the website
For getting the path in the HTML, the regular writing here is not very good. To be studied. A book of regular books has been found recently. Very good.
Proficient in regular expression third edition Jeffrey e.f. Friedl, Yu Yu (Cheng)

http://www.bkjia.com/PHPjc/326760.html www.bkjia.com true http://www.bkjia.com/PHPjc/326760.html techarticle Array_diff ($arr 1, $arr 2) One of the PHP array functions that is used to calculate the difference set of the array. Regular matching HTML picture tags with sinaeditor add a picture delete operation usage One, tonight in Sina ...

  • 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.