使用变量值的图像jpeg输出在PHP GD中不起作用


imagejpeg output using variable value not working in PHP GD

<?php
include('core/init.php');
$name = mysql_result(mysql_query("SELECT `photo_urlpath` FROM photos ORDER BY `photo_id` DESC LIMIT 1"), 0);
        if($name == false){
            echo "Failed to open $name";
        } else {
            echo "Worked! <br>" . "$name";
        }
$stamp = imagecreatefrompng('upload/watermarkMQ.png');
$im = imagecreatefromjpeg($name);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);
// Output and free memory
    imagejpeg($im, "wm_" . $name);
    imagedestroy($im);
?>

mysql 结果返回photo_urlpath并且运行良好,因为它回显"工作!",然后是文件名。

唯一不起作用的是 imagejpeg($im, "wm_" . $name(;

我想使用相同的文件名,但在原始图像名称之前带有wm_。但是使用该代码它不会输出任何内容。当我用"测试.jpg"之类的东西替换$name时,它确实有效,并且输出了一个不错的wm_test.jpg文件。

你不能使用变量作为输出文件名吗?

我不确定它是否对您有帮助,但我正在从文本文件中读取文件名,并且存储路径的每个字符串变量末尾仍然有一个行尾字符。我只是用

$fileName = substr($fileName, 0, -1);

现在它运行完美。我建议您检查SQL查询给您的内容。某处可能有一些特殊字符需要删除。