作者 lyh

hx

... ... @@ -34,13 +34,12 @@ class TestController extends BaseController
<p><a href="mailto:edinguas@gmail.com">Email:edinguas@gmail.com</a></p>';
$pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
$matches = [];
// Use preg_match_all to find all src attributes
preg_match_all($pattern, $test, $matches);
$updatedSources = $this->saveBase64Images($matches[1]);
// Return an array of all src URLs found
// $newsLogic = new NewsLogic();
// $key = $newsLogic->processImage($test);
$this->response('success',Code::SUCCESS,$updatedSources);
foreach($updatedSources as $k => $v){
$outputString = str_replace($v, $k, $outputString);
}
$this->response('success',Code::SUCCESS,$outputString);
}
public function saveBase64Images($imageSources) {
... ... @@ -50,22 +49,16 @@ class TestController extends BaseController
if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
$imageType = $match[1]; // Image type (png, jpg, etc.)
$base64Data = preg_replace('/^data:image\/(png|jpg|jpeg|gif);base64,/', '', $src);
// Decode the base64 data
$decodedData = base64_decode($base64Data);
if ($decodedData === false) {
$updatedSources[] = $src; // If decoding fails, keep original src
continue;
}
// Generate a unique filename and path
$outputFile = 'images' . uniqid() . '.' . $imageType;
$imageUrl = 'http://your-domain.com/' . $outputFile;
$updatedSources[] = $imageUrl;
} else {
$updatedSources[] = $src;
$updatedSources[$imageUrl] = $src;
}
}
return $updatedSources;
}
... ...