我有一个简单的脚本,它为盗链添加水印。最近它开始消耗 CPU(有时一位用户的 CPU 使用率达到 12%!)。我能做些什么来最小化 CPU 使用率?
set_time_limit ( 3 );
if(!empty($_GET['e'])){
$data = getimagesize("images/".$_GET['e']);
// create base image
$base_image = imagecreatetruecolor($data[0],$data[1]);
$photo = imagecreatefromjpeg("images/".$_GET['e']);
$top_image = imagecreatefrompng('watermark.png');
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagesavealpha($base_image, true);
imagealphablending($base_image, true);
$data2 = getimagesize('watermark.png');
// merge images
imagecopy($base_image, $photo, 0, 0, 0, 0, $data[0], $data[1]);
imagecopy($base_image, $top_image, 0, 0, 0, 0, $data2[0], $data2[1]);
// return file
header('Content-Type: image/png');
imagepng($base_image);
} else
header('Location: http://www.mydomain.com/');