Home > Backend Development > PHP Tutorial > How to use php to process images in articles

How to use php to process images in articles

WBOY
Release: 2016-07-25 08:55:40
Original
1190 people have browsed it
  1. public function getimgsinarticle($content)
  2. {
  3. $temp = array();
  4. $imgs = array();
  5. preg_match_all('/http[^d]*[d]+ [.](jpg|gif|png)/',$content,$temp);
  6. $temp = $temp[0];
  7. if(!empty($temp[0]))
  8. {
  9. for($i =0;$i {
  10. $imgs[$i] = pathinfo($temp[$i]);
  11. $imgs[$i] = $imgs[$i][ 'basename'];
  12. }
  13. return $imgs;
  14. }
  15. else
  16. {
  17. return false;
  18. }
  19. }
Copy code

The above uses regular expressions. Explain: first match the four letters of http Then match several non-digit characters. Match at least one numeric character, match one dot (.), and search for the match ending with jpg or gif or png from $congtent. The result is stored in $temp. Save the images in the original data in the database in an array named $oldimgs.

This needs improvement. After saving, it prints out a two-dimensional array, which is a bit cumbersome to use.

Explanation, 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.

  1. $oldimgs = $this->getimgsinarticle($oldarticledata['article_content']);
  2. $newimgs = $this->getimgsinarticle($data['articlecontent']);
  3. //print_r($newimgs);
  4. $newimgs = empty($newimgs)?array():$newimgs;
  5. if($oldimgs!=false)
  6. {
  7. $diff = array_diff($oldimgs,$newimgs);
  8. $diff = array_values($diff);
  9. if(!empty($diff))
  10. {
  11. for($i=0;$i {
  12. $this-> delimg($diff[$i],ARTICLE_IMG_DIR);
  13. }
  14. }
  15. }
Copy code

Delete pictures:

  1. public function delimg($imgname,$dir)
  2. {
  3. @unlink($dir.'/'.$imgname);
  4. return true;
  5. }
Copy code

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__) plus various "./" "../" to $dir to give the picture directory relative to the website directory.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template