如何用PHP为图片添加文字?代码未生效,求排查错误
为什么我的PHP代码无法给PNG图片添加文字?
我尝试用PHP给一张PNG图片添加文字,但代码执行后图片上并没有显示文字。以下是我的代码:
<?php error_reporting(E_ALL); $thumb_src = 'copyright-symbol.png'; $explode = explode('.', $thumb_src); $extension = strtolower( end ( $explode ) ); $file_name = basename( $thumb_src ); //image_resize_base_width( $relative_url, $relative_url, 350, $extension); $jpg_image = imagecreatefrompng( $thumb_src ); // set font size $font = @imageloadfont($jpg_image); $fontSize = imagefontwidth($font); $orig_width = imagesx($jpg_image); $orig_height = imagesy($jpg_image); // Create your canvas containing both image and text $canvas = imagecreatetruecolor( $orig_width, ($orig_height + 40 ) ); // Allocate A Color For The background $bcolor = imagecolorallocate( $canvas, 255, 255, 255 ); // Add background colour into the canvas imagefilledrectangle( $canvas, 0, 0, $orig_width, ($orig_height + 40), $bcolor ); // Save image to the new canvas imagecopyresampled( $canvas, $jpg_image, 0, 0, 0, 0, $orig_width, $orig_height, $orig_width, $orig_height ); $font_path = 'font/arial.ttf'; $path = 'upload'; $text = 'cc-by-nd-'; // Allocate A Color For The Text $color = imagecolorallocate($canvas, 0, 0, 0); // Print Text On Image imagettftext( $canvas, 13, 0, 0, $orig_height + 25, $color, $font_path, $text) ; // Send Image to Browser imagepng( $canvas, $path . '/' . $file_name ); // Clear Memory imagedestroy($canvas);
问题排查与修复
我帮你梳理了代码里的几个关键问题,这些就是导致文字无法显示的核心原因:
错误使用
imageloadfont()
你用@imageloadfont($jpg_image)尝试加载字体,但这个函数的参数应该是点阵字体文件路径(比如.gdf格式),而不是你传入的图片资源$jpg_image。更糟的是你用@抑制了错误,导致这个问题没有被暴露出来。而且你后面用的是imagettftext()(用于加载TrueType字体),这段关于$font和$fontSize的代码完全多余,甚至会干扰后续逻辑。字体路径与文件存在性问题
$font_path = 'font/arial.ttf';需要确保路径正确:如果是相对路径,要以PHP脚本的执行位置为基准;同时服务器必须能访问到这个字体文件,否则imagettftext()会静默失败。输出目录的权限与存在性
$path = 'upload';需要确保这个目录已经存在,并且PHP进程有写入权限,否则无法生成最终的图片文件。(可选)文字位置优化
你设置的X坐标是0,文字会紧贴图片左边缘,可能看起来不美观,可以调整为居中或者合适的偏移量。
修复后的代码
<?php error_reporting(E_ALL); ini_set('display_errors', 1); // 开启错误显示,方便调试 $thumb_src = 'copyright-symbol.png'; $explode = explode('.', $thumb_src); $extension = strtolower(end($explode)); $file_name = basename($thumb_src); // 检查原始图片是否存在 if (!file_exists($thumb_src)) { die("原始图片不存在:{$thumb_src}"); } $png_image = imagecreatefrompng($thumb_src); $orig_width = imagesx($png_image); $orig_height = imagesy($png_image); // 创建新画布 $canvas = imagecreatetruecolor($orig_width, $orig_height + 40); // 分配背景色并填充 $bcolor = imagecolorallocate($canvas, 255, 255, 255); imagefilledrectangle($canvas, 0, 0, $orig_width, $orig_height + 40, $bcolor); // 将原始图片复制到新画布 imagecopyresampled($canvas, $png_image, 0, 0, 0, 0, $orig_width, $orig_height, $orig_width, $orig_height); $font_path = 'font/arial.ttf'; $path = 'upload'; $text = 'cc-by-nd-'; // 检查字体文件是否存在 if (!file_exists($font_path)) { die("字体文件不存在:{$font_path}"); } // 检查输出目录是否存在,不存在则创建 if (!is_dir($path)) { mkdir($path, 0755, true); } // 分配文字颜色 $color = imagecolorallocate($canvas, 0, 0, 0); // 计算文字宽度,实现水平居中(可选) $text_box = imagettfbbox(13, 0, $font_path, $text); $text_width = $text_box[2] - $text_box[0]; $x = ($orig_width - $text_width) / 2; // 水平居中 // 添加文字到图片 imagettftext($canvas, 13, 0, $x, $orig_height + 25, $color, $font_path, $text); // 保存图片 $output_file = $path . '/' . $file_name; if (!imagepng($canvas, $output_file)) { die("无法保存图片到:{$output_file}"); } // 释放内存 imagedestroy($png_image); imagedestroy($canvas); echo "图片处理完成,保存路径:{$output_file}"; ?>
内容的提问来源于stack exchange,提问作者Shibbir
相关产品推荐
相关产品推荐

