imagecreatefrompng() 是 PHP 中用于从 PNG 图像文件创建图像资源的一个函数
$image = imagecreatefrompng('path/to/your/image.png'); 确保将 'path/to/your/image.png' 替换为您的 PNG 图像文件的实际路径。
$width = imagesx($image); $height = imagesy($image); if ($image === false) { echo 'Error: Unable to create image resource from PNG file.'; } else { // 图像加载成功,继续处理 } $newWidth = 300; $newHeight = 200; $resizedImage = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); $outputPath = 'path/to/your/output/image.png'; imagepng($resizedImage, $outputPath); 确保将 'path/to/your/output/image.png' 替换为您希望保存处理后图像的实际路径。
imagedestroy($image); imagedestroy($resizedImage); 将以上代码片段组合在一起,您可以使用 imagecreatefrompng() 函数处理 PNG 图像。根据您的需求,您可以对图像执行其他操作,例如旋转、裁剪、添加文字等。