Use of image processing in PHP articles

Source: Internet
Author: User

Array_diff ($arr 1, $arr 2)

One of the PHP array functions used to compute the difference set of an array.
Regular matching HTML picture label
Picture delete operation added with Sinaeditor
One of the ways to publish an article this evening in the Sina editor.
This function was used

Problem Description:

There are a number of pictures in the article. Automatically uploaded to the site's image catalogue during the addition of the article
If the picture is deleted during the modification of the article. So although in the code (already stored in the database);
The label for the data has been deleted. A label like . But the picture file still exists in
On the website. This time it needs some processing.

Treatment methods:

First: Get the original article content from the database
get the file name of the picture from inside
With a regular

Methods are as follows

The

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;


}


}


For regular explanations, first match the HTTP four letters and then match the number of Non-numeric characters. Match numeric characters to
One less, matching point (.) One that matches a JPG or GIF or PNG end to look up from the $congtent. The result is deposited in $temp.
Save 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 bit of a bother.
Note: My picture name is similar to the one named: "201111291322589013.jpg"

Step Two:
Find all the pictures from the content submitted by the user as above. Get array two named $newimgs
The method for arr1 and arr2 difference sets is as follows
-that is, if the picture in the original data does not exist in the user's newly submitted content. Then the picture will be deleted.

The

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 way to delete a picture is as simple as this.

The

code is as follows:


Public Function delimg ($imgname, $dir)


{


@unlink ($dir. '/'. $imgname);


return true;


}


So my purpose is achieved. When the user edits an article with a picture. If you delete the picture, the corresponding picture will also be deleted from the site.
The method of getting the name of the picture in the article can also be applied to the process of deleting the article.

$dir in the method of deleting a picture can be Realpath (__file__) plus various "./" "... /"To give a catalogue of the pictures in relation to the site
For getting the path in the HTML the regular writing here is not very good. To be studied. A regular book was recently found. It's good.

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.