php notes: the use of image processing in articles

  • 2020-06-01 08:44:35
  • OfStack

array_diff($arr1,$arr2)
php array function 1, used to calculate the difference set of an array.
Match the html image label regularly
Use sinaeditor to add the image delete operation
In the process of publishing the article with sina editor tonight.
This function is used

Problem description:

There are a number of images in the article. In the process of adding the article automatically uploaded to the site's image directory
If the image is deleted in the process of modifying the article, it will be deleted in the code (which has been stored in the database).
The tag of the data has been deleted < img src=http://...... > Such a tag. But the image file still exists
On the website. At this time need 1 definite processing

Treatment:

First: get the original article content from the database
Get the file name of the image from inside
We use the word regex

Methods the following


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 regularity, first match http4 letters and then match a number of non-numeric characters. Match numeric characters to
One less, one match point (.), a match ending in jpg or gif or png is looked up from $congtent. The result is saved into $temp.
Save the images from the raw data in the database in an array. Name them $oldimgs
I think I should improve this area, save it and print it out as a 2-dimensional array. It's a little hard to use
Note: the name of my pictures are similar to the name of like this: "201111291322589013. jpg"

Step 2:
Find all images from user submissions as shown above. Get array 2 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 content submitted by the user, the image will be deleted.


$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 to delete the image is as follows.

 public function delimg($imgname,$dir)
 {
  @unlink($dir.'/'.$imgname);
  return true;
 }

This is my goal. When a user edits an article with a picture, if the picture is deleted, the corresponding picture will be removed from the site
The method of getting the name of the image in the article can also be applied to the process of deleting the article.

$dir in the method of deleting pictures can be added with realpath(s 62en__) plus various "./".. /" to give the image directory relative to the site directory
Regex here is not well written for getting the path in html. To be studied. Recently found a regex book. Very good
Proficient in regular expressions 3rd edition Jeffrey E.F.Friedl, translated by yu sheng (cheng)


Related articles: