作者 lyh

hx

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