作者 lyh

hx

@@ -34,13 +34,12 @@ class TestController extends BaseController @@ -34,13 +34,12 @@ class TestController extends BaseController
34 <p><a href="mailto:edinguas@gmail.com">Email:edinguas@gmail.com</a></p>'; 34 <p><a href="mailto:edinguas@gmail.com">Email:edinguas@gmail.com</a></p>';
35 $pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i'; 35 $pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
36 $matches = []; 36 $matches = [];
37 - // Use preg_match_all to find all src attributes  
38 preg_match_all($pattern, $test, $matches); 37 preg_match_all($pattern, $test, $matches);
39 $updatedSources = $this->saveBase64Images($matches[1]); 38 $updatedSources = $this->saveBase64Images($matches[1]);
40 - // Return an array of all src URLs found  
41 -// $newsLogic = new NewsLogic();  
42 -// $key = $newsLogic->processImage($test);  
43 - $this->response('success',Code::SUCCESS,$updatedSources); 39 + foreach($updatedSources as $k => $v){
  40 + $outputString = str_replace($v, $k, $outputString);
  41 + }
  42 + $this->response('success',Code::SUCCESS,$outputString);
44 } 43 }
45 44
46 public function saveBase64Images($imageSources) { 45 public function saveBase64Images($imageSources) {
@@ -50,22 +49,16 @@ class TestController extends BaseController @@ -50,22 +49,16 @@ class TestController extends BaseController
50 if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) { 49 if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
51 $imageType = $match[1]; // Image type (png, jpg, etc.) 50 $imageType = $match[1]; // Image type (png, jpg, etc.)
52 $base64Data = preg_replace('/^data:image\/(png|jpg|jpeg|gif);base64,/', '', $src); 51 $base64Data = preg_replace('/^data:image\/(png|jpg|jpeg|gif);base64,/', '', $src);
53 -  
54 - // Decode the base64 data  
55 $decodedData = base64_decode($base64Data); 52 $decodedData = base64_decode($base64Data);
56 if ($decodedData === false) { 53 if ($decodedData === false) {
57 $updatedSources[] = $src; // If decoding fails, keep original src 54 $updatedSources[] = $src; // If decoding fails, keep original src
58 continue; 55 continue;
59 } 56 }
60 - // Generate a unique filename and path  
61 $outputFile = 'images' . uniqid() . '.' . $imageType; 57 $outputFile = 'images' . uniqid() . '.' . $imageType;
62 $imageUrl = 'http://your-domain.com/' . $outputFile; 58 $imageUrl = 'http://your-domain.com/' . $outputFile;
63 - $updatedSources[] = $imageUrl;  
64 - } else {  
65 - $updatedSources[] = $src; 59 + $updatedSources[$imageUrl] = $src;
66 } 60 }
67 } 61 }
68 -  
69 return $updatedSources; 62 return $updatedSources;
70 } 63 }
71 64