我正在使用PHP写图片上的图片。我很容易从当前图片创建新图片并使用PHP写文本。但我也希望当我点击应用更改时,将这个新图片移动到我的目录(move_uploaded_file)并用新图像替换当前图像,新图像名称必须与之前图像相同,因为我正在使用PHP下载它。从@imagecreatefromjpeg获得<img>标记?
这是我用来写它的代码。
HTML代码:
<img id="image" src="<?php echo "upload_pic/" . $_FILES["image_file"]["name"]; ?>" alt="your_image" />
<input type="button" name="save_image" id="save_image" value="Save Image" />
<input type="hidden" id="hidden_image_name" name="hidden_image_name" value="<?php echo $_FILES["image_file"]["name"]; ?>" />
jQuery代码:
jQuery('#save_image').click(function(){
var image_name = jQuery('#hidden_image_name').val();
jQuery.ajax({
url:'text_image.php',
data:'file='+image_name,
type:'get',
success:function(data){
alert(data);
}
});
});
text_image.php
<?php
$file = 'upload_pic/'.$_GET['file'];
/*** set the header for the image ***/
header("Content-type: image/jpeg");
/*** specify an image and text ***/
$im = writeToImage($file, 'PHPRO rules again');
//echo $im;
/*** spit the image out the other end ***/
imagejpeg($im);
/**
*
* @Write text to an existing image
*
* @Author Kevin Waterson
*
* @access public
*
* @param string The image path
*
* @param string The text string
*
* @return resource
*
*/
function writeToImage($imagefile, $text){
/*** make sure the file exists ***/
if(file_exists($imagefile))
{
/*** create image ***/
$im = @imagecreatefromjpeg($imagefile);
/*** create the text color ***/
$text_color = imagecolorallocate($im, 233, 14, 91);
/*** splatter the image with text ***/
imagestring($im, 6, 25, 150, "$text", $text_color);
}
else
{
/*** if the file does not exist we will create our own image ***/
/*** Create a black image ***/
$im = imagecreatetruecolor(150, 30); /* Create a black image */
/*** the background color ***/
$bgc = imagecolorallocate($im, 255, 255, 255);
/*** the text color ***/
$tc = imagecolorallocate($im, 0, 0, 0);
/*** a little rectangle ***/
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/*** output and error message ***/
imagestring($im, 1, 5, 5, "Error loading $imagefile", $tc);
}
return $im;
}
?>
在先进的感谢!
我检查响应这是非常复杂的JFIF> CREATOR:GD-JPEG V1.0(使用IJG JPEG V62),默认质量 Ç\t \t $。' ”,#(7),01444'9 = 82 <.342C \t \t \t 2 !! 22222222222222222222222222222222222222222222222222" \t }!1AQa“Q2#BR $ 3房 – CuteBabyMannu
这不是‘复杂’,这正是一个JPEG图像看上去,当你在文本模式下查看等。 – Spudley
另外,要小心使用'$ _FILES [“image_file”] [“name”] - 因为文件的名字由客户端决定,所以它有可能被用作攻击媒介。包含斜线,你的代码可能会加载一个不同的文件到你期望的文件中,你应该确保你在使用之前对它进行了清理,或者根本不使用它(即为你上传的文件构建你自己的名字)。 – Spudley